From a77d44bfacda4eca5fa6c937254ec3b13b07caa1 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Mon, 19 Feb 2024 11:26:07 +0800 Subject: [PATCH] test compression --- .../site/middleware/compression_test.go | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/handler/site/middleware/compression_test.go b/internal/handler/site/middleware/compression_test.go index 6569c78..2188f7a 100644 --- a/internal/handler/site/middleware/compression_test.go +++ b/internal/handler/site/middleware/compression_test.go @@ -8,11 +8,14 @@ import ( "net/http/httptest" "testing" "time" + "strings" + "compress/gzip" "github.com/oursky/pageship/internal/config" "github.com/oursky/pageship/internal/handler/site/middleware" "github.com/oursky/pageship/internal/site" "github.com/stretchr/testify/assert" + "github.com/andybalholm/brotli" ) type mockHandler struct { @@ -67,7 +70,15 @@ func TestCache(t *testing.T) { rec := httptest.NewRecorder() rec.Header().Add("Content-Type", "text/plain") h.ServeHTTP(rec, req) - assert.Equal(t, "gzip", rec.Result().Header.Get("Content-Encoding")) + resp := rec.Result() + assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) + gzreader, err := gzip.NewReader(resp.Body) + defer gzreader.Close() + buf := new(strings.Builder) + n, err := io.Copy(buf, gzreader) + assert.Empty(t, err) + assert.Equal(t, int64(5), n) + assert.Equal(t, "hello", buf.String()) req, err = http.NewRequest("GET", "endpoint", bytes.NewBuffer([]byte("body"))) assert.Empty(t, err) @@ -75,5 +86,12 @@ func TestCache(t *testing.T) { rec = httptest.NewRecorder() rec.Header().Add("Content-Type", "text/plain") h.ServeHTTP(rec, req) - assert.Equal(t, "br", rec.Result().Header.Get("Content-Encoding")) + resp = rec.Result() + assert.Equal(t, "br", resp.Header.Get("Content-Encoding")) + brreader := brotli.NewReader(resp.Body) + buf = new(strings.Builder) + n, err = io.Copy(buf, brreader) + assert.Empty(t, err) + assert.Equal(t, int64(5), n) + assert.Equal(t, "hello", buf.String()) }