api/gzip_test.go
Claude 43abce034e
chore(api): AX compliance sweep — banned imports, naming, test coverage
Replace fmt/errors/strings/encoding/json/os/os/exec/path/filepath with
core primitives; rename abbreviated variables; add Ugly test variants to
all test files; rename integration tests to TestFilename_Function_{Good,Bad,Ugly}.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 09:27:41 +01:00

157 lines
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")
}
}
func TestWithGzip_Ugly_NilBodyDoesNotPanic(t *testing.T) {
gin.SetMode(gin.TestMode)
defer func() {
if r := recover(); r != nil {
t.Fatalf("gzip handler panicked on nil body: %v", r)
}
}()
engine, err := api.New(api.WithGzip())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
recorder := httptest.NewRecorder()
request, _ := http.NewRequest(http.MethodGet, "/health", nil)
request.Header.Set("Accept-Encoding", "gzip")
engine.Handler().ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
}