Skip to content

Commit 33b88bc

Browse files
committed
test: add gzip compression and decompression tests
- Add tests for gzip compression and decompression using the Gin framework - Verify gzip content encoding and response body in tests - Ensure correct handling of gzip and non-gzip requests - Test decompression of gzip-encoded POST request data Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent fb15f54 commit 33b88bc

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

handler_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package gzip
2+
3+
import (
4+
"bytes"
5+
"compress/gzip"
6+
"context"
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
10+
"testing"
11+
12+
"github.com/gin-gonic/gin"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestHandleGzip(t *testing.T) {
17+
gin.SetMode(gin.TestMode)
18+
19+
tests := []struct {
20+
name string
21+
path string
22+
acceptEncoding string
23+
expectedContentEncoding string
24+
expectedBody string
25+
}{
26+
{
27+
name: "Gzip compression",
28+
path: "/",
29+
acceptEncoding: "gzip",
30+
expectedContentEncoding: "gzip",
31+
expectedBody: "Gzip Test Response",
32+
},
33+
{
34+
name: "No compression",
35+
path: "/",
36+
acceptEncoding: "",
37+
expectedContentEncoding: "",
38+
expectedBody: "Gzip Test Response",
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
router := gin.New()
45+
router.Use(Gzip(DefaultCompression))
46+
router.GET("/", func(c *gin.Context) {
47+
c.String(http.StatusOK, "Gzip Test Response")
48+
})
49+
50+
req, _ := http.NewRequestWithContext(context.Background(), "GET", tt.path, nil)
51+
req.Header.Set("Accept-Encoding", tt.acceptEncoding)
52+
53+
w := httptest.NewRecorder()
54+
router.ServeHTTP(w, req)
55+
56+
assert.Equal(t, http.StatusOK, w.Code)
57+
assert.Equal(t, tt.expectedContentEncoding, w.Header().Get("Content-Encoding"))
58+
59+
if tt.expectedContentEncoding == "gzip" {
60+
gr, err := gzip.NewReader(w.Body)
61+
assert.NoError(t, err)
62+
defer gr.Close()
63+
64+
body, _ := io.ReadAll(gr)
65+
assert.Equal(t, tt.expectedBody, string(body))
66+
} else {
67+
assert.Equal(t, tt.expectedBody, w.Body.String())
68+
}
69+
})
70+
}
71+
}
72+
73+
func TestHandleDecompressGzip(t *testing.T) {
74+
gin.SetMode(gin.TestMode)
75+
76+
buf := &bytes.Buffer{}
77+
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
78+
if _, err := gz.Write([]byte("Gzip Test Response")); err != nil {
79+
gz.Close()
80+
t.Fatal(err)
81+
}
82+
gz.Close()
83+
84+
router := gin.New()
85+
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
86+
router.POST("/", func(c *gin.Context) {
87+
data, err := c.GetRawData()
88+
assert.NoError(t, err)
89+
assert.Equal(t, "Gzip Test Response", string(data))
90+
c.String(http.StatusOK, "ok")
91+
})
92+
93+
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
94+
req.Header.Set("Content-Encoding", "gzip")
95+
96+
w := httptest.NewRecorder()
97+
router.ServeHTTP(w, req)
98+
99+
assert.Equal(t, http.StatusOK, w.Code)
100+
assert.Equal(t, "ok", w.Body.String())
101+
}

0 commit comments

Comments
 (0)