From 81c677c3be1846251c6b80edca1023a06b691583 Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Tue, 19 Nov 2019 21:14:20 +0100 Subject: [PATCH] internal/http/request.go: replace GetReason() with GetReasonFromBody() Update tests to inject the required JSON error body with reason. --- internal/client/route_remove_test.go | 5 +++-- internal/client/set_test.go | 7 ++++--- internal/http/request.go | 6 +++++- internal/http/request_test.go | 4 +++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/client/route_remove_test.go b/internal/client/route_remove_test.go index 21fa460..1001dca 100644 --- a/internal/client/route_remove_test.go +++ b/internal/client/route_remove_test.go @@ -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) } diff --git a/internal/client/set_test.go b/internal/client/set_test.go index d13f5b9..8b94c65 100644 --- a/internal/client/set_test.go +++ b/internal/client/set_test.go @@ -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() { diff --git a/internal/http/request.go b/internal/http/request.go index a01063d..ecea096 100644 --- a/internal/http/request.go +++ b/internal/http/request.go @@ -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 { diff --git a/internal/http/request_test.go b/internal/http/request_test.go index 86e9921..bd13c2c 100644 --- a/internal/http/request_test.go +++ b/internal/http/request_test.go @@ -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) {