package handlers import ( "fmt" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "sneak.berlin/go/pixa/internal/imageprocessor" "sneak.berlin/go/pixa/internal/imagerequest" ) // TestAVIFOutputTooLargeAnswers400 checks that respondImageError, which // answers the errors of both image routes, answers 400 naming the AVIF pixel // limit when the image processor refuses an AVIF output above it, wrapped as // the service wraps it. The request's source only goes into a log line, so it // is left empty. func TestAVIFOutputTooLargeAnswers400(t *testing.T) { t.Parallel() h := &Handlers{log: slog.New(slog.DiscardHandler)} err := fmt.Errorf("image processing failed: %w: 4097x4096", imageprocessor.ErrAVIFOutputTooLarge) wantBody := fmt.Sprintf("above %d pixels", imageprocessor.MaxAVIFOutputPixels) rec := httptest.NewRecorder() h.respondImageError(rec, &imagerequest.Request{}, err) t.Logf("status = %d, body = %q", rec.Code, rec.Body.String()) if rec.Code != http.StatusBadRequest { t.Errorf("status = %d, want %d", rec.Code, http.StatusBadRequest) } if !strings.Contains(rec.Body.String(), wantBody) { t.Errorf("body = %q, want it to say %q", rec.Body.String(), wantBody) } }