-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurlCleanerTest.py
More file actions
92 lines (73 loc) · 3.07 KB
/
urlCleanerTest.py
File metadata and controls
92 lines (73 loc) · 3.07 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
# -*- coding: utf-8 -*-
import unittest
from urlCleaner import UrlCleaner
class TestUrlCleaner(unittest.TestCase):
def testCleanASimpleUrl(self):
assert urlCleaner.clean('http://www.wikipedia.org/') == 'http://www.wikipedia.org/'
def testCleanASimpleHttpsUrl(self):
assert urlCleaner.clean('https://wikipedia.org/') == 'https://wikipedia.org/'
def testCleanALongVerboseUrl(self):
assert urlCleaner.clean('http://www.google.fr/search?sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=url+injecton#sclient=psy&hl=fr&client=ubuntu&hs=dKK&channel=cs&source=hp&q=app+engine+sanitize&aq=f&aqi=&aql=&oq=&pbx=1&fp=ad548885dc40634') == 'http://www.google.fr/search?sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=url+injecton#sclient=psy&hl=fr&client=ubuntu&hs=dKK&channel=cs&source=hp&q=app+engine+sanitize&aq=f&aqi=&aql=&oq=&pbx=1&fp=ad548885dc40634'
def testCleanAStrangeUrl(self):
assert urlCleaner.clean('http://www.cwi.nl:80/%7Eguido/Python.html') == 'http://www.cwi.nl:80/%7Eguido/Python.html'
def testCleanABadUrl(self):
assert urlCleaner.clean('http://"\'<>test') == 'http://test'
def testThrowAnErrorIfNotAnHttpUrl(self):
try:
urlCleaner.clean('ftp://someserver')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Bad protocol. It should start with "http://" or "https://"'
def testThrowAnErrorIfUrlIsLocalhost(self):
try:
urlCleaner.clean('http://localhost/some/path')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to localhost'
def testThrowAnErrorIfUrlIsLocalhostOnOtherPort(self):
try:
urlCleaner.clean('http://localhost:8080/some/path')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to localhost'
def testThrowAnErrorIfUrlIsLocalhostIpV4(self):
try:
urlCleaner.clean('http://127.0.0.1/some/path')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to localhost'
def testThrowAnErrorIfUrlIsOnLanClassC(self):
try:
urlCleaner.clean('http://192.168.0.1/some/path')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to lan address'
def testThrowAnErrorIfUrlIsOnLanClassB(self):
try:
urlCleaner.clean('http://172.16.32.1/some/path')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to lan address'
def testThrowAnErrorIfUrlIsSelf(self):
try:
urlCleaner.clean('http://valte.ch')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to self address'
def testThrowAnErrorIfUrlIsSelfOnAppSpot(self):
try:
urlCleaner.clean('http://shorten-url.appspot.com')
assert False
except ValueError, e:
assert True
assert e.args[0] == 'Cannot refer to self address'
if __name__=="__main__":
urlCleaner = UrlCleaner()
unittest.main()