package handlers import ( "log/slog" "net/http" "testing" "github.com/davidbyttow/govips/v2/vips" "sneak.berlin/go/pixa/internal/imgcache" ) // jxlType is the content type of JPEG XL. const jxlType = "image/jxl" // TestFormatForAccept_JPEGXL verifies that the format auto chooses JPEG XL // when Accept names image/jxl, ahead of AVIF whatever their q, and AVIF when // Accept refuses JPEG XL with q=0. func TestFormatForAccept_JPEGXL(t *testing.T) { t.Parallel() tests := []struct { name string accept string want imgcache.ImageFormat }{ {"JPEG XL-capable browser", "image/jxl,image/avif,image/webp,image/*,*/*;q=0.8", imgcache.FormatJXL}, {"JPEG XL only", jxlType, imgcache.FormatJXL}, {"JPEG XL with a lower q than AVIF", "image/avif,image/jxl;q=0.5", imgcache.FormatJXL}, {"q=0 on JPEG XL", "image/jxl;q=0,image/avif,*/*", imgcache.FormatAVIF}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() got, err := formatForAccept(tt.accept) if got != tt.want || err != nil { t.Errorf("formatForAccept(%q) = %q, %v, want %q", tt.accept, got, err, tt.want) } }) } } // TestFormatAuto_JPEGXL requests an auto URL on each image route with an // Accept header that names image/jxl, and checks that the answer is a JPEG XL // image. func TestFormatAuto_JPEGXL(t *testing.T) { t.Parallel() h, srv := newSignedHostServer(t, slog.New(slog.DiscardHandler)) signedURL, encryptedURL := autoPhotoURLs(t, h) for _, target := range []string{signedURL, encryptedURL} { rec := requestImage(t, srv, http.MethodGet, target, "image/jxl,image/avif,image/webp,*/*;q=0.8") gotType := rec.Header().Get("Content-Type") if rec.Code != http.StatusOK || gotType != jxlType { t.Errorf("%s: %d %s, want 200 %s; body %s", target, rec.Code, gotType, jxlType, rec.Body) continue } if got := vips.DetermineImageType(rec.Body.Bytes()); got != vips.ImageTypeJXL { t.Errorf("%s: body is %s, want jxl", target, vips.ImageTypes[got]) } } }