Tests for #88, before the change: the format chosen for each Accept header (AVIF-capable, WebP-only, neither, wildcard only, absent, q=0 on AVIF, malformed), both image routes serving an auto URL in the chosen format with Vary: Accept, also on HEAD and 304, the signature covering auto rather than the chosen format, each chosen format cached apart, and Vary: Accept next to the CORS middleware's Vary: Origin. They do not compile yet: FormatAuto and formatForAccept do not exist. Model: opus-5-5
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
// TestFormatAutoVaryNextToOrigin requests an image URL with the format auto
|
|
// through the server's routes and verifies that the answer carries
|
|
// Vary: Accept next to the Vary: Origin the CORS middleware sends.
|
|
func TestFormatAutoVaryNextToOrigin(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
source := encodeTestPNG(t, 64, 48)
|
|
|
|
upstream := httptest.NewServer(http.HandlerFunc(
|
|
func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
_, _ = w.Write(source)
|
|
}))
|
|
t.Cleanup(upstream.Close)
|
|
|
|
s, _, _ := startImageProxy(t, upstream)
|
|
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
|
"/v1/image/"+upstreamHost+"/photo.png/32x24.auto", nil)
|
|
req.Header.Set("Origin", "https://app.example.com")
|
|
req.Header.Set("Accept", "image/webp")
|
|
|
|
rec := httptest.NewRecorder()
|
|
s.ServeHTTP(rec, req)
|
|
|
|
vary := rec.Header().Values("Vary")
|
|
t.Logf("status %d, Content-Type %s, Vary %v",
|
|
rec.Code, rec.Header().Get("Content-Type"), vary)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
|
|
if want := []string{"Origin", "Accept"}; !slices.Equal(vary, want) {
|
|
t.Errorf("Vary = %v, want %v", vary, want)
|
|
}
|
|
}
|