diff --git a/Gopkg.lock b/Gopkg.lock index c5d1b8b8e..680d75b6c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -43,6 +43,12 @@ packages = ["."] revision = "d175f85701dfbf44cb0510114c9943e665e60907" +[[projects]] + branch = "master" + name = "github.com/gin-contrib/gzip" + packages = ["."] + revision = "ff223ab9f8e3e69d4eb333c90966e057c5a97873" + [[projects]] branch = "master" name = "github.com/gin-contrib/sse" @@ -172,6 +178,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "7f8053eae7793a1bf95a91da813f86e38bf12678ad493d01c3f25864249dd305" + inputs-digest = "32bf99922b6d83811ed209762176a18be21b3a747affed35d9b6a266d53434b6" solver-name = "gps-cdcl" solver-version = 1 diff --git a/main.go b/main.go index 52da73413..216c677da 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/cloudflare/unsee/internal/transform" "github.com/DeanThompson/ginpprof" + "github.com/gin-contrib/gzip" "github.com/gin-contrib/static" "github.com/gin-gonic/contrib/sentry" "github.com/gin-gonic/gin" @@ -45,6 +46,7 @@ func getViewURL(sub string) string { } func setupRouter(router *gin.Engine) { + router.Use(gzip.Gzip(gzip.DefaultCompression)) router.Use(static.Serve(getViewURL("/static"), newBinaryFileSystem("static"))) router.GET(getViewURL("/favicon.ico"), favicon) diff --git a/views_test.go b/views_test.go index 967bf104c..fb1faed04 100644 --- a/views_test.go +++ b/views_test.go @@ -454,3 +454,50 @@ func TestStaticFilesPrefix(t *testing.T) { } } } + +func TestGzipMiddleware(t *testing.T) { + mockConfig() + r := ginTestEngine() + paths := []string{"/", "/help", "/alerts.json", "/autocomplete.json", "/metrics"} + for _, path := range paths { + req, _ := http.NewRequest("GET", path, nil) + req.Header.Set("Accept-Encoding", "gzip") + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + h := resp.Header() + + ce := h.Get("Content-Encoding") + if ce != "gzip" { + t.Errorf("Inavlid 'Content-Encoding' in response for '%s', expected 'gzip', got '%s'", path, ce) + } + + bs := h.Get("Content-Length") + if fmt.Sprint(resp.Body.Len()) != bs { + t.Errorf("Invalid 'Content-Length' in response for '%s', body size was %d but header value was '%s'", path, resp.Body.Len(), bs) + } + } +} + +func TestGzipMiddlewareWithoutAcceptEncoding(t *testing.T) { + mockConfig() + r := ginTestEngine() + paths := []string{"/", "/help", "/alerts.json", "/autocomplete.json", "/metrics"} + for _, path := range paths { + req, _ := http.NewRequest("GET", path, nil) + req.Header.Set("Accept-Encoding", "") // ensure that we don't pass anything up + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + h := resp.Header() + + ce := h.Get("Content-Encoding") + if ce == "gzip" { + t.Errorf("Inavlid 'Content-Encoding' in response for '%s', expected '', got '%s'", path, ce) + } + + bs := h.Get("Content-Length") + // if we got Content-Length then compare it with body size + if bs != "" && fmt.Sprint(resp.Body.Len()) != bs { + t.Errorf("Invalid 'Content-Length' in response for '%s', body size was %d but header value was '%s'", path, resp.Body.Len(), bs) + } + } +}