impr: more verbose http api status errors

This commit is contained in:
mehanizm
2022-03-25 09:28:05 +03:00
parent 14c5a59a88
commit a8da2361cf
2 changed files with 62 additions and 2 deletions

View File

@@ -6,7 +6,10 @@
package airtable
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
)
@@ -20,3 +23,34 @@ func TestHTTPClientError_Error(t *testing.T) {
t.Errorf("HTTPClientError.Error() = %v, want %v", got, expected)
}
}
func Test_makeHTTPClientError(t *testing.T) {
tests := []struct {
name string
url string
resp *http.Response
expected string
}{
{
name: "400",
url: "google.com",
resp: &http.Response{
StatusCode: 400,
Status: "Bad Request",
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
},
expected: `status 400, err: HTTP request failure on google.com:
400 Bad Request
The request encoding is invalid; the request can't be parsed as a valid JSON.
Body: body`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := makeHTTPClientError(tt.url, tt.resp); err.Error() != tt.expected {
t.Errorf("makeHTTPClientError() error:\n%v\n\nexpected:\n%v", err, tt.expected)
}
})
}
}