Improved compression test

This commit is contained in:
Eduard Urbach 2024-03-28 11:27:06 +01:00
parent 341bd08d1d
commit 245a085020
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -40,18 +40,20 @@ func TestResponseCompression(t *testing.T) {
s := web.NewServer()
uncompressed := bytes.Repeat([]byte("This text should be compressed to a size smaller than the original."), 5)
s.Get("/", func(ctx web.Context) error {
response := ctx.Response()
response.Write(uncompressed)
body := response.Body()
response.SetBody(nil)
writer := gzip.NewWriter(response)
defer writer.Close()
_, err := writer.Write(body)
s.Use(func(ctx web.Context) error {
err := ctx.Next()
body := ctx.Response().Body()
ctx.Response().SetBody(nil)
zip := gzip.NewWriter(ctx.Response())
zip.Write(body)
zip.Close()
return err
})
s.Get("/", func(ctx web.Context) error {
return ctx.Bytes(uncompressed)
})
response := s.Request("GET", "/", nil)
assert.Equal(t, response.Status(), 200)
assert.True(t, len(response.Body()) < len(uncompressed))