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) } }