Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 19, 2024
1 parent a77d44b commit ef1bde7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
6 changes: 5 additions & 1 deletion internal/handler/site/middleware/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (
"github.com/oursky/pageship/internal/site"
)

func Compression(site *site.Descriptor, next http.Handler) http.Handler {
func Compression(next http.Handler) http.Handler {
c := middleware.NewCompressor(5)
c.SetEncoder("br", func(w io.Writer, level int) io.Writer {
return brotli.NewWriterV2(w, level)
})
return c.Handler(next)
}

func compression(site *site.Descriptor, next http.Handler) http.Handler {
return Compression(next)
}
84 changes: 28 additions & 56 deletions internal/handler/site/middleware/compression_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package middleware_test //black box testing
package middleware_test

import (
"bytes"
"context"
"io"
"net/http"
"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"
)
Expand All @@ -22,76 +16,54 @@ type mockHandler struct {
executeCount int
}

func (mh *mockHandler) serve(w http.ResponseWriter, r *http.Request) {
func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mh.executeCount++
w.Header().Add("Content-Type", "text/plain")
w.Write([]byte("hello"))
}

type mockFS struct{}

func (m mockFS) Stat(path string) (*site.FileInfo, error) {
return &site.FileInfo{
IsDir: false,
ModTime: time.Now(),
Size: 0,
ContentType: "",
Hash: "",
}, nil
}

type mockRSCloser struct {
io.ReadSeeker
}

func (m mockRSCloser) Close() error {
return nil
}

func (m mockFS) Open(ctx context.Context, path string) (io.ReadSeekCloser, error) {
return mockRSCloser{bytes.NewReader([]byte{})}, nil
}

func TestCache(t *testing.T) {
func TestCacheGzip(t *testing.T) {
//Setup
mh := mockHandler{}
sc := config.DefaultSiteConfig()
mockSiteDescriptor := site.Descriptor{
ID: "",
Domain: "",
Config: &sc,
FS: mockFS{},
}
h := middleware.Compression(&mockSiteDescriptor, http.HandlerFunc(mh.serve))
h := middleware.Compression(new(mockHandler))

//Act Assert
req, err := http.NewRequest("GET", "endpoint", bytes.NewBuffer([]byte("body")))
req, err := http.NewRequest("GET", "endpoint", nil)
assert.Empty(t, err)
req.Header.Add("Accept-Encoding", "gzip")

rec := httptest.NewRecorder()
rec.Header().Add("Content-Type", "text/plain")
h.ServeHTTP(rec, req)

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)
b, err := io.ReadAll(gzreader)

assert.Empty(t, err)
assert.Equal(t, int64(5), n)
assert.Equal(t, "hello", buf.String())
assert.Equal(t, "hello", string(b))
}

func TestCacheBrotli(t *testing.T) {
//Setup
h := middleware.Compression(new(mockHandler))

req, err = http.NewRequest("GET", "endpoint", bytes.NewBuffer([]byte("body")))
//Act Assert
req, err := http.NewRequest("GET", "endpoint", nil)
assert.Empty(t, err)
req.Header.Add("Accept-Encoding", "br")
rec = httptest.NewRecorder()
rec.Header().Add("Content-Type", "text/plain")

rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
resp = rec.Result()

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)
defer resp.Body.Close()
b, err := io.ReadAll(brreader)

assert.Empty(t, err)
assert.Equal(t, int64(5), n)
assert.Equal(t, "hello", buf.String())
assert.Equal(t, "hello", string(b))
}
2 changes: 1 addition & 1 deletion internal/handler/site/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ var Default = []site.Middleware{
CanonicalizePath,
RouteSPA,
IndexPage,
Compression,
compression,
}

0 comments on commit ef1bde7

Please sign in to comment.