Skip to content

Commit ef7d482

Browse files
authored
test: refactor test suite for excluded paths and extensions (#80)
- Update test function name to `TestExcludedPathsAndExtensions` - Add test cases for excluded paths and extensions - Modify router usage in test function - Adjust assertions for content encoding, vary, body, and content length Signed-off-by: appleboy <[email protected]>
1 parent d3b1ccd commit ef7d482

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

gzip_test.go

+28-34
Original file line numberDiff line numberDiff line change
@@ -104,44 +104,38 @@ func TestGzipPNG(t *testing.T) {
104104
assert.Equal(t, w.Body.String(), "this is a PNG!")
105105
}
106106

107-
func TestExcludedExtensions(t *testing.T) {
108-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
109-
req.Header.Add("Accept-Encoding", "gzip")
110-
111-
router := gin.New()
112-
router.Use(Gzip(DefaultCompression, WithExcludedExtensions([]string{".html"})))
113-
router.GET("/index.html", func(c *gin.Context) {
114-
c.String(200, "this is a HTML!")
115-
})
116-
117-
w := httptest.NewRecorder()
118-
router.ServeHTTP(w, req)
119-
120-
assert.Equal(t, http.StatusOK, w.Code)
121-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
122-
assert.Equal(t, "", w.Header().Get("Vary"))
123-
assert.Equal(t, "this is a HTML!", w.Body.String())
124-
assert.Equal(t, "", w.Header().Get("Content-Length"))
125-
}
107+
func TestExcludedPathsAndExtensions(t *testing.T) {
108+
tests := []struct {
109+
path string
110+
option Option
111+
expectedContentEncoding string
112+
expectedVary string
113+
expectedBody string
114+
expectedContentLength string
115+
}{
116+
{"/api/books", WithExcludedPaths([]string{"/api/"}), "", "", "this is books!", ""},
117+
{"/index.html", WithExcludedExtensions([]string{".html"}), "", "", "this is a HTML!", ""},
118+
}
126119

127-
func TestExcludedPaths(t *testing.T) {
128-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/api/books", nil)
129-
req.Header.Add("Accept-Encoding", "gzip")
120+
for _, tt := range tests {
121+
req, _ := http.NewRequestWithContext(context.Background(), "GET", tt.path, nil)
122+
req.Header.Add("Accept-Encoding", "gzip")
130123

131-
router := gin.New()
132-
router.Use(Gzip(DefaultCompression, WithExcludedPaths([]string{"/api/"})))
133-
router.GET("/api/books", func(c *gin.Context) {
134-
c.String(200, "this is books!")
135-
})
124+
router := gin.New()
125+
router.Use(Gzip(DefaultCompression, tt.option))
126+
router.GET(tt.path, func(c *gin.Context) {
127+
c.String(200, tt.expectedBody)
128+
})
136129

137-
w := httptest.NewRecorder()
138-
router.ServeHTTP(w, req)
130+
w := httptest.NewRecorder()
131+
router.ServeHTTP(w, req)
139132

140-
assert.Equal(t, http.StatusOK, w.Code)
141-
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
142-
assert.Equal(t, "", w.Header().Get("Vary"))
143-
assert.Equal(t, "this is books!", w.Body.String())
144-
assert.Equal(t, "", w.Header().Get("Content-Length"))
133+
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"))
136+
assert.Equal(t, tt.expectedBody, w.Body.String())
137+
assert.Equal(t, tt.expectedContentLength, w.Header().Get("Content-Length"))
138+
}
145139
}
146140

147141
func TestNoGzip(t *testing.T) {

0 commit comments

Comments
 (0)