From ebe7689fdc21a3a32ee8836d538a450a5606bc22 Mon Sep 17 00:00:00 2001 From: vicanso Date: Mon, 8 Apr 2019 20:55:06 +0800 Subject: [PATCH] fix: remove unused field from response's header --- go.mod | 4 ++-- go.sum | 4 ++++ recover.go | 9 +++++++++ recover_test.go | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index dd3d77a..a5bbf1f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4b8ead7..d4b8bf2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/recover.go b/recover.go index 3d674d0..8c73e5f 100644 --- a/recover.go +++ b/recover.go @@ -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 diff --git a/recover_test.go b/recover_test.go index 5f1e929..29605e2 100644 --- a/recover_test.go +++ b/recover_test.go @@ -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) { @@ -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") + } + } }