From ef1bde78cf7040d973580bda7d24f34815689614 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Mon, 19 Feb 2024 14:20:27 +0800 Subject: [PATCH] Clean up code --- .../handler/site/middleware/compression.go | 6 +- .../site/middleware/compression_test.go | 84 +++++++------------ .../handler/site/middleware/middleware.go | 2 +- 3 files changed, 34 insertions(+), 58 deletions(-) diff --git a/internal/handler/site/middleware/compression.go b/internal/handler/site/middleware/compression.go index f2fff71..ef3fe97 100644 --- a/internal/handler/site/middleware/compression.go +++ b/internal/handler/site/middleware/compression.go @@ -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) +} diff --git a/internal/handler/site/middleware/compression_test.go b/internal/handler/site/middleware/compression_test.go index 2188f7a..6b06fb6 100644 --- a/internal/handler/site/middleware/compression_test.go +++ b/internal/handler/site/middleware/compression_test.go @@ -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" ) @@ -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)) } diff --git a/internal/handler/site/middleware/middleware.go b/internal/handler/site/middleware/middleware.go index 7b894c1..80e434d 100644 --- a/internal/handler/site/middleware/middleware.go +++ b/internal/handler/site/middleware/middleware.go @@ -9,5 +9,5 @@ var Default = []site.Middleware{ CanonicalizePath, RouteSPA, IndexPage, - Compression, + compression, }