1// SPDX-License-Identifier: MIT
2
3package urlorigin
4
5import "testing"
6
7func TestCanonical(t *testing.T) {
8 for in, want := range map[string]string{
9 "https://Ermite.example/x": "https://ermite.example:443",
10 "HTTPS://ermite.example": "https://ermite.example:443",
11 "https://ermite.example:443/x": "https://ermite.example:443",
12 "http://a.test:80": "http://a.test:80",
13 "http://a.test": "http://a.test:80",
14 "http://a.test:8088/p?q#f": "http://a.test:8088",
15 "https://[::1]:8443/x": "https://[::1]:8443",
16 "https://[::1]/x": "https://[::1]:443",
17 "https://[::1]:443": "https://[::1]:443",
18 "https://[2001:DB8::1]:8443": "https://[2001:db8::1]:8443",
19 "http://[::1]:80": "http://[::1]:80",
20 "http://10.0.2.2:8088": "http://10.0.2.2:8088",
21 "ftp://a.test": "", "a.test": "", "https://u@a.test": "", "https://u:p@a.test": "", "": "", "https://": "", "//a.test": "",
22 "https://[::1:8443]/x": "https://[::1:8443]:443", // an IPv6 host with no port at all
23 } {
24 if got := Canonical(in); got != want {
25 t.Errorf("Canonical(%q) = %q, want %q", in, got, want)
26 }
27 }
28 // GO-012: the host ::1 on port 8443 is not the host ::1:8443.
29 if Canonical("https://[::1]:8443/x") == Canonical("https://[::1:8443]/x") {
30 t.Error("two different origins collide")
31 }
32 // Spellings of one origin agree.
33 for _, pair := range [][2]string{
34 {"https://a.test", "https://A.TEST:443/path"}, {"http://a.test:80/", "http://a.test"}, {"https://[::1]", "https://[::1]:443"},
35 } {
36 if Canonical(pair[0]) != Canonical(pair[1]) {
37 t.Errorf("%q and %q differ: %q %q", pair[0], pair[1], Canonical(pair[0]), Canonical(pair[1]))
38 }
39 }
40 // Other ports, schemes and hosts differ.
41 for _, pair := range [][2]string{
42 {"https://a.test", "http://a.test"}, {"https://a.test", "https://a.test:8443"}, {"https://a.test", "https://b.a.test"}, {"http://a.test", "http://a.test:443"},
43 } {
44 if Canonical(pair[0]) == Canonical(pair[1]) {
45 t.Errorf("%q and %q collide", pair[0], pair[1])
46 }
47 }
48}
49
50func TestPrefix(t *testing.T) {
51 for in, want := range map[string]string{
52 "https://Ermite.example:443/x": "https://ermite.example", "http://a.test:80": "http://a.test", "http://a.test:8088": "http://a.test:8088",
53 "https://[::1]:443": "https://[::1]", "http://[::1]:80/": "http://[::1]", "https://[::1]:8443": "https://[::1]:8443",
54 "http://10.0.2.2:8088": "http://10.0.2.2:8088", "https://a.test:80": "https://a.test:80", "ftp://x": "",
55 } {
56 if got := Prefix(in); got != want {
57 t.Errorf("Prefix(%q) = %q, want %q", in, got, want)
58 }
59 }
60}