Files
summercms/modules/lagoon/attach/diskext_test.go
Jakub Zych 38f6d65c09 test(19.3-05): build tags, S3 tag guard, DiskExtension and public_url
- manifest_tags_test.go: build.tags load, trim, dedupe and validation
  errors naming build.tags; SaveManifest keeps tags and plugin order;
  goBuildArgs puts one -tags before -o
- s3_test.go: s3:// refused without summercms_s3 naming the tag, opens
  with it (both build modes, any scheme case)
- diskext_test.go: RESEARCH probe rows and table consistency
- url_test.go: public_url base joining, File.URL and thumbnails,
  malformed values refused naming the key
- store_test.go: Postgres regression table through Store
- gate: attach names in both tag modes, Store regression in docker
2026-10-15 19:37:35 +02:00

112 lines
4.0 KiB
Go

package attach
import (
"net/http"
"slices"
"testing"
)
// TestDiskExtensionTable (plan 05 of phase 19.3, D-15) pins DiskExtension on
// the RESEARCH probe rows and on the consistency of its tables: a client
// extension that belongs to the sniffed type is kept, any other becomes the
// type's canonical one, and a script-capable extension under a generic type
// becomes txt (text/*) or bin.
func TestDiskExtensionTable(t *testing.T) {
for _, tc := range []struct {
contentType, clientExt, want string
}{
// RESEARCH probe rows.
{"image/png", "html", "png"},
{"image/jpeg", "jpeg", "jpeg"},
{"image/jpeg", "jpg", "jpg"},
{"image/jpeg", "png", "jpg"},
{"video/webm", "mkv", "mkv"},
{"video/webm", "webm", "webm"},
{"application/zip", "docx", "docx"},
{"application/zip", "xlsx", "xlsx"},
{"text/plain; charset=utf-8", "ics", "ics"},
{"text/plain; charset=utf-8", "html", "txt"},
{"text/xml; charset=utf-8", "svg", "txt"},
{"application/octet-stream", "js", "bin"},
{"application/pdf", "txt", "pdf"},
// HTML bytes under their own name never stay html.
{"text/html; charset=utf-8", "html", "txt"},
{"text/html; charset=utf-8", "txt", "txt"},
// Normalisation: case, a leading dot, parameters and spaces.
{"IMAGE/PNG", ".PNG", "png"},
{" image/jpeg ; q=1", "JPE", "jpe"},
{"application/octet-stream", ".Docx", "docx"},
// Extensions outside [a-z0-9]{1,10} fall back like a script-capable one.
{"application/octet-stream", "", "bin"},
{"text/plain", "", "txt"},
{"application/octet-stream", "tar.gz", "bin"},
{"text/plain", "averyverylongext", "txt"},
{"application/octet-stream", "ex e", "bin"},
// Other specific types.
{"image/gif", "png", "gif"},
{"image/webp", "webp", "webp"},
{"application/ogg", "opus", "opus"},
{"audio/mpeg", "wav", "mp3"},
{"font/woff2", "woff2", "woff2"},
{"application/x-gzip", "tgz", "tgz"},
} {
if got := DiskExtension(tc.contentType, tc.clientExt); got != tc.want {
t.Errorf("DiskExtension(%q, %q) = %q, want %q", tc.contentType, tc.clientExt, got, tc.want)
}
}
// Every specific type keeps each of its own extensions, maps a
// script-capable one to its canonical extension, and lists no
// script-capable extension itself.
for ct, exts := range sniffedExtensions {
if len(exts) == 0 {
t.Fatalf("%s has no extensions", ct)
}
for _, ext := range exts {
if !extPattern.MatchString(ext) {
t.Errorf("%s: extension %q does not match the disk name pattern", ct, ext)
}
if slices.Contains(scriptCapableExtensions, ext) {
t.Errorf("%s: script-capable extension %q is kept", ct, ext)
}
if got := DiskExtension(ct, ext); got != ext {
t.Errorf("DiskExtension(%q, %q) = %q", ct, ext, got)
}
}
for _, bad := range scriptCapableExtensions {
if got := DiskExtension(ct, bad); got != exts[0] {
t.Errorf("DiskExtension(%q, %q) = %q, want %q", ct, bad, got, exts[0])
}
}
}
// Under every generic type a script-capable extension is rewritten.
for _, ct := range []string{"text/plain; charset=utf-8", "text/html; charset=utf-8", "text/xml; charset=utf-8", "text/css"} {
for _, bad := range scriptCapableExtensions {
if got := DiskExtension(ct, bad); got != "txt" {
t.Errorf("DiskExtension(%q, %q) = %q, want txt", ct, bad, got)
}
}
}
for _, ct := range []string{"application/octet-stream", "application/zip", "application/x-msdownload", ""} {
for _, bad := range scriptCapableExtensions {
if got := DiskExtension(ct, bad); got != "bin" {
t.Errorf("DiskExtension(%q, %q) = %q, want bin", ct, bad, got)
}
}
}
// The table agrees with what the standard library actually sniffs.
for body, want := range map[string]string{
"\x89PNG\r\n\x1a\n": "png",
"%PDF-1.7\n": "pdf",
"<html><body>x</body></html>": "txt",
"<?xml version=\"1.0\"?><svg xmlns=\"http://www.w3.org/2000/svg\"/>": "txt",
} {
ct := http.DetectContentType([]byte(body))
if got := DiskExtension(ct, "html"); got != want {
t.Errorf("body %q sniffed %q: DiskExtension = %q, want %q", body, ct, got, want)
}
}
}