saveinmed/backend/internal/http/middleware/compress_test.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

60 lines
1.5 KiB
Go

package middleware
import (
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestGzip_SkipsWhenNotAccepted(t *testing.T) {
handler := Gzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("plain"))
}))
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if got := rec.Header().Get("Content-Encoding"); got != "" {
t.Errorf("expected no content encoding, got %q", got)
}
if rec.Body.String() != "plain" {
t.Errorf("expected plain response, got %q", rec.Body.String())
}
}
func TestGzip_CompressesWhenAccepted(t *testing.T) {
handler := Gzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("compressed"))
}))
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "gzip")
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if got := rec.Header().Get("Content-Encoding"); got != "gzip" {
t.Fatalf("expected gzip encoding, got %q", got)
}
if got := rec.Header().Get("Vary"); !strings.Contains(got, "Accept-Encoding") {
t.Fatalf("expected Vary to include Accept-Encoding, got %q", got)
}
reader, err := gzip.NewReader(rec.Body)
if err != nil {
t.Fatalf("failed to create gzip reader: %v", err)
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("failed to read gzip body: %v", err)
}
if string(data) != "compressed" {
t.Errorf("expected decompressed body 'compressed', got %q", string(data))
}
}