From 0d6db9bcffa7db61e8da1a7c664de3b2f83c0a96 Mon Sep 17 00:00:00 2001 From: Chris Camel Date: Mon, 22 Jul 2019 15:58:00 +0200 Subject: [PATCH 1/3] Add 415 HTTP status code --- error.go | 5 +++++ error_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/error.go b/error.go index f5ec142..4259594 100644 --- a/error.go +++ b/error.go @@ -42,6 +42,11 @@ func (resp *Response) PreconditionFailed(v interface{}) { resp.writeResponse(http.StatusPreconditionFailed, 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) diff --git a/error_test.go b/error_test.go index 06dbe05..8d0cdfc 100644 --- a/error_test.go +++ b/error_test.go @@ -187,6 +187,28 @@ func TestPreconditionFailed(t *testing.T) { } } +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() From 6a21eab25b37c026cf25a2928105ef8d961521b3 Mon Sep 17 00:00:00 2001 From: Chris Camel Date: Mon, 22 Jul 2019 16:01:18 +0200 Subject: [PATCH 2/3] Add 413 HTTP status code --- error.go | 5 +++++ error_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/error.go b/error.go index 4259594..2f01203 100644 --- a/error.go +++ b/error.go @@ -42,6 +42,11 @@ 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) diff --git a/error_test.go b/error_test.go index 8d0cdfc..6b7a450 100644 --- a/error_test.go +++ b/error_test.go @@ -187,6 +187,28 @@ 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() From 114826cf27baea864dc69445d1c7ed047bb6d8a9 Mon Sep 17 00:00:00 2001 From: Chris Camel Date: Mon, 22 Jul 2019 16:02:59 +0200 Subject: [PATCH 3/3] Add mention to 413 & 415 HTTP status code --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dda5477..5ecb151 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ func main() { | 409 | Conflict() | | 411 | LengthRequired() | | 412 | PreconditionFailed() | +| 413 | RequestEntityTooLarge() | +| 415 | UnsupportedMediaType() | | 422 | UnprocessableEntity() | | 500 | InternalServerError() | | 501 | NotImplemented() |