Files
pixa/internal/magic/svg_internal_test.go
T
sneak 632fe0bd36
check / check (push) Canceled after 0s
Support SVG sources (closes #68)
The fetcher accepts image/svg+xml again and internal/magic detects SVG.
A raster format renders the SVG with librsvg at the density that makes
it cover the output size, so an enlarged SVG stays sharp; the rendered
size is held to MaxInputDimension. orig of an SVG at the orig size is
the SVG unchanged, cached as a variant; at any other size it is
ErrSVGNotOriginalSize, answered 400. The cache reads up to 1024 first
bytes of a file to find its format, as an SVG's root element can come
after a prolog. Tests that pinned SVG as refused now expect it to be
read.

Model: opus-5-5
2026-11-11 00:38:16 +00:00

86 lines
2.5 KiB
Go

package magic
import (
"errors"
"strings"
"testing"
"sneak.berlin/go/pixa/internal/imagerequest"
)
// TestDetectFormat_SVG checks that DetectFormat names SVG text with what may
// come before the root element, and does not name as SVG text that does not
// start with "<" or has no "<svg" within its first MaxMagicBytes bytes.
func TestDetectFormat_SVG(t *testing.T) {
t.Parallel()
const root = `<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"/>`
tests := []struct {
name string
data string
wantErr error
}{
{name: "root element only", data: root},
{name: "XML declaration", data: `<?xml version="1.0"?>` + "\n" + root},
{name: "byte order mark and white space", data: "\xEF\xBB\xBF \n\t" + root},
{name: "comment", data: "<!-- a drawing -->" + root},
{
name: "DOCTYPE",
data: `<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ` +
`"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">` + root,
},
{
name: "root element after the first bytes",
data: "<!--" + strings.Repeat(" ", MaxMagicBytes) + "-->" + root,
wantErr: ErrUnknownFormat,
},
{name: "text naming <svg", data: "draw it with " + root, wantErr: ErrUnknownFormat},
{name: "HTML", data: "<!DOCTYPE html><html></html>", wantErr: ErrUnknownFormat},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := DetectFormat([]byte(tt.data))
t.Logf("DetectFormat() = %q, %v", got, err)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("DetectFormat() error = %v, want %v", err, tt.wantErr)
}
return
}
if err != nil || got != imagerequest.FormatSVG {
t.Errorf("DetectFormat() = %q, %v; want %q", got, err,
imagerequest.FormatSVG)
}
})
}
}
// TestValidateMagicBytes_SVG checks that SVG data is SVG for the
// Content-Type image/svg+xml, with a charset too, and a mismatch for another.
func TestValidateMagicBytes_SVG(t *testing.T) {
t.Parallel()
data := []byte(`<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"/>`)
for _, declared := range []string{"image/svg+xml", "image/svg+xml; charset=utf-8"} {
got, err := ValidateMagicBytes(data, declared)
if err != nil || got != imagerequest.FormatSVG {
t.Errorf("ValidateMagicBytes(%q) = %q, %v; want %q", declared, got,
err, imagerequest.FormatSVG)
}
}
_, err := ValidateMagicBytes(data, "image/png")
if !errors.Is(err, ErrMagicByteMismatch) {
t.Errorf("ValidateMagicBytes(image/png) error = %v, want %v", err,
ErrMagicByteMismatch)
}
}