Skip to content

Commit

Permalink
Merge pull request #11 from georgizhivankin/406-not-acceptable
Browse files Browse the repository at this point in the history
Add a method for the 406 Not Acceptable HTTP code
  • Loading branch information
nicklaw5 authored Sep 1, 2021
2 parents 6e1c899 + 2040669 commit 5972f8a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func main() {
| 403 | Forbidden() |
| 404 | NotFound() |
| 405 | MethodNotAllowed() |
| 406 | NotAcceptable() |
| 409 | Conflict() |
| 411 | LengthRequired() |
| 412 | PreconditionFailed() |
Expand Down
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func (resp *Response) MethodNotAllowed(v interface{}) {
resp.writeResponse(http.StatusMethodNotAllowed, v)
}

// NotAcceptable returns a 406 Not Acceptable JSON response
func (resp *Response) NotAcceptable(v interface{}) {
resp.writeResponse(http.StatusNotAcceptable, v)
}

// Conflict returns a 409 Conflict JSON response
func (resp *Response) Conflict(v interface{}) {
resp.writeResponse(http.StatusConflict, v)
Expand Down
22 changes: 22 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ func TestMethodNotAllowed(t *testing.T) {
}
}

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

req := newRequest(t, "GET")

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
NewResponse(w).
NotAcceptable(&Error{406, "Not acceptable"})
})
handler.ServeHTTP(rr, req)

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

expected := `{"code":406,"message":"Not acceptable"}`
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
t.Fatal(err.Error())
}
}

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

Expand Down

0 comments on commit 5972f8a

Please sign in to comment.