Skip to content

Commit

Permalink
refactor: refactor header handling with constants in handler module
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Jan 12, 2025
1 parent 53b6bc1 commit bc23737
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
62 changes: 31 additions & 31 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func newServer() *gin.Engine {

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

w := httptest.NewRecorder()
r := newServer()
r.ServeHTTP(w, req)

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

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

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

assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Header().Get("Vary"), "")
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
assert.Equal(t, w.Header().Get(headerVary), "")
assert.Equal(t, w.Body.String(), "this is a PNG!")
}

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

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

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

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

assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
assert.Equal(t, w.Header().Get("Content-Length"), "19")
assert.Equal(t, w.Body.String(), testResponse)
}

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

w := newCloseNotifyingRecorder()
r := newServer()
r.ServeHTTP(w, req)

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

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

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

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
assert.Equal(t, "", w.Header().Get(headerVary))
assert.Equal(t, testResponse, w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}

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

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

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
assert.Equal(t, "", w.Header().Get(headerVary))
assert.Equal(t, "ok", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}

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

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

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

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

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
assert.Equal(t, "", w.Header().Get(headerVary))
assert.Equal(t, testResponse, w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
Expand All @@ -296,13 +296,13 @@ func TestGzipWithDecompressOnly(t *testing.T) {
gz.Close()

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

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

assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Header().Get(headerContentEncoding), "")
assert.Equal(t, w.Body.String(), testResponse)
}

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

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

assert.Equal(t, 200, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get(headerContentEncoding))
assert.Equal(t, "19", w.Header().Get("Content-Length"))
assert.Equal(t, testResponse, w.Body.String())
}
10 changes: 8 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/gin-gonic/gin"
)

const (
headerAcceptEncoding = "Accept-Encoding"
headerContentEncoding = "Content-Encoding"
headerVary = "Vary"
)

type gzipHandler struct {
*config
gzPool sync.Pool
Expand Down Expand Up @@ -65,8 +71,8 @@ func (g *gzipHandler) Handle(c *gin.Context) {
defer gz.Reset(io.Discard)
gz.Reset(c.Writer)

c.Header("Content-Encoding", "gzip")
c.Header("Vary", "Accept-Encoding")
c.Header(headerContentEncoding, "gzip")
c.Header(headerVary, headerAcceptEncoding)
c.Writer = &gzipWriter{c.Writer, gz}
defer func() {
if c.Writer.Size() < 0 {
Expand Down
2 changes: 1 addition & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestHandleGzip(t *testing.T) {
})

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

w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expand Down

0 comments on commit bc23737

Please sign in to comment.