package handlers import ( "fmt" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "sneak.berlin/go/pixa/internal/httpfetcher" "sneak.berlin/go/pixa/internal/imageprocessor" "sneak.berlin/go/pixa/internal/imgcache" ) // TestServerBusyAnswers503 checks that both image routes answer 503 with // a clear error when the image service gives up waiting for a free upstream // connection or processing slot, wrapped as the service wraps them. func TestServerBusyAnswers503(t *testing.T) { t.Parallel() h := &Handlers{log: slog.New(slog.DiscardHandler)} req := &imgcache.ImageRequest{SourceHost: "img.example.com", SourcePath: "/a.jpg"} for _, err := range []error{ fmt.Errorf("upstream fetch failed: %w", httpfetcher.ErrTooManyConnections), fmt.Errorf("image processing failed: %w", imageprocessor.ErrTooManyImages), } { plain := httptest.NewRecorder() h.respondImageError(plain, req, err) encrypted := httptest.NewRecorder() h.handleImageError(encrypted, err) for route, rec := range map[string]*httptest.ResponseRecorder{ "/v1/image/": plain, "/v1/e/": encrypted, } { if rec.Code != http.StatusServiceUnavailable { t.Errorf("%s for %v: status = %d, want %d", route, err, rec.Code, http.StatusServiceUnavailable) } if !strings.Contains(rec.Body.String(), "server busy, try again later") { t.Errorf("%s for %v: body = %q, want the server busy error", route, err, rec.Body.String()) } } } }