From 341bd08d1d9971eba2a9ecf76160616166252c01 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 28 Mar 2024 11:22:52 +0100 Subject: [PATCH] Added compression test --- Response_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Response_test.go b/Response_test.go index 256bc32..22a7c3b 100644 --- a/Response_test.go +++ b/Response_test.go @@ -1,6 +1,8 @@ package web_test import ( + "bytes" + "compress/gzip" "io" "testing" @@ -34,6 +36,34 @@ func TestWriteString(t *testing.T) { 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) { s := web.NewServer()