api/gzip_test.go
Claude ca9b495884
chore: migrate to dappco.re vanity import path
Change module path from forge.lthn.ai/core/api to dappco.re/go/core/api.
Update all Go imports accordingly:
- forge.lthn.ai/core/api -> dappco.re/go/core/api
- forge.lthn.ai/core/go-io -> dappco.re/go/core/io
- forge.lthn.ai/core/go-log -> dappco.re/go/core/log

forge.lthn.ai/core/cli left as-is (not yet migrated).
Local replace directives added for dappco.re paths until vanity
URL server is configured.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 23:50:37 +00:00

133 lines
3.4 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"compress/gzip"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// ── WithGzip ──────────────────────────────────────────────────────────
func TestWithGzip_Good_CompressesResponse(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithGzip())
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
req.Header.Set("Accept-Encoding", "gzip")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
ce := w.Header().Get("Content-Encoding")
if ce != "gzip" {
t.Fatalf("expected Content-Encoding=%q, got %q", "gzip", ce)
}
}
func TestWithGzip_Good_NoCompressionWithoutAcceptHeader(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithGzip())
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
// Deliberately not setting Accept-Encoding header.
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
ce := w.Header().Get("Content-Encoding")
if ce == "gzip" {
t.Fatal("expected no gzip Content-Encoding when client does not request it")
}
}
func TestWithGzip_Good_DefaultLevel(t *testing.T) {
// Calling WithGzip() with no arguments should use default compression
// and not panic.
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithGzip())
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
req.Header.Set("Accept-Encoding", "gzip")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
ce := w.Header().Get("Content-Encoding")
if ce != "gzip" {
t.Fatalf("expected Content-Encoding=%q with default level, got %q", "gzip", ce)
}
}
func TestWithGzip_Good_CustomLevel(t *testing.T) {
// WithGzip(gzip.BestSpeed) should work without panicking and still compress.
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithGzip(gzip.BestSpeed))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
req.Header.Set("Accept-Encoding", "gzip")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
ce := w.Header().Get("Content-Encoding")
if ce != "gzip" {
t.Fatalf("expected Content-Encoding=%q with BestSpeed, got %q", "gzip", ce)
}
}
func TestWithGzip_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(
api.WithGzip(),
api.WithRequestID(),
)
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
req.Header.Set("Accept-Encoding", "gzip")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Both gzip compression and request ID should be present.
ce := w.Header().Get("Content-Encoding")
if ce != "gzip" {
t.Fatalf("expected Content-Encoding=%q, got %q", "gzip", ce)
}
rid := w.Header().Get("X-Request-ID")
if rid == "" {
t.Fatal("expected X-Request-ID header from WithRequestID")
}
}