Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func main() {
| 409 | Conflict() |
| 411 | LengthRequired() |
| 412 | PreconditionFailed() |
| 413 | RequestEntityTooLarge() |
| 415 | UnsupportedMediaType() |
| 422 | UnprocessableEntity() |
| 500 | InternalServerError() |
| 501 | NotImplemented() |
Expand Down
10 changes: 10 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (resp *Response) PreconditionFailed(v interface{}) {
resp.writeResponse(http.StatusPreconditionFailed, v)
}

// RequestEntityTooLarge returns a 413 Request Entity Too Large JSON response
func (resp *Response) RequestEntityTooLarge(v interface{}) {
resp.writeResponse(http.StatusRequestEntityTooLarge, v)
}

// UnsupportedMediaType returns a 415 Unsupported Media Type JSON response
func (resp *Response) UnsupportedMediaType(v interface{}) {
resp.writeResponse(http.StatusUnsupportedMediaType, v)
}

// UnprocessableEntity returns a 422 Unprocessable Entity JSON response
func (resp *Response) UnprocessableEntity(v interface{}) {
resp.writeResponse(http.StatusUnprocessableEntity, v)
Expand Down
44 changes: 44 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,50 @@ func TestPreconditionFailed(t *testing.T) {
}
}

func TestRequestEntityTooLarge(t *testing.T) {
t.Parallel()

req := newRequest(t, "POST")

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w)
res.RequestEntityTooLarge(&Error{413, "Payload too large"})
})
handler.ServeHTTP(rr, req)

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

expected := `{"code":413,"message":"Payload too large"}`
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
t.Fatal(err.Error())
}
}

func TestUnsupportedMediaType(t *testing.T) {
t.Parallel()

req := newRequest(t, "POST")

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w)
res.UnsupportedMediaType(&Error{415, "Unsupported Media Type"})
})
handler.ServeHTTP(rr, req)

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

expected := `{"code":415,"message":"Unsupported Media Type"}`
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
t.Fatal(err.Error())
}
}

func TestUnprocessableEntity(t *testing.T) {
t.Parallel()

Expand Down