Added compression test

This commit is contained in:
Eduard Urbach 2024-03-28 11:22:52 +01:00
parent 8969c32887
commit 341bd08d1d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -1,6 +1,8 @@
package web_test package web_test
import ( import (
"bytes"
"compress/gzip"
"io" "io"
"testing" "testing"
@ -34,6 +36,34 @@ func TestWriteString(t *testing.T) {
assert.Equal(t, string(response.Body()), "Hello") assert.Equal(t, string(response.Body()), "Hello")
} }
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)
return err
})
response := s.Request("GET", "/", nil)
assert.Equal(t, response.Status(), 200)
assert.True(t, len(response.Body()) < len(uncompressed))
reader, err := gzip.NewReader(bytes.NewReader(response.Body()))
assert.Nil(t, err)
decompressed, err := io.ReadAll(reader)
assert.Nil(t, err)
assert.DeepEqual(t, decompressed, uncompressed)
}
func TestResponseHeader(t *testing.T) { func TestResponseHeader(t *testing.T) {
s := web.NewServer() s := web.NewServer()