Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
fix: remove unused field from response's header
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 8, 2019
1 parent f8c61fa commit ebe7689
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/vicanso/cod-recover

require (
github.com/vicanso/cod v0.0.2
github.com/vicanso/hes v0.1.3
github.com/vicanso/cod v0.0.6
github.com/vicanso/hes v0.1.4
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/vicanso/cod v0.0.2 h1:bTGMY9Qp4R+rsgFXXSexmt3GSAcxL/zyxRhRfaSlRcQ=
github.com/vicanso/cod v0.0.2/go.mod h1:m+avmqlCzw472clYqSNtyNNm+BO3DW9OJCek23kSUUE=
github.com/vicanso/cod v0.0.6 h1:T5lLjAoCPPw5GnxbmqfuU7TD5pMY+JEJkOx2gvXiK90=
github.com/vicanso/cod v0.0.6/go.mod h1:m+avmqlCzw472clYqSNtyNNm+BO3DW9OJCek23kSUUE=
github.com/vicanso/hes v0.1.3 h1:3txPJL+J+xBRhIotkK3XKNbmBz+JXJ1knmGM0L+Qe4k=
github.com/vicanso/hes v0.1.3/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo=
github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o=
github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo=
github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM=
github.com/vicanso/keygrip v0.1.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho=
9 changes: 9 additions & 0 deletions recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func New() cod.Handler {
err = he
}
c.Cod(nil).EmitError(c, err)
// 出错时清除部分响应头
for _, key := range []string{
cod.HeaderETag,
cod.HeaderLastModified,
cod.HeaderContentEncoding,
cod.HeaderContentLength,
} {
c.SetHeader(key, "")
}
// 如果已直接对Response写入数据,则将 Committed设置为 true
c.Committed = true
resp := c.Response
Expand Down
14 changes: 14 additions & 0 deletions recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func TestRecover(t *testing.T) {
})
req := httptest.NewRequest("GET", "https://aslant.site/", nil)
resp := httptest.NewRecorder()
keys := []string{
cod.HeaderETag,
cod.HeaderLastModified,
cod.HeaderContentEncoding,
cod.HeaderContentLength,
}
for _, key := range keys {
resp.Header().Set(key, "a")
}

catchError := false
d.OnError(func(_ *cod.Context, _ error) {
Expand All @@ -31,4 +40,9 @@ func TestRecover(t *testing.T) {
!catchError {
t.Fatalf("recover fail")
}
for _, key := range keys {
if resp.Header().Get(key) != "" {
t.Fatalf("reset response header fail")
}
}
}

0 comments on commit ebe7689

Please sign in to comment.