internal/http/request.go: replace GetReason() with GetReasonFromBody()
Update tests to inject the required JSON error body with reason.
This commit is contained in:
@@ -43,12 +43,13 @@ func TestRemoveRouteErrorNonExistent(t *testing.T) {
|
||||
defer gock.Off()
|
||||
gock.New("http://localhost:8080").
|
||||
Delete("/routes/ROUTE_BAD").
|
||||
Reply(http.StatusNotFound)
|
||||
Reply(http.StatusNotFound).
|
||||
BodyString(`{"reason": "Route Not Found"}`)
|
||||
|
||||
err := RemoveRoute("http://localhost:8080", "ROUTE_BAD")
|
||||
if err == nil {
|
||||
t.Errorf("Error not reported for nonexistent route")
|
||||
} else if err.Error() != "Not Found" {
|
||||
} else if err.Error() != "Route Not Found" {
|
||||
t.Errorf(`Error mismatch: got %q, want "Not Found"`, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,8 @@ func TestSetDataErrIfBadHandlerID(t *testing.T) {
|
||||
defer gock.Off()
|
||||
gock.New("http://localhost:8080").
|
||||
Put("/HANDLER_BAD/response/status/code").
|
||||
Reply(http.StatusNotFound)
|
||||
Reply(http.StatusNotFound).
|
||||
BodyString(`{"reason": "Handler ID Not Found"}`)
|
||||
|
||||
if err := client.SetData(
|
||||
"http://localhost:8080",
|
||||
@@ -61,8 +62,8 @@ func TestSetDataErrIfBadHandlerID(t *testing.T) {
|
||||
strings.NewReader("200"),
|
||||
); err == nil {
|
||||
t.Error("Expected error not present")
|
||||
} else if err.Error() != "Not Found" {
|
||||
t.Errorf(`Error mismatch: expected "Not Found", got %q`, err)
|
||||
} else if err.Error() != "Handler ID Not Found" {
|
||||
t.Errorf(`Error mismatch: expected "Handler ID Not Found", got %q`, err)
|
||||
}
|
||||
|
||||
if !gock.IsDone() {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user