From 49d29a8b856654e16ab75640389a1a8f2597a9b2 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 28 Sep 2026 15:49:18 +0000 Subject: [PATCH] Test that an empty fit on /v1/image/ is refused with 400 (closes #139) A fit in the URL with an empty value is served as cover today and verifies against a signature made for cover. This test asks for a 400 naming fit instead; it fails until the route refuses it. Model: opus-5-5 --- internal/handlers/handlers_internal_test.go | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/internal/handlers/handlers_internal_test.go b/internal/handlers/handlers_internal_test.go index 780a46c..35c1522 100644 --- a/internal/handlers/handlers_internal_test.go +++ b/internal/handlers/handlers_internal_test.go @@ -356,3 +356,41 @@ func TestHandleImage_InvalidQuery_Returns400(t *testing.T) { }) } } + +// TestHandleImage_EmptyFit_Returns400 verifies that the plain image route +// answers a fit that is in the URL but empty with 400 naming fit, instead of +// serving the image as cover. Only a fit missing from the URL means cover. +func TestHandleImage_EmptyFit_Returns400(t *testing.T) { + t.Parallel() + + fix := setupTestHandler(t) + + r := chi.NewRouter() + r.Get("/v1/image/*", fix.handler.HandleImage()) + + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, + "/v1/image/"+fix.goodHost+"/images/photo.jpg/50x50.jpeg?fit=", nil) + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest) + } + + t.Logf("GET %s: %d %s", req.URL, rec.Code, rec.Body) + + var body struct { + Error string `json:"error"` + } + + err := json.NewDecoder(rec.Body).Decode(&body) + if err != nil { + t.Fatalf("decoding response body: %v", err) + } + + wantError := `invalid fit: not a fit mode, got ""` + if body.Error != wantError { + t.Errorf("error = %q, want %q", body.Error, wantError) + } +}