mirror of
https://github.com/prymitive/karma
synced 2026-05-09 03:36:44 +00:00
Merge pull request #162 from cloudflare/gzip
Use gzip middlerware to compress responses if client can accept compr…
This commit is contained in:
8
Gopkg.lock
generated
8
Gopkg.lock
generated
@@ -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
|
||||
|
||||
2
main.go
2
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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user