Skip to content

Commit 54f5cd3

Browse files
authored
Merge pull request #7 from ccamel/more_4xx_methods
Add support for HTTP statuses 413 and 415
2 parents 3bbdd46 + 114826c commit 54f5cd3

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func main() {
6262
| 409 | Conflict() |
6363
| 411 | LengthRequired() |
6464
| 412 | PreconditionFailed() |
65+
| 413 | RequestEntityTooLarge() |
66+
| 415 | UnsupportedMediaType() |
6567
| 422 | UnprocessableEntity() |
6668
| 500 | InternalServerError() |
6769
| 501 | NotImplemented() |

error.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func (resp *Response) PreconditionFailed(v interface{}) {
4242
resp.writeResponse(http.StatusPreconditionFailed, v)
4343
}
4444

45+
// RequestEntityTooLarge returns a 413 Request Entity Too Large JSON response
46+
func (resp *Response) RequestEntityTooLarge(v interface{}) {
47+
resp.writeResponse(http.StatusRequestEntityTooLarge, v)
48+
}
49+
50+
// UnsupportedMediaType returns a 415 Unsupported Media Type JSON response
51+
func (resp *Response) UnsupportedMediaType(v interface{}) {
52+
resp.writeResponse(http.StatusUnsupportedMediaType, v)
53+
}
54+
4555
// UnprocessableEntity returns a 422 Unprocessable Entity JSON response
4656
func (resp *Response) UnprocessableEntity(v interface{}) {
4757
resp.writeResponse(http.StatusUnprocessableEntity, v)

error_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,50 @@ func TestPreconditionFailed(t *testing.T) {
187187
}
188188
}
189189

190+
func TestRequestEntityTooLarge(t *testing.T) {
191+
t.Parallel()
192+
193+
req := newRequest(t, "POST")
194+
195+
rr := httptest.NewRecorder()
196+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
197+
res := NewResponse(w)
198+
res.RequestEntityTooLarge(&Error{413, "Payload too large"})
199+
})
200+
handler.ServeHTTP(rr, req)
201+
202+
if err := validateStatusCode(rr.Code, http.StatusRequestEntityTooLarge); err != nil {
203+
t.Fatal(err.Error())
204+
}
205+
206+
expected := `{"code":413,"message":"Payload too large"}`
207+
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
208+
t.Fatal(err.Error())
209+
}
210+
}
211+
212+
func TestUnsupportedMediaType(t *testing.T) {
213+
t.Parallel()
214+
215+
req := newRequest(t, "POST")
216+
217+
rr := httptest.NewRecorder()
218+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
219+
res := NewResponse(w)
220+
res.UnsupportedMediaType(&Error{415, "Unsupported Media Type"})
221+
})
222+
handler.ServeHTTP(rr, req)
223+
224+
if err := validateStatusCode(rr.Code, http.StatusUnsupportedMediaType); err != nil {
225+
t.Fatal(err.Error())
226+
}
227+
228+
expected := `{"code":415,"message":"Unsupported Media Type"}`
229+
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
230+
t.Fatal(err.Error())
231+
}
232+
}
233+
190234
func TestUnprocessableEntity(t *testing.T) {
191235
t.Parallel()
192236

0 commit comments

Comments
 (0)