Skip to content

Commit

Permalink
test compression
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 19, 2024
1 parent 97de51a commit a77d44b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/handler/site/middleware/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,13 +70,28 @@ 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)
req.Header.Add("Accept-Encoding", "br")
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())
}

0 comments on commit a77d44b

Please sign in to comment.