-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathurls_test.go
More file actions
154 lines (148 loc) · 3.9 KB
/
Copy pathurls_test.go
File metadata and controls
154 lines (148 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package postmortems
import "testing"
func TestCanonicalURL(t *testing.T) {
t.Parallel()
cases := []struct {
name, in, want string
}{
{
name: "https + trim trailing slash",
in: "https://example.com/foo/",
want: "https://example.com/foo",
},
{
name: "lowercase host + drop www",
in: "https://WWW.Example.COM/Path",
want: "https://example.com/Path",
},
{
name: "http upgraded to https",
in: "http://example.com/path",
want: "https://example.com/path",
},
{
name: "wayback unwrapped",
in: "https://web.archive.org/web/20220124104632/https://example.com/foo/",
want: "https://example.com/foo",
},
{
name: "wayback unwrap with iframe flag",
in: "https://web.archive.org/web/20220124104632if_/https://example.com/foo",
want: "https://example.com/foo",
},
{
name: "fragment dropped",
in: "https://example.com/foo#section",
want: "https://example.com/foo",
},
{
name: "bare host",
in: "https://example.com/",
want: "https://example.com",
},
{
name: "preserves query",
in: "https://example.com/foo?q=1",
want: "https://example.com/foo?q=1",
},
{
name: "empty",
in: "",
want: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := CanonicalURL(tc.in); got != tc.want {
t.Errorf("CanonicalURL(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestURLsEquivalent(t *testing.T) {
t.Parallel()
if !URLsEquivalent(
"http://www.example.com/foo/",
"https://example.com/foo",
) {
t.Errorf("equal urls reported as different")
}
if !URLsEquivalent(
"https://web.archive.org/web/20220124104632/https://example.com/foo",
"https://example.com/foo",
) {
t.Errorf("wayback wrap should match unwrapped origin")
}
if URLsEquivalent("https://a.com/x", "https://b.com/x") {
t.Errorf("different hosts should not match")
}
if URLsEquivalent("", "https://example.com") {
t.Errorf("empty url should not match anything")
}
}
func TestParseWaybackURL(t *testing.T) {
t.Parallel()
cases := []struct {
in string
wantOK bool
wantOrigin string
wantSnapshot string
}{
{
in: "https://web.archive.org/web/20220124104632/https://example.com/foo",
wantOK: true,
wantOrigin: "https://example.com/foo",
wantSnapshot: "https://web.archive.org/web/20220124104632if_/https://example.com/foo",
},
{
in: "http://web.archive.org/web/20160610080136/https://www.example.org/x?q=1",
wantOK: true,
wantOrigin: "https://www.example.org/x?q=1",
wantSnapshot: "https://web.archive.org/web/20160610080136if_/https://www.example.org/x?q=1",
},
{
in: "https://web.archive.org/web/20220621124002if_/https://example.com/already-iframe",
wantOK: true,
wantOrigin: "https://example.com/already-iframe",
wantSnapshot: "https://web.archive.org/web/20220621124002if_/https://example.com/already-iframe",
},
{in: "https://example.com/", wantOK: false},
{in: "", wantOK: false},
}
for _, tc := range cases {
origin, snap, ok := ParseWaybackURL(tc.in)
if ok != tc.wantOK {
t.Errorf("ParseWaybackURL(%q) ok=%v, want %v", tc.in, ok, tc.wantOK)
continue
}
if !ok {
continue
}
if origin != tc.wantOrigin {
t.Errorf("origin = %q, want %q", origin, tc.wantOrigin)
}
if snap != tc.wantSnapshot {
t.Errorf("snapshot = %q, want %q", snap, tc.wantSnapshot)
}
}
}
func TestLooksLikeSingleURL(t *testing.T) {
t.Parallel()
cases := []struct {
in string
want bool
}{
{"https://example.com/foo", true},
{"http://example.com/foo?q=1", true},
{"https://example.com/foo), [see also](https://other.com/x", false},
{"not a url", false},
{"https://example.com/foo bar", false},
{"", false},
}
for _, tc := range cases {
if got := looksLikeSingleURL(tc.in); got != tc.want {
t.Errorf("looksLikeSingleURL(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}