1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | """ |
---|
4 | Unit tests for IS_URL() |
---|
5 | """ |
---|
6 | |
---|
7 | import unittest |
---|
8 | |
---|
9 | from pydal.validators import IS_URL, IS_HTTP_URL, IS_GENERIC_URL |
---|
10 | from pydal.validators import unicode_to_ascii_authority |
---|
11 | |
---|
12 | |
---|
13 | class TestIsUrl(unittest.TestCase): |
---|
14 | def testModeHttp(self): |
---|
15 | |
---|
16 | # defaults to mode='http' |
---|
17 | |
---|
18 | x = IS_URL() |
---|
19 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
20 | self.assertEqual(x("google.ca"), ("http://google.ca", None)) |
---|
21 | self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None)) |
---|
22 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
23 | self.assertEqual(x("google..ca"), ("google..ca", "Enter a valid URL")) |
---|
24 | self.assertEqual(x("google.ca.."), ("google.ca..", "Enter a valid URL")) |
---|
25 | |
---|
26 | # explicit use of 'http' mode |
---|
27 | |
---|
28 | x = IS_URL(mode="http") |
---|
29 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
30 | self.assertEqual(x("google.ca"), ("http://google.ca", None)) |
---|
31 | self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None)) |
---|
32 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
33 | |
---|
34 | # prepends 'https' instead of 'http' |
---|
35 | |
---|
36 | x = IS_URL(mode="http", prepend_scheme="https") |
---|
37 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
38 | self.assertEqual(x("google.ca"), ("https://google.ca", None)) |
---|
39 | self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None)) |
---|
40 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
41 | |
---|
42 | # prepending disabled |
---|
43 | |
---|
44 | x = IS_URL(prepend_scheme=None) |
---|
45 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
46 | self.assertEqual(x("google.ca"), ("google.ca", None)) |
---|
47 | self.assertEqual(x("google.ca:80"), ("google.ca:80", None)) |
---|
48 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
49 | |
---|
50 | # custom allowed_schemes |
---|
51 | |
---|
52 | x = IS_URL(mode="http", allowed_schemes=[None, "http"]) |
---|
53 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
54 | self.assertEqual( |
---|
55 | x("https://google.ca"), ("https://google.ca", "Enter a valid URL") |
---|
56 | ) |
---|
57 | self.assertEqual(x("google.ca"), ("http://google.ca", None)) |
---|
58 | self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None)) |
---|
59 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
60 | |
---|
61 | # custom allowed_schemes, excluding None |
---|
62 | |
---|
63 | x = IS_URL(allowed_schemes=["http"]) |
---|
64 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
65 | self.assertEqual( |
---|
66 | x("https://google.ca"), ("https://google.ca", "Enter a valid URL") |
---|
67 | ) |
---|
68 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
69 | self.assertEqual(x("google.ca:80"), ("google.ca:80", "Enter a valid URL")) |
---|
70 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
71 | |
---|
72 | # custom allowed_schemes and prepend_scheme |
---|
73 | |
---|
74 | x = IS_URL(allowed_schemes=[None, "https"], prepend_scheme="https") |
---|
75 | self.assertEqual( |
---|
76 | x("http://google.ca"), ("http://google.ca", "Enter a valid URL") |
---|
77 | ) |
---|
78 | self.assertEqual(x("https://google.ca"), ("https://google.ca", None)) |
---|
79 | self.assertEqual(x("google.ca"), ("https://google.ca", None)) |
---|
80 | self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None)) |
---|
81 | self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL")) |
---|
82 | |
---|
83 | # Now any URL requiring prepending will fail, but prepending is still |
---|
84 | # enabled! |
---|
85 | |
---|
86 | x = IS_URL(allowed_schemes=["http"]) |
---|
87 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
88 | |
---|
89 | def testModeGeneric(self): |
---|
90 | |
---|
91 | # 'generic' mode |
---|
92 | |
---|
93 | x = IS_URL(mode="generic") |
---|
94 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
95 | self.assertEqual(x("google.ca"), ("google.ca", None)) |
---|
96 | self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None)) |
---|
97 | self.assertEqual(x("blargg://unreal"), ("blargg://unreal", "Enter a valid URL")) |
---|
98 | |
---|
99 | # 'generic' mode with custom allowed_schemes that still includes |
---|
100 | # 'http' (the default for prepend_scheme) |
---|
101 | |
---|
102 | x = IS_URL(mode="generic", allowed_schemes=["http", "blargg"]) |
---|
103 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
104 | self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", "Enter a valid URL")) |
---|
105 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
106 | self.assertEqual(x("google.ca:80"), ("google.ca:80", "Enter a valid URL")) |
---|
107 | self.assertEqual(x("blargg://unreal"), ("blargg://unreal", None)) |
---|
108 | |
---|
109 | # 'generic' mode with overriden prepend_scheme |
---|
110 | |
---|
111 | x = IS_URL(mode="generic", prepend_scheme="ftp") |
---|
112 | self.assertEqual(x("http://google.ca"), ("http://google.ca", None)) |
---|
113 | self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", None)) |
---|
114 | self.assertEqual(x("google.ca"), ("google.ca", None)) |
---|
115 | self.assertEqual(x("google.ca:80"), ("ftp://google.ca:80", None)) |
---|
116 | self.assertEqual(x("blargg://unreal"), ("blargg://unreal", "Enter a valid URL")) |
---|
117 | |
---|
118 | # 'generic' mode with overriden allowed_schemes and prepend_scheme |
---|
119 | |
---|
120 | x = IS_URL( |
---|
121 | mode="generic", allowed_schemes=[None, "ftp", "ftps"], prepend_scheme="ftp" |
---|
122 | ) |
---|
123 | self.assertEqual( |
---|
124 | x("http://google.ca"), ("http://google.ca", "Enter a valid URL") |
---|
125 | ) |
---|
126 | self.assertEqual(x("google.ca"), ("google.ca", None)) |
---|
127 | self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", None)) |
---|
128 | self.assertEqual(x("google.ca:80"), ("ftp://google.ca:80", None)) |
---|
129 | self.assertEqual(x("blargg://unreal"), ("blargg://unreal", "Enter a valid URL")) |
---|
130 | |
---|
131 | # Now any URL requiring prepending will fail, but prepending is still |
---|
132 | # enabled! |
---|
133 | |
---|
134 | x = IS_URL(mode="generic", allowed_schemes=["http"]) |
---|
135 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
136 | |
---|
137 | def testExceptionalUse(self): |
---|
138 | |
---|
139 | # mode must be in set ['http', 'generic'] |
---|
140 | |
---|
141 | try: |
---|
142 | x = IS_URL(mode="ftp") |
---|
143 | x("http://www.google.ca") |
---|
144 | except Exception as e: |
---|
145 | if str(e) != "invalid mode 'ftp' in IS_URL": |
---|
146 | self.fail("Wrong exception: " + str(e)) |
---|
147 | else: |
---|
148 | self.fail("Accepted invalid mode: 'ftp'") |
---|
149 | |
---|
150 | # allowed_schemes in 'http' mode must be in set [None, 'http', 'https'] |
---|
151 | |
---|
152 | try: |
---|
153 | x = IS_URL(allowed_schemes=[None, "ftp", "ftps"], prepend_scheme="ftp") |
---|
154 | x("http://www.benn.ca") # we can only reasonably know about the |
---|
155 | # error at calling time |
---|
156 | except Exception as e: |
---|
157 | if str(e) != "allowed_scheme value 'ftp' is not in [None, 'http', 'https']": |
---|
158 | self.fail("Wrong exception: " + str(e)) |
---|
159 | else: |
---|
160 | self.fail("Accepted invalid allowed_schemes: [None, 'ftp', 'ftps']") |
---|
161 | |
---|
162 | # prepend_scheme's value must be in allowed_schemes (default for 'http' |
---|
163 | # mode is [None, 'http', 'https']) |
---|
164 | |
---|
165 | try: |
---|
166 | x = IS_URL(prepend_scheme="ftp") |
---|
167 | x("http://www.benn.ca") # we can only reasonably know about the |
---|
168 | # error at calling time |
---|
169 | except Exception as e: |
---|
170 | if ( |
---|
171 | str(e) |
---|
172 | != "prepend_scheme='ftp' is not in allowed_schemes=[None, 'http', 'https']" |
---|
173 | ): |
---|
174 | self.fail("Wrong exception: " + str(e)) |
---|
175 | else: |
---|
176 | self.fail("Accepted invalid prepend_scheme: 'ftp'") |
---|
177 | |
---|
178 | # custom allowed_schemes that excludes 'http', so prepend_scheme must be |
---|
179 | # specified! |
---|
180 | |
---|
181 | try: |
---|
182 | x = IS_URL(allowed_schemes=[None, "https"]) |
---|
183 | except Exception as e: |
---|
184 | if ( |
---|
185 | str(e) |
---|
186 | != "prepend_scheme='http' is not in allowed_schemes=[None, 'https']" |
---|
187 | ): |
---|
188 | self.fail("Wrong exception: " + str(e)) |
---|
189 | else: |
---|
190 | self.fail("Accepted invalid prepend_scheme: 'http'") |
---|
191 | |
---|
192 | # prepend_scheme must be in allowed_schemes |
---|
193 | |
---|
194 | try: |
---|
195 | x = IS_URL(allowed_schemes=[None, "http"], prepend_scheme="https") |
---|
196 | except Exception as e: |
---|
197 | if ( |
---|
198 | str(e) |
---|
199 | != "prepend_scheme='https' is not in allowed_schemes=[None, 'http']" |
---|
200 | ): |
---|
201 | self.fail("Wrong exception: " + str(e)) |
---|
202 | else: |
---|
203 | self.fail("Accepted invalid prepend_scheme: 'https'") |
---|
204 | |
---|
205 | # prepend_scheme's value (default is 'http') must be in allowed_schemes |
---|
206 | |
---|
207 | try: |
---|
208 | x = IS_URL(mode="generic", allowed_schemes=[None, "ftp", "ftps"]) |
---|
209 | except Exception as e: |
---|
210 | if ( |
---|
211 | str(e) |
---|
212 | != "prepend_scheme='http' is not in allowed_schemes=[None, 'ftp', 'ftps']" |
---|
213 | ): |
---|
214 | self.fail("Wrong exception: " + str(e)) |
---|
215 | else: |
---|
216 | self.fail("Accepted invalid prepend_scheme: 'http'") |
---|
217 | |
---|
218 | # prepend_scheme's value must be in allowed_schemes, which by default |
---|
219 | # is all schemes that really exist |
---|
220 | |
---|
221 | try: |
---|
222 | x = IS_URL(mode="generic", prepend_scheme="blargg") |
---|
223 | x("http://www.google.ca") |
---|
224 | # we can only reasonably know about the error at calling time |
---|
225 | except Exception as e: |
---|
226 | if not str(e).startswith( |
---|
227 | "prepend_scheme='blargg' is not in allowed_schemes=" |
---|
228 | ): |
---|
229 | self.fail("Wrong exception: " + str(e)) |
---|
230 | else: |
---|
231 | self.fail("Accepted invalid prepend_scheme: 'blargg'") |
---|
232 | |
---|
233 | # prepend_scheme's value must be in allowed_schemes |
---|
234 | |
---|
235 | try: |
---|
236 | x = IS_URL( |
---|
237 | mode="generic", allowed_schemes=[None, "http"], prepend_scheme="blargg" |
---|
238 | ) |
---|
239 | except Exception as e: |
---|
240 | if ( |
---|
241 | str(e) |
---|
242 | != "prepend_scheme='blargg' is not in allowed_schemes=[None, 'http']" |
---|
243 | ): |
---|
244 | self.fail("Wrong exception: " + str(e)) |
---|
245 | else: |
---|
246 | self.fail("Accepted invalid prepend_scheme: 'blargg'") |
---|
247 | |
---|
248 | # Not inluding None in the allowed_schemes essentially disabled |
---|
249 | # prepending, so even though |
---|
250 | # prepend_scheme has the invalid value 'http', we don't care! |
---|
251 | |
---|
252 | x = IS_URL(allowed_schemes=["https"], prepend_scheme="https") |
---|
253 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
254 | |
---|
255 | # Not inluding None in the allowed_schemes essentially disabled prepending, so even though |
---|
256 | # prepend_scheme has the invalid value 'http', we don't care! |
---|
257 | |
---|
258 | x = IS_URL(mode="generic", allowed_schemes=["https"], prepend_scheme="https") |
---|
259 | self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
260 | |
---|
261 | |
---|
262 | # ############################################################################## |
---|
263 | |
---|
264 | |
---|
265 | class TestIsGenericUrl(unittest.TestCase): |
---|
266 | |
---|
267 | x = IS_GENERIC_URL() |
---|
268 | |
---|
269 | def testInvalidUrls(self): |
---|
270 | urlsToCheckA = [] |
---|
271 | for i in list(range(0, 32)) + [127]: |
---|
272 | |
---|
273 | # Control characters are disallowed in any part of a URL |
---|
274 | |
---|
275 | urlsToCheckA.append("http://www.benn" + chr(i) + ".ca") |
---|
276 | |
---|
277 | urlsToCheckB = [ |
---|
278 | None, |
---|
279 | "", |
---|
280 | "http://www.no spaces allowed.com", |
---|
281 | "http://www.benn.ca/no spaces allowed/", |
---|
282 | "http://www.benn.ca/angle_<bracket/", |
---|
283 | "http://www.benn.ca/angle_>bracket/", |
---|
284 | "http://www.benn.ca/invalid%character", |
---|
285 | "http://www.benn.ca/illegal%%20use", |
---|
286 | "http://www.benn.ca/illegaluse%", |
---|
287 | "http://www.benn.ca/illegaluse%0", |
---|
288 | "http://www.benn.ca/illegaluse%x", |
---|
289 | "http://www.benn.ca/ill%egaluse%x", |
---|
290 | 'http://www.benn.ca/double"quote/', |
---|
291 | "http://www.curly{brace.com", |
---|
292 | "http://www.benn.ca/curly}brace/", |
---|
293 | "http://www.benn.ca/or|symbol/", |
---|
294 | "http://www.benn.ca/back\\slash", |
---|
295 | "http://www.benn.ca/the^carat", |
---|
296 | "http://left[bracket.me", |
---|
297 | "http://www.benn.ca/right]bracket", |
---|
298 | "http://www.benn.ca/angle`quote", |
---|
299 | "-ttp://www.benn.ca", |
---|
300 | "+ttp://www.benn.ca", |
---|
301 | ".ttp://www.benn.ca", |
---|
302 | "9ttp://www.benn.ca", |
---|
303 | "ht;tp://www.benn.ca", |
---|
304 | "ht@tp://www.benn.ca", |
---|
305 | "ht&tp://www.benn.ca", |
---|
306 | "ht=tp://www.benn.ca", |
---|
307 | "ht$tp://www.benn.ca", |
---|
308 | "ht,tp://www.benn.ca", |
---|
309 | "ht:tp://www.benn.ca", |
---|
310 | "htp://invalid_scheme.com", |
---|
311 | ] |
---|
312 | |
---|
313 | failures = [] |
---|
314 | |
---|
315 | for url in urlsToCheckA + urlsToCheckB: |
---|
316 | if self.x(url)[1] is None: |
---|
317 | failures.append("Incorrectly accepted: " + str(url)) |
---|
318 | |
---|
319 | if len(failures) > 0: |
---|
320 | self.fail(failures) |
---|
321 | |
---|
322 | def testValidUrls(self): |
---|
323 | urlsToCheck = [ |
---|
324 | "ftp://ftp.is.co.za/rfc/rfc1808.txt", |
---|
325 | "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles", |
---|
326 | "http://www.math.uio.no/faq/compression-faq/part1.html", |
---|
327 | "mailto:mduerst@ifi.unizh.ch", |
---|
328 | "news:comp.infosystems.www.servers.unix", |
---|
329 | "telnet://melvyl.ucop.edu/", |
---|
330 | "hTTp://www.benn.ca", |
---|
331 | "%66%74%70://ftp.is.co.za/rfc/rfc1808.txt", |
---|
332 | "%46%74%70://ftp.is.co.za/rfc/rfc1808.txt", |
---|
333 | "/faq/compression-faq/part1.html", |
---|
334 | "google.com", |
---|
335 | "www.google.com:8080", |
---|
336 | "128.127.123.250:8080", |
---|
337 | "blargg:ping", |
---|
338 | "http://www.benn.ca", |
---|
339 | "http://benn.ca", |
---|
340 | "http://amazon.com/books/", |
---|
341 | "https://amazon.com/movies", |
---|
342 | "rtsp://idontknowthisprotocol", |
---|
343 | "HTTP://allcaps.com", |
---|
344 | "http://localhost", |
---|
345 | "http://localhost#fragment", |
---|
346 | "http://localhost/hello", |
---|
347 | "http://localhost/hello?query=True", |
---|
348 | "http://localhost/hello/", |
---|
349 | "http://localhost:8080", |
---|
350 | "http://localhost:8080/", |
---|
351 | "http://localhost:8080/hello", |
---|
352 | "http://localhost:8080/hello/", |
---|
353 | "file:///C:/Documents%20and%20Settings/Jonathan/Desktop/view.py", |
---|
354 | ] |
---|
355 | |
---|
356 | failures = [] |
---|
357 | |
---|
358 | for url in urlsToCheck: |
---|
359 | if self.x(url)[1] is not None: |
---|
360 | failures.append("Incorrectly rejected: " + str(url)) |
---|
361 | |
---|
362 | if len(failures) > 0: |
---|
363 | self.fail(failures) |
---|
364 | |
---|
365 | def testPrepending(self): |
---|
366 | # Does not prepend scheme for abbreviated domains |
---|
367 | self.assertEqual(self.x("google.ca"), ("google.ca", None)) |
---|
368 | |
---|
369 | # Does not prepend scheme for abbreviated domains |
---|
370 | self.assertEqual(self.x("google.ca:8080"), ("google.ca:8080", None)) |
---|
371 | |
---|
372 | # Does not prepend when scheme already exists |
---|
373 | self.assertEqual(self.x("https://google.ca"), ("https://google.ca", None)) |
---|
374 | |
---|
375 | # Does not prepend if None type is not specified in allowed_scheme, |
---|
376 | # because a scheme is required |
---|
377 | |
---|
378 | y = IS_GENERIC_URL(allowed_schemes=["http", "blargg"], prepend_scheme="http") |
---|
379 | self.assertEqual(y("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
380 | |
---|
381 | |
---|
382 | # ############################################################################## |
---|
383 | |
---|
384 | |
---|
385 | class TestIsHttpUrl(unittest.TestCase): |
---|
386 | |
---|
387 | x = IS_HTTP_URL() |
---|
388 | |
---|
389 | def testInvalidUrls(self): |
---|
390 | urlsToCheck = [ |
---|
391 | None, |
---|
392 | "", |
---|
393 | "http://invalid" + chr(2) + ".com", |
---|
394 | "htp://invalid_scheme.com", |
---|
395 | "blargg://invalid_scheme.com", |
---|
396 | "http://-123.com", |
---|
397 | "http://abcd-.ca", |
---|
398 | "http://-abc123-.me", |
---|
399 | "http://www.dom&ain.com/", |
---|
400 | "http://www.dom=ain.com/", |
---|
401 | "http://www.benn.ca&", |
---|
402 | "http://%62%65%6E%6E%2E%63%61/path", |
---|
403 | "http://.domain.com", |
---|
404 | "http://.domain.com./path", |
---|
405 | "http://domain..com", |
---|
406 | "http://domain...at..com", |
---|
407 | "http://domain.com..", |
---|
408 | "http://domain.com../path", |
---|
409 | "http://domain.3m", |
---|
410 | "http://domain.-3m", |
---|
411 | "http://domain.3m-", |
---|
412 | "http://domain.-3m-", |
---|
413 | "http://domain.co&m", |
---|
414 | "http://domain.m3456", |
---|
415 | "http://domain.m-3/path#fragment", |
---|
416 | "http://domain.m---k/path?query=value", |
---|
417 | "http://23.32..", |
---|
418 | "http://23..32.56.0", |
---|
419 | "http://38997.222.999", |
---|
420 | "http://23.32.56.99.", |
---|
421 | "http://.23.32.56.99", |
---|
422 | "http://.23.32.56.99.", |
---|
423 | "http://w127.123.0.256:8080", |
---|
424 | "http://23.32.56.99:abcd", |
---|
425 | "http://23.32.56.99:23cd", |
---|
426 | "http://google.com:cd22", |
---|
427 | "http://23.32:1300.56.99", |
---|
428 | "http://www.yahoo:1600.com", |
---|
429 | "path/segment/without/starting/slash", |
---|
430 | "http://www.math.uio.no;param=3", |
---|
431 | "://ABC.com:/%7esmith/home.html", |
---|
432 | ] |
---|
433 | |
---|
434 | failures = [] |
---|
435 | |
---|
436 | for url in urlsToCheck: |
---|
437 | if self.x(url)[1] is None: |
---|
438 | failures.append("Incorrectly accepted: " + str(url)) |
---|
439 | |
---|
440 | if len(failures) > 0: |
---|
441 | self.fail(failures) |
---|
442 | |
---|
443 | def testValidUrls(self): |
---|
444 | |
---|
445 | urlsToCheck = [ |
---|
446 | "http://abc.com:80/~smith/home.html", |
---|
447 | "http://ABC.com/%7Esmith/home.html", |
---|
448 | "http://ABC.com:/%7esmith/home.html", |
---|
449 | "http://www.math.uio.no/faq/compression-faq/part1.html", |
---|
450 | "//google.ca/faq/compression-faq/part1.html", |
---|
451 | "//google.ca/faq;param=3", |
---|
452 | "//google.ca/faq/index.html?query=5", |
---|
453 | "//google.ca/faq/index.html;param=value?query=5", |
---|
454 | "/faq/compression-faq/part1.html", |
---|
455 | "/faq;param=3", |
---|
456 | "/faq/index.html?query=5", |
---|
457 | "/faq/index.html;param=value?query=5", |
---|
458 | "google.com", |
---|
459 | "benn.ca/init/default", |
---|
460 | "benn.ca/init;param=value/default?query=value", |
---|
461 | "http://host-name---with-dashes.me", |
---|
462 | "http://www.host-name---with-dashes.me", |
---|
463 | "http://a.com", |
---|
464 | "http://a.3.com", |
---|
465 | "http://a.bl-ck.com", |
---|
466 | "http://bl-e.b.com", |
---|
467 | "http://host123with456numbers.ca", |
---|
468 | "http://1234567890.com.", |
---|
469 | "http://1234567890.com./path", |
---|
470 | "http://google.com./path", |
---|
471 | "http://domain.xn--d1acj3b", |
---|
472 | "http://127.123.0.256", |
---|
473 | "http://127.123.0.256/document/drawer", |
---|
474 | "127.123.0.256/document/", |
---|
475 | "156.212.123.100", |
---|
476 | "http://www.google.com:180200", |
---|
477 | "http://www.google.com:8080/path", |
---|
478 | "http://www.google.com:8080", |
---|
479 | "//www.google.com:8080", |
---|
480 | "www.google.com:8080", |
---|
481 | "http://127.123.0.256:8080/path", |
---|
482 | "//127.123.0.256:8080", |
---|
483 | "127.123.0.256:8080", |
---|
484 | "http://example.me??query=value?", |
---|
485 | "http://a.com", |
---|
486 | "http://3.com", |
---|
487 | "http://www.benn.ca", |
---|
488 | "http://benn.ca", |
---|
489 | "http://amazon.com/books/", |
---|
490 | "https://amazon.com/movies", |
---|
491 | "hTTp://allcaps.com", |
---|
492 | "http://localhost", |
---|
493 | "HTTPS://localhost.", |
---|
494 | "http://localhost#fragment", |
---|
495 | "http://localhost/hello;param=value", |
---|
496 | "http://localhost/hello;param=value/hi;param2=value2;param3=value3", |
---|
497 | "http://localhost/hello?query=True", |
---|
498 | "http://www.benn.ca/hello;param=value/hi;param2=value2;param3=value3/index.html?query=3", |
---|
499 | "http://localhost/hello/?query=1500&five=6", |
---|
500 | "http://localhost:8080", |
---|
501 | "http://localhost:8080/", |
---|
502 | "http://localhost:8080/hello", |
---|
503 | "http://localhost:8080/hello%20world/", |
---|
504 | "http://www.a.3.be-nn.5.ca", |
---|
505 | "http://www.amazon.COM", |
---|
506 | ] |
---|
507 | |
---|
508 | failures = [] |
---|
509 | |
---|
510 | for url in urlsToCheck: |
---|
511 | if self.x(url)[1] is not None: |
---|
512 | failures.append("Incorrectly rejected: " + str(url)) |
---|
513 | |
---|
514 | if len(failures) > 0: |
---|
515 | self.fail(failures) |
---|
516 | |
---|
517 | def testPrepending(self): |
---|
518 | # prepends scheme for abbreviated domains |
---|
519 | self.assertEqual(self.x("google.ca"), ("http://google.ca", None)) |
---|
520 | |
---|
521 | # prepends scheme for abbreviated domains |
---|
522 | self.assertEqual(self.x("google.ca:8080"), ("http://google.ca:8080", None)) |
---|
523 | |
---|
524 | # does not prepend when scheme already exists |
---|
525 | self.assertEqual(self.x("https://google.ca"), ("https://google.ca", None)) |
---|
526 | |
---|
527 | y = IS_HTTP_URL(prepend_scheme="https", allowed_schemes=[None, "https"]) |
---|
528 | self.assertEqual( |
---|
529 | y("google.ca"), ("https://google.ca", None) |
---|
530 | ) # prepends https if asked |
---|
531 | |
---|
532 | z = IS_HTTP_URL(prepend_scheme=None) |
---|
533 | self.assertEqual( |
---|
534 | z("google.ca:8080"), ("google.ca:8080", None) |
---|
535 | ) # prepending disabled |
---|
536 | |
---|
537 | try: |
---|
538 | IS_HTTP_URL(prepend_scheme="mailto") |
---|
539 | except Exception as e: |
---|
540 | if ( |
---|
541 | str(e) |
---|
542 | != "prepend_scheme='mailto' is not in allowed_schemes=[None, 'http', 'https']" |
---|
543 | ): |
---|
544 | self.fail("Wrong exception: " + str(e)) |
---|
545 | else: |
---|
546 | self.fail("Got invalid prepend_scheme: 'mailto'") |
---|
547 | |
---|
548 | # Does not prepend if None type is not specified in allowed_scheme, because a scheme is required |
---|
549 | |
---|
550 | a = IS_HTTP_URL(allowed_schemes=["http"]) |
---|
551 | self.assertEqual(a("google.ca"), ("google.ca", "Enter a valid URL")) |
---|
552 | self.assertEqual(a("google.ca:80"), ("google.ca:80", "Enter a valid URL")) |
---|
553 | |
---|
554 | |
---|
555 | class TestUnicode(unittest.TestCase): |
---|
556 | x = IS_URL() |
---|
557 | y = IS_URL(allowed_schemes=["https"], prepend_scheme="https") |
---|
558 | # excludes the option for abbreviated URLs with no scheme |
---|
559 | z = IS_URL(prepend_scheme=None) |
---|
560 | # disables prepending the scheme in the return value |
---|
561 | |
---|
562 | def testUnicodeToAsciiUrl(self): |
---|
563 | self.assertEqual( |
---|
564 | unicode_to_ascii_authority(u"www.Alliancefran\xe7aise.nu"), |
---|
565 | "www.xn--alliancefranaise-npb.nu", |
---|
566 | ) |
---|
567 | self.assertEqual(unicode_to_ascii_authority(u"www.benn.ca"), "www.benn.ca") |
---|
568 | self.assertRaises( |
---|
569 | UnicodeError, unicode_to_ascii_authority, u"\u4e2d" * 1000 |
---|
570 | ) # label is too long |
---|
571 | |
---|
572 | def testValidUrls(self): |
---|
573 | self.assertEqual( |
---|
574 | self.x(u"www.Alliancefrancaise.nu"), |
---|
575 | ("http://www.Alliancefrancaise.nu", None), |
---|
576 | ) |
---|
577 | self.assertEqual( |
---|
578 | self.x(u"www.Alliancefran\xe7aise.nu"), |
---|
579 | ("http://www.xn--alliancefranaise-npb.nu", None), |
---|
580 | ) |
---|
581 | self.assertEqual( |
---|
582 | self.x(u"www.Alliancefran\xe7aise.nu:8080"), |
---|
583 | ("http://www.xn--alliancefranaise-npb.nu:8080", None), |
---|
584 | ) |
---|
585 | self.assertEqual( |
---|
586 | self.x(u"http://www.Alliancefran\xe7aise.nu"), |
---|
587 | ("http://www.xn--alliancefranaise-npb.nu", None), |
---|
588 | ) |
---|
589 | self.assertEqual( |
---|
590 | self.x(u"http://www.Alliancefran\xe7aise.nu/parnaise/blue"), |
---|
591 | ("http://www.xn--alliancefranaise-npb.nu/parnaise/blue", None), |
---|
592 | ) |
---|
593 | self.assertEqual( |
---|
594 | self.x(u"http://www.Alliancefran\xe7aise.nu/parnaise/blue#fragment"), |
---|
595 | ("http://www.xn--alliancefranaise-npb.nu/parnaise/blue#fragment", None), |
---|
596 | ) |
---|
597 | self.assertEqual( |
---|
598 | self.x( |
---|
599 | u"http://www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment" |
---|
600 | ), |
---|
601 | ( |
---|
602 | "http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment", |
---|
603 | None, |
---|
604 | ), |
---|
605 | ) |
---|
606 | self.assertEqual( |
---|
607 | self.x( |
---|
608 | u"http://www.Alliancefran\xe7aise.nu:8080/parnaise/blue?query=value#fragment" |
---|
609 | ), |
---|
610 | ( |
---|
611 | "http://www.xn--alliancefranaise-npb.nu:8080/parnaise/blue?query=value#fragment", |
---|
612 | None, |
---|
613 | ), |
---|
614 | ) |
---|
615 | self.assertEqual( |
---|
616 | self.x(u"www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment"), |
---|
617 | ( |
---|
618 | "http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment", |
---|
619 | None, |
---|
620 | ), |
---|
621 | ) |
---|
622 | self.assertEqual( |
---|
623 | self.x(u"http://\u4e2d\u4fd4.com"), ("http://xn--fiq13b.com", None) |
---|
624 | ) |
---|
625 | self.assertEqual( |
---|
626 | self.x(u"http://\u4e2d\u4fd4.com/\u4e86"), |
---|
627 | ("http://xn--fiq13b.com/%4e%86", None), |
---|
628 | ) |
---|
629 | self.assertEqual( |
---|
630 | self.x(u"http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86"), |
---|
631 | ("http://xn--fiq13b.com/%4e%86?query=%4e%86", None), |
---|
632 | ) |
---|
633 | self.assertEqual( |
---|
634 | self.x(u"http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86#fragment"), |
---|
635 | ("http://xn--fiq13b.com/%4e%86?query=%4e%86#fragment", None), |
---|
636 | ) |
---|
637 | self.assertEqual( |
---|
638 | self.x(u"http://\u4e2d\u4fd4.com?query=\u4e86#fragment"), |
---|
639 | ("http://xn--fiq13b.com?query=%4e%86#fragment", None), |
---|
640 | ) |
---|
641 | self.assertEqual( |
---|
642 | self.x(u"http://B\xfccher.ch"), ("http://xn--bcher-kva.ch", None) |
---|
643 | ) |
---|
644 | self.assertEqual( |
---|
645 | self.x(u"http://\xe4\xf6\xfc\xdf.com"), ("http://xn--ss-uia6e4a.com", None) |
---|
646 | ) |
---|
647 | self.assertEqual( |
---|
648 | self.x(u"http://visegr\xe1d.com"), ("http://xn--visegrd-mwa.com", None) |
---|
649 | ) |
---|
650 | self.assertEqual( |
---|
651 | self.x(u"http://h\xe1zipatika.com"), ("http://xn--hzipatika-01a.com", None) |
---|
652 | ) |
---|
653 | self.assertEqual( |
---|
654 | self.x(u"http://www.\xe7ukurova.com"), |
---|
655 | ("http://www.xn--ukurova-txa.com", None), |
---|
656 | ) |
---|
657 | self.assertEqual( |
---|
658 | self.x(u"http://nixier\xf6hre.nixieclock-tube.com"), |
---|
659 | ("http://xn--nixierhre-57a.nixieclock-tube.com", None), |
---|
660 | ) |
---|
661 | self.assertEqual(self.x(u"google.ca."), ("http://google.ca.", None)) |
---|
662 | |
---|
663 | self.assertEqual(self.y(u"https://google.ca"), ("https://google.ca", None)) |
---|
664 | self.assertEqual( |
---|
665 | self.y(u"https://\u4e2d\u4fd4.com"), ("https://xn--fiq13b.com", None) |
---|
666 | ) |
---|
667 | |
---|
668 | self.assertEqual(self.z(u"google.ca"), ("google.ca", None)) |
---|
669 | |
---|
670 | def testInvalidUrls(self): |
---|
671 | self.assertEqual(self.x(u"://ABC.com"), (u"://ABC.com", "Enter a valid URL")) |
---|
672 | self.assertEqual( |
---|
673 | self.x(u"http://\u4e2d\u4fd4.dne"), |
---|
674 | (u"http://\u4e2d\u4fd4.dne", "Enter a valid URL"), |
---|
675 | ) |
---|
676 | self.assertEqual( |
---|
677 | self.x(u"https://google.dne"), (u"https://google.dne", "Enter a valid URL") |
---|
678 | ) |
---|
679 | self.assertEqual( |
---|
680 | self.x(u"https://google..ca"), (u"https://google..ca", "Enter a valid URL") |
---|
681 | ) |
---|
682 | self.assertEqual(self.x(u"google..ca"), (u"google..ca", "Enter a valid URL")) |
---|
683 | self.assertEqual( |
---|
684 | self.x(u"http://" + u"\u4e2d" * 1000 + u".com"), |
---|
685 | (u"http://" + u"\u4e2d" * 1000 + u".com", "Enter a valid URL"), |
---|
686 | ) |
---|
687 | |
---|
688 | self.assertEqual( |
---|
689 | self.x(u"http://google.com#fragment_\u4e86"), |
---|
690 | (u"http://google.com#fragment_\u4e86", "Enter a valid URL"), |
---|
691 | ) |
---|
692 | self.assertEqual( |
---|
693 | self.x(u"http\u4e86://google.com"), |
---|
694 | (u"http\u4e86://google.com", "Enter a valid URL"), |
---|
695 | ) |
---|
696 | self.assertEqual( |
---|
697 | self.x(u"http\u4e86://google.com#fragment_\u4e86"), |
---|
698 | (u"http\u4e86://google.com#fragment_\u4e86", "Enter a valid URL"), |
---|
699 | ) |
---|
700 | |
---|
701 | self.assertEqual( |
---|
702 | self.y(u"http://\u4e2d\u4fd4.com/\u4e86"), |
---|
703 | (u"http://\u4e2d\u4fd4.com/\u4e86", "Enter a valid URL"), |
---|
704 | ) |
---|
705 | # self.assertEqual(self.y(u'google.ca'), (u'google.ca', 'Enter a valid URL')) |
---|
706 | |
---|
707 | self.assertEqual( |
---|
708 | self.z(u"invalid.domain..com"), |
---|
709 | (u"invalid.domain..com", "Enter a valid URL"), |
---|
710 | ) |
---|
711 | self.assertEqual( |
---|
712 | self.z(u"invalid.\u4e2d\u4fd4.blargg"), |
---|
713 | (u"invalid.\u4e2d\u4fd4.blargg", "Enter a valid URL"), |
---|
714 | ) |
---|
715 | |
---|
716 | |
---|
717 | # ############################################################################## |
---|
718 | |
---|
719 | |
---|
720 | class TestSimple(unittest.TestCase): |
---|
721 | def test_IS_URL(self): |
---|
722 | rtn = IS_URL()("abc.com") |
---|
723 | self.assertEqual(rtn, ("http://abc.com", None)) |
---|
724 | rtn = IS_URL(mode="generic")("abc.com") |
---|
725 | self.assertEqual(rtn, ("abc.com", None)) |
---|
726 | rtn = IS_URL(allowed_schemes=["https"], prepend_scheme="https")( |
---|
727 | "https://abc.com" |
---|
728 | ) |
---|
729 | self.assertEqual(rtn, ("https://abc.com", None)) |
---|
730 | rtn = IS_URL(prepend_scheme="https")("abc.com") |
---|
731 | self.assertEqual(rtn, ("https://abc.com", None)) |
---|
732 | rtn = IS_URL( |
---|
733 | mode="generic", allowed_schemes=["ftps", "https"], prepend_scheme="https" |
---|
734 | )("https://abc.com") |
---|
735 | self.assertEqual(rtn, ("https://abc.com", None)) |
---|
736 | rtn = IS_URL( |
---|
737 | mode="generic", |
---|
738 | allowed_schemes=["ftps", "https", None], |
---|
739 | prepend_scheme="https", |
---|
740 | )("abc.com") |
---|
741 | self.assertEqual(rtn, ("abc.com", None)) |
---|
742 | # regression test for issue 773 |
---|
743 | rtn = IS_URL()("domain.ninja") |
---|
744 | self.assertEqual(rtn, ("http://domain.ninja", None)) |
---|
745 | # addition of allowed_tlds |
---|
746 | rtn = IS_URL(allowed_tlds=["com", "net", "org"])("domain.ninja") |
---|
747 | self.assertEqual(rtn, ("domain.ninja", "Enter a valid URL")) |
---|
748 | # mode = 'generic' doesn't consider allowed_tlds |
---|
749 | rtn = IS_URL(mode="generic", allowed_tlds=["com", "net", "org"])("domain.ninja") |
---|
750 | self.assertEqual(rtn, ("domain.ninja", None)) |
---|