|
| 1 | +package crawler |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestResolveURL_SchemeAllowList(t *testing.T) { |
| 6 | + const base = "https://example.com/docs/" |
| 7 | + cases := []struct { |
| 8 | + name string |
| 9 | + href string |
| 10 | + want string |
| 11 | + }{ |
| 12 | + {"relative path", "guide.html", "https://example.com/docs/guide.html"}, |
| 13 | + {"absolute http", "http://example.com/x", "http://example.com/x"}, |
| 14 | + {"absolute https", "https://example.com/x", "https://example.com/x"}, |
| 15 | + {"fragment only", "#anchor", ""}, |
| 16 | + {"mailto", "mailto:a@b.c", ""}, |
| 17 | + {"javascript", "javascript:alert(1)", ""}, |
| 18 | + {"javascript case", "JavaScript:alert(1)", ""}, |
| 19 | + {"data uri", "data:text/html,<script>alert(1)</script>", ""}, |
| 20 | + {"vbscript", "vbscript:msgbox(1)", ""}, |
| 21 | + {"tel", "tel:+15555555555", ""}, |
| 22 | + {"file", "file:///etc/passwd", ""}, |
| 23 | + {"blob", "blob:https://example.com/abc", ""}, |
| 24 | + {"ftp", "ftp://example.com/file", ""}, |
| 25 | + } |
| 26 | + for _, tc := range cases { |
| 27 | + t.Run(tc.name, func(t *testing.T) { |
| 28 | + got := resolveURL(base, tc.href) |
| 29 | + if got != tc.want { |
| 30 | + t.Errorf("resolveURL(%q, %q) = %q, want %q", base, tc.href, got, tc.want) |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
0 commit comments