package server import ( "net/http" "net/http/httptest" "os" "testing" "github.com/davidbyttow/govips/v2/vips" ) // TestImageProxyFlowJPEGXL requests the JPEG XL testdata/red.jxl, 64x48 and // made with libvips, through pixa's router, upstream fetcher, image processor // and disk cache: resized as PNG, resized as JPEG XL, and at its own size and // format, which is JPEG XL. The second request for each URL is a cache hit. func TestImageProxyFlowJPEGXL(t *testing.T) { t.Parallel() source, err := os.ReadFile("testdata/red.jxl") if err != nil { t.Fatalf("reading the test image: %v", err) } upstream := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "image/jxl") _, _ = w.Write(source) })) t.Cleanup(upstream.Close) s, _, _ := startImageProxy(t, upstream) tests := []struct { sizeFormat string // the . part of the image URL contentType string imageType vips.ImageType width, height int }{ {"32x24.png", "image/png", vips.ImageTypePNG, 32, 24}, {"32x24.jxl", "image/jxl", vips.ImageTypeJXL, 32, 24}, {"orig.orig", "image/jxl", vips.ImageTypeJXL, 64, 48}, } for _, tt := range tests { target := "/v1/image/" + upstreamHost + "/red.jxl/" + tt.sizeFormat for _, wantCache := range []string{"MISS", "HIT"} { rec := getImage(t, s, target) gotType := rec.Header().Get("Content-Type") gotCache := rec.Header().Get("X-Pixa-Cache") if gotType != tt.contentType || gotCache != wantCache { t.Errorf("%s: %s, X-Pixa-Cache %s, want %s, %s", target, gotType, gotCache, tt.contentType, wantCache) } img, err := vips.NewImageFromBuffer(rec.Body.Bytes()) if err != nil { t.Fatalf("%s: loading the image: %v", target, err) } if img.Format() != tt.imageType || img.Width() != tt.width || img.Height() != tt.height { t.Errorf("%s: %s %dx%d, want %s %dx%d", target, vips.ImageTypes[img.Format()], img.Width(), img.Height(), vips.ImageTypes[tt.imageType], tt.width, tt.height) } img.Close() } } }