check / check (push) Canceled after 0s
magic.DetectFormat returns an imagerequest.Format. ValidateMagicBytes returns the format it checked against the upstream Content-Type. The cache stores that format in the source's row, so a variant made from a cached source uses it too. The proxy hands it to Process, which still refuses input libvips would load with another loader, such as an SVG passing as AVIF. Format.MIMEType declares each MIME string once, and the fetcher, the cache's variant read and the Accept matching use it. magic.MIMEType, magic.ImageFormat, MIMEToImageFormat, magic's SVG detection, detectFormat, formatFromString, FormatToMIME and Result.InputFormat are gone. Model: opus-5-5
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
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/imagerequest"
|
|
)
|
|
|
|
// TestUnsupportedSourceFormatAnswers415 checks that respondImageError, which
|
|
// answers the errors of both image routes, answers 415 with a clear error when
|
|
// the upstream answers with a content type pixa does not accept, such as SVG,
|
|
// and when libvips does not read a source as the format its first bytes
|
|
// show, wrapped as the service wraps them.
|
|
func TestUnsupportedSourceFormatAnswers415(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
h := &Handlers{log: slog.New(slog.DiscardHandler)}
|
|
req := &imagerequest.Request{
|
|
Source: imagerequest.Source{Host: "img.example.com", Path: "/a.svg"},
|
|
}
|
|
|
|
for _, err := range []error{
|
|
fmt.Errorf("upstream fetch failed: %w: image/svg+xml",
|
|
httpfetcher.ErrInvalidContentType),
|
|
fmt.Errorf("image processing failed: %w: libvips does not read the input as avif",
|
|
imageprocessor.ErrUnsupportedInputFormat),
|
|
} {
|
|
rec := httptest.NewRecorder()
|
|
h.respondImageError(rec, req, err)
|
|
t.Logf("for %v: %d %s", err, rec.Code, rec.Body.String())
|
|
|
|
if rec.Code != http.StatusUnsupportedMediaType {
|
|
t.Errorf("for %v: status = %d, want %d",
|
|
err, rec.Code, http.StatusUnsupportedMediaType)
|
|
}
|
|
|
|
if !strings.Contains(rec.Body.String(), "unsupported source format") {
|
|
t.Errorf("for %v: body = %q, want the unsupported source format error",
|
|
err, rec.Body.String())
|
|
}
|
|
}
|
|
}
|