Files
sm-sitemap-plugin/classes/rewrite_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

53 lines
2.7 KiB
Go

package classes_test
import (
"testing"
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
)
// TestRewrite pins the PHP routes.php frontend_url rewrite: the backend URL
// is replaced inside loc, inside xhtml:link href and in the exact stylesheet
// href, nowhere else; slashes are trimmed on both; an empty or "0" frontend
// URL and equal URLs leave the document unchanged.
func TestRewrite(t *testing.T) {
const backend = "https://api.ex.pl"
doc := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
`<?xml-stylesheet type="text/xsl" href="https://api.ex.pl/sitemap.xsl"?>` + "\n" +
`<urlset><url><loc>https://api.ex.pl/a</loc><lastmod>2026</lastmod><changefreq>https://api.ex.pl</changefreq><priority>1</priority>` +
`<xhtml:link rel="alternate" hreflang="pl" href="https://api.ex.pl/a"/><xhtml:link rel="alternate" hreflang="en" href="https://api.ex.pl/en/a"/>` +
`<extra>https://api.ex.pl/x</extra></url><url><loc>https://other.pl/https://api.ex.pl</loc><changefreq>c</changefreq><priority>p</priority></url></urlset>` + "\n"
want := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
`<?xml-stylesheet type="text/xsl" href="https://www.ex.pl/sitemap.xsl"?>` + "\n" +
`<urlset><url><loc>https://www.ex.pl/a</loc><lastmod>2026</lastmod><changefreq>https://api.ex.pl</changefreq><priority>1</priority>` +
`<xhtml:link rel="alternate" hreflang="pl" href="https://www.ex.pl/a"/><xhtml:link rel="alternate" hreflang="en" href="https://www.ex.pl/en/a"/>` +
`<extra>https://api.ex.pl/x</extra></url><url><loc>https://other.pl/https://www.ex.pl</loc><changefreq>c</changefreq><priority>p</priority></url></urlset>` + "\n"
for _, frontend := range []string{"https://www.ex.pl", "https://www.ex.pl/", "https://www.ex.pl///"} {
for _, back := range []string{backend, backend + "/"} {
if got := classes.Rewrite(doc, back, frontend); got != want {
t.Fatalf("Rewrite(%q, %q):\n%s\nwant:\n%s", back, frontend, got, want)
}
}
}
for _, frontend := range []string{"", "0"} {
if got := classes.Rewrite(doc, backend, frontend); got != doc {
t.Fatalf("frontend %q changed the document", frontend)
}
}
if got := classes.Rewrite(doc, backend, backend); got != doc {
t.Fatal("equal URLs changed the document")
}
if got := classes.Rewrite(doc, backend+"/", backend); got != doc {
t.Fatal("equal URLs up to a slash changed the document")
}
// An empty backend URL replaces nothing.
if got := classes.Rewrite(doc, "", "https://www.ex.pl"); got != doc {
t.Fatal("an empty backend URL changed the document")
}
// A stylesheet on another path is not the PHP pattern.
other := `<?xml-stylesheet type="text/xsl" href="https://api.ex.pl/other.xsl"?>`
if got := classes.Rewrite(other, backend, "https://www.ex.pl"); got != other {
t.Fatalf("other stylesheet = %q", got)
}
}