Skip to content

Commit

Permalink
Merge 5c700e1 into f6cd63a
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwasila committed Jul 27, 2018
2 parents f6cd63a + 5c700e1 commit b55df1d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func main() {
| 422 | UnprocessableEntity() |
| 500 | InternalServerError() |
| 501 | NotImplemented() |
| 502 | BadGateway() |
| 503 | ServiceUnavailable() |
| 504 | GatewayTimeout() |

See [here](https://httpstatuses.com/) for a complete list of HTTP responses, along with an explanation of each.

Expand Down
15 changes: 15 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ func (resp *Response) InternalServerError(v interface{}) {
func (resp *Response) NotImplemented(v interface{}) {
resp.writeResponse(http.StatusNotImplemented, v)
}

// BadGateway returns a 502 Bad Gateway JSON response
func (resp *Response) BadGateway(v interface{}) {
resp.writeResponse(http.StatusBadGateway, v)
}

// ServiceUnavailable returns a 503 Service Unavailable JSON response
func (resp *Response) ServiceUnavailable(v interface{}) {
resp.writeResponse(http.StatusServiceUnavailable, v)
}

// GatewayTimeout returns a 504 Gateway Timeout JSON response
func (resp *Response) GatewayTimeout(v interface{}) {
resp.writeResponse(http.StatusGatewayTimeout, v)
}
52 changes: 52 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,55 @@ func TestNotImplemented(t *testing.T) {
t.Fatal(err.Error())
}
}

var testData = []struct {
testName string

inputHttpVerb string
inputHttpCode int
inputJsonData string

methodUnderTest func(r *resp.Response, v interface{})

expectedStatus int
expectedBody string
}{
{"502 Bad Gateway",
/*input*/ "POST", 502, "Bad Gateway - sample error content",
/*method under test*/ (*resp.Response).BadGateway,
/*expected*/ http.StatusBadGateway, `{"code":502,"message":"Bad Gateway - sample error content"}`},

{"503 Service Unavailable",
/*input*/ "POST", 503, "Service Unavailable - sample error content",
/*method under test*/ (*resp.Response).ServiceUnavailable,
/*expected*/ http.StatusServiceUnavailable, `{"code":503,"message":"Service Unavailable - sample error content"}`},
{"504 Gateway Timeout",
/*input*/ "POST", 504, "Gateway Timeout - sample error content",
/*method under test*/ (*resp.Response).GatewayTimeout,
/*expected*/ http.StatusGatewayTimeout, `{"code":504,"message":"Gateway Timeout - sample error content"}`},
}

func TestErrorResponses(t *testing.T) {
for _, datum := range testData {
t.Run(datum.testName, func(t *testing.T) {
// t.Parallel()
req := newRequest(t, datum.inputHttpVerb)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := resp.NewResponse(w)
datum.methodUnderTest(resp, &Error{datum.inputHttpCode, datum.inputJsonData})
})
handler.ServeHTTP(rr, req)

if err := validateStatusCode(rr.Code, datum.expectedStatus); err != nil {
t.Fatal(err.Error())
}

expected := datum.expectedBody
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
t.Fatal(err.Error())
}
})
}
}

0 comments on commit b55df1d

Please sign in to comment.