package handler import ( "strings" "testing" "unicode/utf8" ) func TestSanitizePageURL(t *testing.T) { got := sanitizePageURL("https://shop.example.com/p/1?token=secret&ok=1#/cart") if got == "" || strings.Contains(got, "token=") { t.Fatalf("sanitize failed: %q", got) } if !strings.Contains(got, "ok=1") { t.Fatalf("should keep ok param: %q", got) } // SPA hash 路由必须保留,否则换页轨迹会被去重丢掉 if !strings.Contains(got, "#/cart") { t.Fatalf("should keep hash fragment for SPA: %q", got) } home := sanitizePageURL("http://localhost:5173/demo-shop/index.html#/") cart := sanitizePageURL("http://localhost:5173/demo-shop/index.html#/cart") if home == "" || cart == "" || home == cart { t.Fatalf("hash pages must differ: home=%q cart=%q", home, cart) } if sanitizePageURL("javascript:alert(1)") != "" { t.Fatal("reject javascript") } if sanitizePageURL("") != "" { t.Fatal("empty") } } func TestSanitizePageTitle(t *testing.T) { long := strings.Repeat("测", 300) got := sanitizePageTitle(long) if utf8.RuneCountInString(got) != maxPageTitleLen { t.Fatalf("title len %d", utf8.RuneCountInString(got)) } }