Skip to content

Commit

Permalink
feat: clear some header field in error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 7, 2019
1 parent 29d0031 commit 4d21197
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
10 changes: 10 additions & 0 deletions cod.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,20 @@ func (d *Cod) NotFound(resp http.ResponseWriter, req *http.Request) {

// Error error handle
func (d *Cod) Error(c *Context, err error) {
// 出错时清除部分响应头
for _, key := range []string{
HeaderETag,
HeaderLastModified,
HeaderContentEncoding,
HeaderContentLength,
} {
c.SetHeader(key, "")
}
if d.ErrorHandler != nil {
d.ErrorHandler(c, err)
return
}

resp := c.Response
he, ok := err.(*hes.Error)
if ok {
Expand Down
55 changes: 42 additions & 13 deletions cod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cod

import (
"bytes"
"errors"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -336,21 +337,49 @@ func TestHandle(t *testing.T) {
}

func TestErrorHandler(t *testing.T) {
d := New()
d.GET("/", func(c *Context) error {
return hes.New("abc")
t.Run("remove header", func(t *testing.T) {
d := New()
resp := httptest.NewRecorder()
c := NewContext(resp, nil)
keys := []string{
HeaderETag,
HeaderLastModified,
HeaderContentEncoding,
HeaderContentLength,
}
for _, key := range keys {
c.SetHeader(key, "a")
}
d.Error(c, errors.New("abcd"))
for _, key := range keys {
value := c.GetHeader(key)
if value != "" {
t.Fatalf("default error handler should remove some header files")
}
}
if resp.Code != http.StatusInternalServerError ||
resp.Body.String() != "abcd" {
t.Fatalf("error response fail")
}
})

done := false
d.ErrorHandler = func(c *Context, err error) {
done = true
}
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
if !done {
t.Fatalf("custom error handler is not called")
}
t.Run("custom error handler", func(t *testing.T) {
d := New()
d.GET("/", func(c *Context) error {
return hes.New("abc")
})

done := false
d.ErrorHandler = func(c *Context, err error) {
done = true
}
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
if !done {
t.Fatalf("custom error handler is not called")
}
})
}

func TestNotFoundHandler(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions df.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const (
HeaderLastModified = "Last-Modified"
// HeaderContentEncoding content encoding
HeaderContentEncoding = "Content-Encoding"
// HeaderContentLength content length
HeaderContentLength = "Content-Length"
// HeaderIfModifiedSince if modified since
HeaderIfModifiedSince = "If-Modified-Since"
// HeaderIfNoneMatch if none match
Expand Down

0 comments on commit 4d21197

Please sign in to comment.