Skip to content

Commit

Permalink
Merge 114826c into 3bbdd46
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jul 22, 2019
2 parents 3bbdd46 + 114826c commit d020dd4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
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
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
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

0 comments on commit d020dd4

Please sign in to comment.