mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Add the ability to change the status code using Response beforeFuncs
This commit is contained in:
parent
b90e4e8ea1
commit
0406abe066
@ -56,11 +56,11 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
r.echo.Logger.Warn("response already committed")
|
r.echo.Logger.Warn("response already committed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
r.Status = code
|
||||||
for _, fn := range r.beforeFuncs {
|
for _, fn := range r.beforeFuncs {
|
||||||
fn()
|
fn()
|
||||||
}
|
}
|
||||||
r.Status = code
|
r.Writer.WriteHeader(r.Status)
|
||||||
r.Writer.WriteHeader(code)
|
|
||||||
r.Committed = true
|
r.Committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,3 +56,19 @@ func TestResponse_Flush(t *testing.T) {
|
|||||||
res.Flush()
|
res.Flush()
|
||||||
assert.True(t, rec.Flushed)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user