Skip to content

Commit

Permalink
Merge pull request #12 from georgizhivankin/410-gone
Browse files Browse the repository at this point in the history
Add a method for the 410 Gone HTTP code
  • Loading branch information
nicklaw5 committed Sep 24, 2021
2 parents 5972f8a + b67d536 commit 82f97ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {
| 405 | MethodNotAllowed() |
| 406 | NotAcceptable() |
| 409 | Conflict() |
| 410 | Gone() |
| 411 | LengthRequired() |
| 412 | PreconditionFailed() |
| 413 | RequestEntityTooLarge() |
Expand Down
7 changes: 6 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (resp *Response) Unauthorized(v interface{}) {
resp.writeResponse(http.StatusUnauthorized, v)
}

// Forbidden returns a 401 Forbidden JSON response
// Forbidden returns a 403 Forbidden JSON response
func (resp *Response) Forbidden(v interface{}) {
resp.writeResponse(http.StatusForbidden, v)
}
Expand All @@ -37,6 +37,11 @@ func (resp *Response) Conflict(v interface{}) {
resp.writeResponse(http.StatusConflict, v)
}

// Gone returns a 410 Gone JSON response
func (resp *Response) Gone(v interface{}) {
resp.writeResponse(http.StatusGone, v)
}

// LengthRequired returns a 411 Length Required JSON response
func (resp *Response) LengthRequired(v interface{}) {
resp.writeResponse(http.StatusLengthRequired, v)
Expand Down
22 changes: 22 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ func TestConflict(t *testing.T) {
}
}

func TestGone(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.Gone(&Error{410, "Service gone"})
})
handler.ServeHTTP(rr, req)

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

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

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

Expand Down

0 comments on commit 82f97ac

Please sign in to comment.