-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |