Skip to content

Commit

Permalink
test: refactor test suite for excluded paths and extensions
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Mar 23, 2024
1 parent d3b1ccd commit 0ae2960
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,38 @@ func TestGzipPNG(t *testing.T) {
assert.Equal(t, w.Body.String(), "this is a PNG!")
}

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

router := gin.New()
router.Use(Gzip(DefaultCompression, WithExcludedExtensions([]string{".html"})))
router.GET("/index.html", func(c *gin.Context) {
c.String(200, "this is a HTML!")
})

w := httptest.NewRecorder()
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, "this is a HTML!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
func TestExcludedPathsAndExtensions(t *testing.T) {
tests := []struct {
path string
option Option
expectedContentEncoding string
expectedVary string
expectedBody string
expectedContentLength string
}{
{"/api/books", WithExcludedPaths([]string{"/api/"}), "", "", "this is books!", ""},
{"/index.html", WithExcludedExtensions([]string{".html"}), "", "", "this is a HTML!", ""},
}

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

router := gin.New()
router.Use(Gzip(DefaultCompression, WithExcludedPaths([]string{"/api/"})))
router.GET("/api/books", func(c *gin.Context) {
c.String(200, "this is books!")
})
router := gin.New()
router.Use(Gzip(DefaultCompression, tt.option))
router.GET(tt.path, func(c *gin.Context) {
c.String(200, tt.expectedBody)
})

w := httptest.NewRecorder()
router.ServeHTTP(w, req)
w := httptest.NewRecorder()
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, "this is books!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
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.expectedBody, w.Body.String())
assert.Equal(t, tt.expectedContentLength, w.Header().Get("Content-Length"))
}
}

func TestNoGzip(t *testing.T) {
Expand Down

0 comments on commit 0ae2960

Please sign in to comment.