Files
sm-sitemap-plugin/classes/urlto_test.go
Jakub Zych 4e55939a1f test(17-06): sitemap parity unit tests
- TestURLTo (every Url::to golden case, BackendURL, URLPath),
  TestXMLEscaping (PHP XmlEscapingTest as behaviour), TestTiers (each
  default rule, first match, off, bad pattern fails Boot), TestRewrite,
  TestHooks, TestURLCap, TestManifest (fail-safe reads, key
  normalization, dates), TestFillable (PHP MassAssignmentTest),
  TestDefinitionItemJSON, TestBackendURL (trusted forwarded proto)
- TestImportWinter on Postgres: maps and drops with report, --theme,
  --dry-run, --force, settings upsert, refusals
2026-10-12 19:38:04 +02:00

58 lines
1.6 KiB
Go

package classes_test
import (
"encoding/json"
"strings"
"testing"
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
)
// TestURLTo runs every case of testdata/urlto.json through URLTo and
// compares it with the line PHP's Url::to wrote to urlto.golden (SC-3), then
// pins the helpers built on it: BackendURL and URLPath.
func TestURLTo(t *testing.T) {
var in struct {
Root string `json:"root"`
URLs []*string `json:"urls"`
}
if err := json.Unmarshal(readTestdata(t, "urlto.json"), &in); err != nil {
t.Fatal(err)
}
want := strings.Split(strings.TrimSuffix(string(readTestdata(t, "urlto.golden")), "\n"), "\n")
if len(in.URLs) < 50 || len(want) != len(in.URLs) {
t.Fatalf("%d inputs, %d golden lines", len(in.URLs), len(want))
}
for i, u := range in.URLs {
path := "<null>"
input := ""
if u != nil {
path, input = *u, *u
}
if got := classes.URLTo(in.Root, input); got != want[i] {
t.Errorf("case %d URLTo(%q) = %q, PHP %q", i, path, got, want[i])
}
}
for _, tc := range []struct{ root, want string }{
{"https://api.ex.pl", "https://api.ex.pl"},
{"https://api.ex.pl/", "https://api.ex.pl"},
{"http://h:8080/base", "http://h:8080/base"},
} {
if got := classes.BackendURL(tc.root); got != tc.want {
t.Errorf("BackendURL(%q) = %q, want %q", tc.root, got, tc.want)
}
}
for raw, want := range map[string]string{
"https://h/a/b?x=1#f": "/a/b",
"https://h": "",
"https://h/": "/",
"/rel/path": "/rel/path",
"": "",
} {
if got := classes.URLPath(raw); got != want {
t.Errorf("URLPath(%q) = %q, want %q", raw, got, want)
}
}
}