|
1
|
|
|
package validate_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation/validate" |
|
7
|
|
|
"github.com/stretchr/testify/assert" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
func TestURL_WhenDefaultProtocolsAndValidURL_ExpectNoError(t *testing.T) { |
|
11
|
|
|
for _, url := range validURLs() { |
|
12
|
|
|
t.Run(url, func(t *testing.T) { |
|
13
|
|
|
err := validate.URL(url) |
|
14
|
|
|
|
|
15
|
|
|
assert.NoError(t, err) |
|
16
|
|
|
}) |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
func TestURL_WhenDefaultProtocolsAndInvalidURL_ExpectError(t *testing.T) { |
|
21
|
|
|
for _, url := range invalidURLs() { |
|
22
|
|
|
t.Run(url, func(t *testing.T) { |
|
23
|
|
|
err := validate.URL(url) |
|
24
|
|
|
|
|
25
|
|
|
assert.Error(t, err) |
|
26
|
|
|
}) |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
func TestURL_WhenCustomProtocolsAndValidURL_ExpectNoError(t *testing.T) { |
|
31
|
|
|
for _, url := range validURLsWithCustomProtocols() { |
|
32
|
|
|
t.Run(url, func(t *testing.T) { |
|
33
|
|
|
err := validate.URL(url, "ftp", "file", "git") |
|
34
|
|
|
|
|
35
|
|
|
assert.NoError(t, err) |
|
36
|
|
|
}) |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
func TestRelativeURL_WhenDefaultProtocolsAndValidURL_ExpectNoError(t *testing.T) { |
|
41
|
|
|
urls := append(validURLs(), validRelativeURLs()...) |
|
42
|
|
|
for _, url := range urls { |
|
43
|
|
|
t.Run(url, func(t *testing.T) { |
|
44
|
|
|
err := validate.RelativeURL(url) |
|
45
|
|
|
|
|
46
|
|
|
assert.NoError(t, err) |
|
47
|
|
|
}) |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
func TestRelativeURL_WhenDefaultProtocolsAndInvalidURL_ExpectError(t *testing.T) { |
|
52
|
|
|
urls := append(invalidURLs(), invalidRelativeURLs()...) |
|
53
|
|
|
for _, url := range urls { |
|
54
|
|
|
t.Run(url, func(t *testing.T) { |
|
55
|
|
|
err := validate.RelativeURL(url) |
|
56
|
|
|
|
|
57
|
|
|
assert.Error(t, err) |
|
58
|
|
|
}) |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
func TestRelativeURL_WhenCustomProtocolsAndValidURL_ExpectNoError(t *testing.T) { |
|
63
|
|
|
for _, url := range validURLsWithCustomProtocols() { |
|
64
|
|
|
t.Run(url, func(t *testing.T) { |
|
65
|
|
|
err := validate.RelativeURL(url, "ftp", "file", "git") |
|
66
|
|
|
|
|
67
|
|
|
assert.NoError(t, err) |
|
68
|
|
|
}) |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func validURLs() []string { |
|
73
|
|
|
return []string{ |
|
74
|
|
|
"http://a.pl", |
|
75
|
|
|
"http://www.example.com", |
|
76
|
|
|
"HTTP://WWW.EXAMPLE.COM", |
|
77
|
|
|
"http://www.example.com.", |
|
78
|
|
|
"http://www.example.museum", |
|
79
|
|
|
"https://example.com/", |
|
80
|
|
|
"https://example.com:80/", |
|
81
|
|
|
"http://examp_le.com", |
|
82
|
|
|
"http://www.sub_domain.examp_le.com", |
|
83
|
|
|
"http://www.example.coop/", |
|
84
|
|
|
"http://www.test-example.com/", |
|
85
|
|
|
"http://www.example.com/", |
|
86
|
|
|
"http://example.fake/blog/", |
|
87
|
|
|
"http://example.com/?", |
|
88
|
|
|
"http://example.com/search?type=&q=url+validator", |
|
89
|
|
|
"http://example.com/#", |
|
90
|
|
|
"http://example.com/#?", |
|
91
|
|
|
"http://www.example.com/doc/current/book/validation.html#supported-constraints", |
|
92
|
|
|
"http://very.long.domain.name.com/", |
|
93
|
|
|
"http://localhost/", |
|
94
|
|
|
"http://myhost123/", |
|
95
|
|
|
"http://127.0.0.1/", |
|
96
|
|
|
"http://127.0.0.1:80/", |
|
97
|
|
|
"http://[::1]/", |
|
98
|
|
|
"http://[::1]:80/", |
|
99
|
|
|
"http://[1:2:3::4:5:6:7]/", |
|
100
|
|
|
"http://sãopaulo.com/", |
|
101
|
|
|
"http://xn--sopaulo-xwa.com/", |
|
102
|
|
|
"http://sãopaulo.com.br/", |
|
103
|
|
|
"http://xn--sopaulo-xwa.com.br/", |
|
104
|
|
|
"http://пример.испытание/", |
|
105
|
|
|
"http://xn--e1afmkfd.xn--80akhbyknj4f/", |
|
106
|
|
|
"http://مثال.إختبار/", |
|
107
|
|
|
"http://xn--mgbh0fb.xn--kgbechtv/", |
|
108
|
|
|
"http://例子.测试/", |
|
109
|
|
|
"http://xn--fsqu00a.xn--0zwm56d/", |
|
110
|
|
|
"http://例子.測試/", |
|
111
|
|
|
"http://xn--fsqu00a.xn--g6w251d/", |
|
112
|
|
|
"http://例え.テスト/", |
|
113
|
|
|
"http://xn--r8jz45g.xn--zckzah/", |
|
114
|
|
|
"http://مثال.آزمایشی/", |
|
115
|
|
|
"http://xn--mgbh0fb.xn--hgbk6aj7f53bba/", |
|
116
|
|
|
"http://실례.테스트/", |
|
117
|
|
|
"http://xn--9n2bp8q.xn--9t4b11yi5a/", |
|
118
|
|
|
"http://العربية.idn.icann.org/", |
|
119
|
|
|
"http://xn--ogb.idn.icann.org/", |
|
120
|
|
|
"http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/", |
|
121
|
|
|
"http://xn--espaa-rta.xn--ca-ol-fsay5a/", |
|
122
|
|
|
"http://xn--d1abbgf6aiiy.xn--p1ai/", |
|
123
|
|
|
"http://☎.com/", |
|
124
|
|
|
"http://username:[email protected]", |
|
125
|
|
|
"http://user.name:[email protected]", |
|
126
|
|
|
"http://user_name:[email protected]", |
|
127
|
|
|
"http://username:[email protected]", |
|
128
|
|
|
"http://user.name:[email protected]", |
|
129
|
|
|
"http://[email protected]", |
|
130
|
|
|
"http://[email protected]", |
|
131
|
|
|
"http://u%24er:[email protected]", |
|
132
|
|
|
"http://user:pa%24%[email protected]", |
|
133
|
|
|
"http://example.com?", |
|
134
|
|
|
"http://example.com?query=1", |
|
135
|
|
|
"http://example.com/?query=1", |
|
136
|
|
|
"http://example.com/?querie%24=1", |
|
137
|
|
|
"http://example.com/path%24/?query=1", |
|
138
|
|
|
"http://example.com#", |
|
139
|
|
|
"http://example.com#fragment", |
|
140
|
|
|
"http://example.com/#fragment", |
|
141
|
|
|
"http://example.com/#one_more%20test", |
|
142
|
|
|
"http://example.com/exploit.html?hello[0]=test", |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
func validRelativeURLs() []string { |
|
147
|
|
|
return []string{ |
|
148
|
|
|
"//example.com", |
|
149
|
|
|
"//examp_le.com", |
|
150
|
|
|
"//example.fake/blog/", |
|
151
|
|
|
"//example.com/search?type=&q=url+validator", |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
func invalidURLs() []string { |
|
156
|
|
|
return []string{ |
|
157
|
|
|
"", |
|
158
|
|
|
"example.com", |
|
159
|
|
|
"://example.com", |
|
160
|
|
|
"http ://example.com", |
|
161
|
|
|
"http:/example.com", |
|
162
|
|
|
"http://example.com::aa", |
|
163
|
|
|
"http://example.com:aa", |
|
164
|
|
|
"ftp://example.fr", |
|
165
|
|
|
"faked://example.fr", |
|
166
|
|
|
"http://127.0.0.1:aa/", |
|
167
|
|
|
"ftp://[::1]/", |
|
168
|
|
|
"http://[::1", |
|
169
|
|
|
"http://hello.☎/", |
|
170
|
|
|
"http://:[email protected]", |
|
171
|
|
|
"http://:password@@example.com", |
|
172
|
|
|
"http://username:passwordexample.com", |
|
173
|
|
|
"http://usern@me:[email protected]", |
|
174
|
|
|
"http://nota%hex:[email protected]", |
|
175
|
|
|
"http://username:nota%[email protected]", |
|
176
|
|
|
"http://example.com/exploit.html?<script>alert(1);</script>", |
|
177
|
|
|
"http://example.com/exploit.html?hel lo", |
|
178
|
|
|
"http://example.com/exploit.html?not_a%hex", |
|
179
|
|
|
"http://", |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
func invalidRelativeURLs() []string { |
|
184
|
|
|
return []string{ |
|
185
|
|
|
"/example.com", |
|
186
|
|
|
"//example.com::aa", |
|
187
|
|
|
"//example.com:aa", |
|
188
|
|
|
"//127.0.0.1:aa/", |
|
189
|
|
|
"//[::1", |
|
190
|
|
|
"//hello.☎/", |
|
191
|
|
|
"//:[email protected]", |
|
192
|
|
|
"//:password@@example.com", |
|
193
|
|
|
"//username:passwordexample.com", |
|
194
|
|
|
"//usern@me:[email protected]", |
|
195
|
|
|
"//example.com/exploit.html?<script>alert(1);</script>", |
|
196
|
|
|
"//example.com/exploit.html?hel lo", |
|
197
|
|
|
"//example.com/exploit.html?not_a%hex", |
|
198
|
|
|
"//", |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
func validURLsWithCustomProtocols() []string { |
|
203
|
|
|
return []string{ |
|
204
|
|
|
"ftp://example.com", |
|
205
|
|
|
"file://127.0.0.1", |
|
206
|
|
|
"git://[::1]/", |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|