internal/http/request.go: replace GetReason() with GetReasonFromBody()

Update tests to inject the required JSON error body with reason.
This commit is contained in:
pancho horrillo
2019-11-19 21:14:20 +01:00
parent e7619238ae
commit 81c677c3be
4 changed files with 15 additions and 7 deletions
+5 -1
View File
@@ -66,7 +66,11 @@ func Request(method string, url string, contentType string, r io.Reader, w io.Wr
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return errors.New(GetReason(res))
reason, err := GetReasonFromBody(res)
if err != nil {
return err
}
return errors.New(reason)
}
if w == nil {
+3 -1
View File
@@ -72,7 +72,9 @@ func TestReturnHTTPErrorAsIs(t *testing.T) {
func TestReturnHTTPReasonAsErrorWhenUnsuccessful(t *testing.T) {
defer gock.Off()
gock.New("http://localhost").Reply(http.StatusTeapot)
gock.New("http://localhost").
Reply(http.StatusTeapot).
BodyString(`{"reason": "I'm a teapot"}`)
err := Request("GET", "http://localhost", "", nil, nil)
if err == nil || err.Error() != http.StatusText(http.StatusTeapot) {