Skip to content

Commit

Permalink
Add the ability to change the status code using Response beforeFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
RashadAnsari committed Dec 6, 2020
1 parent b90e4e8 commit 0406abe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func (r *Response) WriteHeader(code int) {
r.echo.Logger.Warn("response already committed")
return
}
r.Status = code
for _, fn := range r.beforeFuncs {
fn()
}
r.Status = code
r.Writer.WriteHeader(code)
r.Writer.WriteHeader(r.Status)
r.Committed = true
}

Expand Down
16 changes: 16 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ func TestResponse_Flush(t *testing.T) {
res.Flush()
assert.True(t, rec.Flushed)
}

func TestResponse_ChangeStatusCodeBeforeWrite(t *testing.T) {
e := New()
rec := httptest.NewRecorder()
res := &Response{echo: e, Writer: rec}

res.Before(func() {
if 200 < res.Status && res.Status < 300 {
res.Status = 200
}
})

res.WriteHeader(209)

assert.Equal(t, http.StatusOK, rec.Code)
}

0 comments on commit 0406abe

Please sign in to comment.