Skip to content

Commit

Permalink
fix: use Header().Add instead of Header() for setting Vary head…
Browse files Browse the repository at this point in the history
…er (#99)

- Modify middleware to add "Vary: Origin" header before applying gzip compression
- Add a new test to verify the "Vary" header is correctly set and gzip compression is applied
- Update handler to use `Header().Add` instead of `Header()` for setting "Vary" header

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy authored Jan 27, 2025
1 parent bc3ccd1 commit 3bd96db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
12 changes: 8 additions & 4 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import (

func main() {
r := gin.Default()
r.Use(gzip.Gzip(
gzip.DefaultCompression,
gzip.WithExcludedPaths([]string{"/ping2"}),
))
r.Use(
func(c *gin.Context) {
c.Writer.Header().Add("Vary", "Origin")
},
gzip.Gzip(
gzip.DefaultCompression,
gzip.WithExcludedPaths([]string{"/ping2"}),
))
r.Use(func(c *gin.Context) {
log.Println("Request received")
c.Next()
Expand Down
29 changes: 29 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,41 @@ func newServer() *gin.Engine {
c.Header("Content-Length", strconv.Itoa(len(testResponse)))
c.String(200, testResponse)
})
router.GET("/ping", func(c *gin.Context) {
c.Writer.Header().Add("Vary", "Origin")
}, func(c *gin.Context) {
c.Header("Content-Length", strconv.Itoa(len(testResponse)))
c.String(200, testResponse)
})
router.Any("/reverse", func(c *gin.Context) {
rp.ServeHTTP(c.Writer, c.Request)
})
return router
}

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

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

assert.Equal(t, 200, w.Code)
assert.Equal(t, "gzip", w.Header().Get(headerContentEncoding))
assert.Equal(t, []string{headerAcceptEncoding, "Origin"}, w.Header().Values(headerVary))
assert.NotEqual(t, "0", w.Header().Get("Content-Length"))
assert.NotEqual(t, 19, w.Body.Len())
assert.Equal(t, w.Header().Get("Content-Length"), fmt.Sprint(w.Body.Len()))

gr, err := gzip.NewReader(w.Body)
assert.NoError(t, err)
defer gr.Close()

body, _ := io.ReadAll(gr)
assert.Equal(t, testResponse, string(body))
}

func TestGzip(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
req.Header.Add(headerAcceptEncoding, "gzip")
Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (g *gzipHandler) Handle(c *gin.Context) {
gz.Reset(c.Writer)

c.Header(headerContentEncoding, "gzip")
c.Header(headerVary, headerAcceptEncoding)
c.Writer.Header().Add(headerVary, headerAcceptEncoding)
c.Writer = &gzipWriter{c.Writer, gz}
defer func() {
if c.Writer.Size() < 0 {
Expand Down

0 comments on commit 3bd96db

Please sign in to comment.