Skip to content

Commit bc23737

Browse files
committed
refactor: refactor header handling with constants in handler module
- Replace hardcoded header strings with constants for `Accept-Encoding`, `Content-Encoding`, and `Vary` - Add constants for `Accept-Encoding`, `Content-Encoding`, and `Vary` headers in `handler.go` Signed-off-by: appleboy <[email protected]>
1 parent 53b6bc1 commit bc23737

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

gzip_test.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func newServer() *gin.Engine {
6464

6565
func TestGzip(t *testing.T) {
6666
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
67-
req.Header.Add("Accept-Encoding", "gzip")
67+
req.Header.Add(headerAcceptEncoding, "gzip")
6868

6969
w := httptest.NewRecorder()
7070
r := newServer()
7171
r.ServeHTTP(w, req)
7272

7373
assert.Equal(t, w.Code, 200)
74-
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
75-
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
74+
assert.Equal(t, w.Header().Get(headerContentEncoding), "gzip")
75+
assert.Equal(t, w.Header().Get(headerVary), headerAcceptEncoding)
7676
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
7777
assert.NotEqual(t, w.Body.Len(), 19)
7878
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
@@ -87,7 +87,7 @@ func TestGzip(t *testing.T) {
8787

8888
func TestGzipPNG(t *testing.T) {
8989
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/image.png", nil)
90-
req.Header.Add("Accept-Encoding", "gzip")
90+
req.Header.Add(headerAcceptEncoding, "gzip")
9191

9292
router := gin.New()
9393
router.Use(Gzip(DefaultCompression))
@@ -99,8 +99,8 @@ func TestGzipPNG(t *testing.T) {
9999
router.ServeHTTP(w, req)
100100

101101
assert.Equal(t, w.Code, 200)
102-
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
103-
assert.Equal(t, w.Header().Get("Vary"), "")
102+
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
103+
assert.Equal(t, w.Header().Get(headerVary), "")
104104
assert.Equal(t, w.Body.String(), "this is a PNG!")
105105
}
106106

@@ -119,7 +119,7 @@ func TestExcludedPathsAndExtensions(t *testing.T) {
119119

120120
for _, tt := range tests {
121121
req, _ := http.NewRequestWithContext(context.Background(), "GET", tt.path, nil)
122-
req.Header.Add("Accept-Encoding", "gzip")
122+
req.Header.Add(headerAcceptEncoding, "gzip")
123123

124124
router := gin.New()
125125
router.Use(Gzip(DefaultCompression, tt.option))
@@ -131,8 +131,8 @@ func TestExcludedPathsAndExtensions(t *testing.T) {
131131
router.ServeHTTP(w, req)
132132

133133
assert.Equal(t, http.StatusOK, w.Code)
134-
assert.Equal(t, tt.expectedContentEncoding, w.Header().Get("Content-Encoding"))
135-
assert.Equal(t, tt.expectedVary, w.Header().Get("Vary"))
134+
assert.Equal(t, tt.expectedContentEncoding, w.Header().Get(headerContentEncoding))
135+
assert.Equal(t, tt.expectedVary, w.Header().Get(headerVary))
136136
assert.Equal(t, tt.expectedBody, w.Body.String())
137137
assert.Equal(t, tt.expectedContentLength, w.Header().Get("Content-Length"))
138138
}
@@ -146,22 +146,22 @@ func TestNoGzip(t *testing.T) {
146146
r.ServeHTTP(w, req)
147147

148148
assert.Equal(t, w.Code, 200)
149-
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
149+
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
150150
assert.Equal(t, w.Header().Get("Content-Length"), "19")
151151
assert.Equal(t, w.Body.String(), testResponse)
152152
}
153153

154154
func TestGzipWithReverseProxy(t *testing.T) {
155155
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/reverse", nil)
156-
req.Header.Add("Accept-Encoding", "gzip")
156+
req.Header.Add(headerAcceptEncoding, "gzip")
157157

158158
w := newCloseNotifyingRecorder()
159159
r := newServer()
160160
r.ServeHTTP(w, req)
161161

162162
assert.Equal(t, w.Code, 200)
163-
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
164-
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
163+
assert.Equal(t, w.Header().Get(headerContentEncoding), "gzip")
164+
assert.Equal(t, w.Header().Get(headerVary), headerAcceptEncoding)
165165
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
166166
assert.NotEqual(t, w.Body.Len(), 19)
167167
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
@@ -184,12 +184,12 @@ func TestDecompressGzip(t *testing.T) {
184184
gz.Close()
185185

186186
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
187-
req.Header.Add("Content-Encoding", "gzip")
187+
req.Header.Add(headerContentEncoding, "gzip")
188188

189189
router := gin.New()
190190
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
191191
router.POST("/", func(c *gin.Context) {
192-
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
192+
if v := c.Request.Header.Get(headerContentEncoding); v != "" {
193193
t.Errorf("unexpected `Content-Encoding`: %s header", v)
194194
}
195195
if v := c.Request.Header.Get("Content-Length"); v != "" {
@@ -206,15 +206,15 @@ func TestDecompressGzip(t *testing.T) {
206206
router.ServeHTTP(w, req)
207207

208208
assert.Equal(t, http.StatusOK, w.Code)
209-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
210-
assert.Equal(t, "", w.Header().Get("Vary"))
209+
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
210+
assert.Equal(t, "", w.Header().Get(headerVary))
211211
assert.Equal(t, testResponse, w.Body.String())
212212
assert.Equal(t, "", w.Header().Get("Content-Length"))
213213
}
214214

215215
func TestDecompressGzipWithEmptyBody(t *testing.T) {
216216
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", nil)
217-
req.Header.Add("Content-Encoding", "gzip")
217+
req.Header.Add(headerContentEncoding, "gzip")
218218

219219
router := gin.New()
220220
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
@@ -226,15 +226,15 @@ func TestDecompressGzipWithEmptyBody(t *testing.T) {
226226
router.ServeHTTP(w, req)
227227

228228
assert.Equal(t, http.StatusOK, w.Code)
229-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
230-
assert.Equal(t, "", w.Header().Get("Vary"))
229+
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
230+
assert.Equal(t, "", w.Header().Get(headerVary))
231231
assert.Equal(t, "ok", w.Body.String())
232232
assert.Equal(t, "", w.Header().Get("Content-Length"))
233233
}
234234

235235
func TestDecompressGzipWithIncorrectData(t *testing.T) {
236236
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", bytes.NewReader([]byte(testResponse)))
237-
req.Header.Add("Content-Encoding", "gzip")
237+
req.Header.Add(headerContentEncoding, "gzip")
238238

239239
router := gin.New()
240240
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
@@ -258,12 +258,12 @@ func TestDecompressOnly(t *testing.T) {
258258
gz.Close()
259259

260260
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
261-
req.Header.Add("Content-Encoding", "gzip")
261+
req.Header.Add(headerContentEncoding, "gzip")
262262

263263
router := gin.New()
264264
router.Use(Gzip(NoCompression, WithDecompressOnly(), WithDecompressFn(DefaultDecompressHandle)))
265265
router.POST("/", func(c *gin.Context) {
266-
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
266+
if v := c.Request.Header.Get(headerContentEncoding); v != "" {
267267
t.Errorf("unexpected `Content-Encoding`: %s header", v)
268268
}
269269
if v := c.Request.Header.Get("Content-Length"); v != "" {
@@ -280,8 +280,8 @@ func TestDecompressOnly(t *testing.T) {
280280
router.ServeHTTP(w, req)
281281

282282
assert.Equal(t, http.StatusOK, w.Code)
283-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
284-
assert.Equal(t, "", w.Header().Get("Vary"))
283+
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
284+
assert.Equal(t, "", w.Header().Get(headerVary))
285285
assert.Equal(t, testResponse, w.Body.String())
286286
assert.Equal(t, "", w.Header().Get("Content-Length"))
287287
}
@@ -296,13 +296,13 @@ func TestGzipWithDecompressOnly(t *testing.T) {
296296
gz.Close()
297297

298298
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
299-
req.Header.Add("Content-Encoding", "gzip")
300-
req.Header.Add("Accept-Encoding", "gzip")
299+
req.Header.Add(headerContentEncoding, "gzip")
300+
req.Header.Add(headerAcceptEncoding, "gzip")
301301

302302
r := gin.New()
303303
r.Use(Gzip(NoCompression, WithDecompressOnly(), WithDecompressFn(DefaultDecompressHandle)))
304304
r.POST("/", func(c *gin.Context) {
305-
assert.Equal(t, c.Request.Header.Get("Content-Encoding"), "")
305+
assert.Equal(t, c.Request.Header.Get(headerContentEncoding), "")
306306
assert.Equal(t, c.Request.Header.Get("Content-Length"), "")
307307
body, err := c.GetRawData()
308308
if err != nil {
@@ -316,13 +316,13 @@ func TestGzipWithDecompressOnly(t *testing.T) {
316316
r.ServeHTTP(w, req)
317317

318318
assert.Equal(t, w.Code, 200)
319-
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
319+
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
320320
assert.Equal(t, w.Body.String(), testResponse)
321321
}
322322

323323
func TestCustomShouldCompressFn(t *testing.T) {
324324
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
325-
req.Header.Add("Accept-Encoding", "gzip")
325+
req.Header.Add(headerAcceptEncoding, "gzip")
326326

327327
router := gin.New()
328328
router.Use(Gzip(
@@ -340,7 +340,7 @@ func TestCustomShouldCompressFn(t *testing.T) {
340340
router.ServeHTTP(w, req)
341341

342342
assert.Equal(t, 200, w.Code)
343-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
343+
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
344344
assert.Equal(t, "19", w.Header().Get("Content-Length"))
345345
assert.Equal(t, testResponse, w.Body.String())
346346
}

handler.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"github.com/gin-gonic/gin"
1313
)
1414

15+
const (
16+
headerAcceptEncoding = "Accept-Encoding"
17+
headerContentEncoding = "Content-Encoding"
18+
headerVary = "Vary"
19+
)
20+
1521
type gzipHandler struct {
1622
*config
1723
gzPool sync.Pool
@@ -65,8 +71,8 @@ func (g *gzipHandler) Handle(c *gin.Context) {
6571
defer gz.Reset(io.Discard)
6672
gz.Reset(c.Writer)
6773

68-
c.Header("Content-Encoding", "gzip")
69-
c.Header("Vary", "Accept-Encoding")
74+
c.Header(headerContentEncoding, "gzip")
75+
c.Header(headerVary, headerAcceptEncoding)
7076
c.Writer = &gzipWriter{c.Writer, gz}
7177
defer func() {
7278
if c.Writer.Size() < 0 {

handler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestHandleGzip(t *testing.T) {
4848
})
4949

5050
req, _ := http.NewRequestWithContext(context.Background(), "GET", tt.path, nil)
51-
req.Header.Set("Accept-Encoding", tt.acceptEncoding)
51+
req.Header.Set(headerAcceptEncoding, tt.acceptEncoding)
5252

5353
w := httptest.NewRecorder()
5454
router.ServeHTTP(w, req)

0 commit comments

Comments
 (0)