A status page with 3128 components was 30 MB of uncompressed HTML: all components at once, each with 90 bars carrying a tooltip. - The status page server gzips text responses. - Status pages list 50 components at a time (?page=N) and offer a search (?q=) over component and group names once they have more than one page. Group headings keep the status and count of the whole group. Single-file exports still list every component. - Rows linking a detail page draw their bars as one path per status, without tooltips; the detail page keeps them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package platform
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestCompress(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
page := strings.Repeat("<li>component</li>", 2000)
|
|
r := gin.New()
|
|
r.Use(Compress())
|
|
r.GET("/page", func(c *gin.Context) {
|
|
c.Header("Content-Type", "text/html; charset=utf-8")
|
|
c.Status(http.StatusOK)
|
|
for i := 0; i < 2000; i++ {
|
|
_, _ = c.Writer.WriteString("<li>component</li>")
|
|
}
|
|
})
|
|
r.GET("/small", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
|
|
r.GET("/sized", func(c *gin.Context) {
|
|
c.Header("Content-Length", strconv.Itoa(len("tiny")))
|
|
c.Data(http.StatusOK, "text/plain", []byte("tiny"))
|
|
})
|
|
r.GET("/png", func(c *gin.Context) { c.Data(http.StatusOK, "image/png", []byte(page)) })
|
|
r.GET("/json", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"page": page}) })
|
|
r.GET("/notmodified", func(c *gin.Context) { c.Status(http.StatusNotModified) })
|
|
|
|
get := func(path, accept string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
if accept != "" {
|
|
req.Header.Set("Accept-Encoding", accept)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
r.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
gunzip := func(t *testing.T, rec *httptest.ResponseRecorder) string {
|
|
t.Helper()
|
|
zr, err := gzip.NewReader(rec.Body)
|
|
if err != nil {
|
|
t.Fatalf("gzip.NewReader() error = %v", err)
|
|
}
|
|
b, err := io.ReadAll(zr)
|
|
if err != nil {
|
|
t.Fatalf("reading gzip body error = %v", err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
rec := get("/page", "br, gzip;q=0.8")
|
|
if rec.Header().Get("Content-Encoding") != "gzip" || rec.Header().Get("Vary") != "Accept-Encoding" {
|
|
t.Fatalf("headers = %v, want gzip with Vary", rec.Header())
|
|
}
|
|
if rec.Body.Len() >= len(page)/10 {
|
|
t.Errorf("compressed size = %d, want far below %d", rec.Body.Len(), len(page))
|
|
}
|
|
if got := gunzip(t, rec); got != page {
|
|
t.Errorf("decompressed body differs: %d bytes, want %d", len(got), len(page))
|
|
}
|
|
if rec := get("/json", "gzip"); rec.Header().Get("Content-Encoding") != "gzip" || !strings.Contains(gunzip(t, rec), "component") {
|
|
t.Errorf("JSON not compressed: %v", rec.Header())
|
|
}
|
|
for _, tc := range []struct{ path, accept string }{
|
|
{"/page", ""},
|
|
{"/page", "gzip;q=0, deflate"},
|
|
{"/page", "identity"},
|
|
{"/sized", "gzip"},
|
|
{"/png", "gzip"},
|
|
{"/notmodified", "gzip"},
|
|
} {
|
|
if rec := get(tc.path, tc.accept); rec.Header().Get("Content-Encoding") != "" {
|
|
t.Errorf("GET %s with %q compressed, want plain", tc.path, tc.accept)
|
|
}
|
|
}
|
|
if rec := get("/page", "*"); rec.Header().Get("Content-Encoding") != "gzip" {
|
|
t.Errorf("Accept-Encoding * not compressed")
|
|
}
|
|
}
|