Skip to content

Commit

Permalink
test: add gzip compression and decompression tests
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
appleboy committed Dec 24, 2024
1 parent fb15f54 commit 33b88bc
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package gzip

import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

func TestHandleGzip(t *testing.T) {
gin.SetMode(gin.TestMode)

tests := []struct {
name string
path string
acceptEncoding string
expectedContentEncoding string
expectedBody string
}{
{
name: "Gzip compression",
path: "/",
acceptEncoding: "gzip",
expectedContentEncoding: "gzip",
expectedBody: "Gzip Test Response",
},
{
name: "No compression",
path: "/",
acceptEncoding: "",
expectedContentEncoding: "",
expectedBody: "Gzip Test Response",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := gin.New()
router.Use(Gzip(DefaultCompression))
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Gzip Test Response")
})

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

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

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, tt.expectedContentEncoding, w.Header().Get("Content-Encoding"))

if tt.expectedContentEncoding == "gzip" {
gr, err := gzip.NewReader(w.Body)
assert.NoError(t, err)
defer gr.Close()

body, _ := io.ReadAll(gr)
assert.Equal(t, tt.expectedBody, string(body))
} else {
assert.Equal(t, tt.expectedBody, w.Body.String())
}
})
}
}

func TestHandleDecompressGzip(t *testing.T) {
gin.SetMode(gin.TestMode)

buf := &bytes.Buffer{}
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
if _, err := gz.Write([]byte("Gzip Test Response")); err != nil {
gz.Close()
t.Fatal(err)
}
gz.Close()

router := gin.New()
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
data, err := c.GetRawData()
assert.NoError(t, err)
assert.Equal(t, "Gzip Test Response", string(data))
c.String(http.StatusOK, "ok")
})

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

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

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "ok", w.Body.String())
}

0 comments on commit 33b88bc

Please sign in to comment.