Skip to content

Commit

Permalink
#216 - Make linter happy + test case
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlOfSoup committed Jul 11, 2024
1 parent fd2cfe5 commit 7f3bf88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion status_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ type ErrorStatusHandler struct {
Component
}

// Handle generic error reponses.
// Handle generic error responses.
func (*ErrorStatusHandler) Handle(response *Response, _ *Request) {
message := map[string]string{
"error": http.StatusText(response.GetStatus()),
}
response.JSON(response.GetStatus(), message)
}

// RequestErrorStatusHandler a generic (error) status handler for requests.
type RequestErrorStatusHandler struct {
Component
}

// Handle generic request (error) responses.
func (h *RequestErrorStatusHandler) Handle(response *Response, request *Request) {
var errorMessages []string
var lang string
Expand Down
21 changes: 20 additions & 1 deletion status_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestValidationStatusHandler(t *testing.T) {
assert.Equal(t, "{\"error\":{\"body\":{\"fields\":{\"field\":{\"errors\":[\"The field is required\"]}},\"errors\":[\"The body is required\"]},\"query\":{\"fields\":{\"query\":{\"errors\":[\"The query is required\"]}}}}}\n", string(body))
}

func TestRequestErrorStatusHandler(t *testing.T) {
func TestRequestErrorStatusHandlerWithSliceOfErrors(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
handler := &RequestErrorStatusHandler{}
handler.Init(resp.server)
Expand All @@ -150,3 +150,22 @@ func TestRequestErrorStatusHandler(t *testing.T) {

assert.Equal(t, "{\"error\":[\"The request Content-Type indicates JSON, but the request body is empty or invalid\"]}\n", string(body))
}

func TestRequestErrorStatusHandlerWithString(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()

handler := &RequestErrorStatusHandler{}
handler.Init(resp.server)

req.Extra[ExtraRequestError{}] = "request.json-invalid-body"

handler.Handle(resp, req)

res := recorder.Result()
body, err := io.ReadAll(res.Body)

assert.NoError(t, res.Body.Close())
require.NoError(t, err)

assert.Equal(t, "{\"error\":[\"The request Content-Type indicates JSON, but the request body is empty or invalid\"]}\n", string(body))
}

0 comments on commit 7f3bf88

Please sign in to comment.