forked from caylan/403Section
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_normalize_test.py
150 lines (135 loc) · 4.3 KB
/
url_normalize_test.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import url_normalize
'''
Pulled from url_normalize.py
'''
suite = unittest.TestSuite()
""" from http://www.intertwingly.net/wiki/pie/PaceCanonicalIds """
tests1 = [
(False, "http://:@example.com/"),
(False, "http://@example.com/"),
(False, "http://example.com"),
(False, "HTTP://example.com/"),
(False, "http://EXAMPLE.COM/"),
(False, "http://example.com/%7Ejane"),
(False, "http://example.com/?q=%C7"),
(False, "http://example.com/?q=%5c"),
(False, "http://example.com/?q=C%CC%A7"),
(False, "http://example.com/a/../a/b"),
(False, "http://example.com/a/./b"),
(False, "http://example.com:80/"),
(True, "http://example.com/"),
(True, "http://example.com/?q=%C3%87"),
(True, "http://example.com/?q=%E2%85%A0"),
(True, "http://example.com/?q=%5C"),
(True, "http://example.com/~jane"),
(True, "http://example.com/a/b"),
(True, "http://example.com:8080/"),
(True, "http://user:[email protected]/"),
# from rfc2396bis
(True, "ftp://ftp.is.co.za/rfc/rfc1808.txt"),
(True, "http://www.ietf.org/rfc/rfc2396.txt"),
(True, "ldap://[2001:db8::7]/c=GB?objectClass?one"),
(True, "mailto:[email protected]"),
(True, "news:comp.infosystems.www.servers.unix"),
(True, "tel:+1-816-555-1212"),
(True, "telnet://192.0.2.16:80/"),
(True, "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"),
# other
(True, "http://127.0.0.1/"),
(False, "http://127.0.0.1:80/"),
(True, "http://www.w3.org/2000/01/rdf-schema#"),
(False, "http://example.com:081/"),
]
def testcase1(expected, value):
class test(unittest.TestCase):
def runTest(self):
assert (url_normalize.url_normalize(value) == value) == expected, (expected, value, url_normalize(value))
return test()
for (expected, value) in tests1:
suite.addTest(testcase1(expected, value))
""" mnot test suite; three tests updated for rfc2396bis. """
tests2 = {
'/foo/bar/.':
'/foo/bar/',
'/foo/bar/./':
'/foo/bar/',
'/foo/bar/..':
'/foo/',
'/foo/bar/../':
'/foo/',
'/foo/bar/../baz':
'/foo/baz',
'/foo/bar/../..':
'/',
'/foo/bar/../../':
'/',
'/foo/bar/../../baz':
'/baz',
'/foo/bar/../../../baz':
'/baz', #was: '/../baz',
'/foo/bar/../../../../baz':
'/baz',
'/./foo':
'/foo',
'/../foo':
'/foo', #was: '/../foo',
'/foo.':
'/foo.',
'/.foo':
'/.foo',
'/foo..':
'/foo..',
'/..foo':
'/..foo',
'/./../foo':
'/foo', #was: '/../foo',
'/./foo/.':
'/foo/',
'/foo/./bar':
'/foo/bar',
'/foo/../bar':
'/bar',
'/foo//':
'/foo/',
'/foo///bar//':
'/foo/bar/',
'http://www.foo.com:80/foo':
'http://www.foo.com/foo',
'http://www.foo.com:8000/foo':
'http://www.foo.com:8000/foo',
'http://www.foo.com./foo/bar.html':
'http://www.foo.com/foo/bar.html',
'http://www.foo.com.:81/foo':
'http://www.foo.com:81/foo',
'http://www.foo.com/%7ebar':
'http://www.foo.com/~bar',
'http://www.foo.com/%7Ebar':
'http://www.foo.com/~bar',
'ftp://user:[email protected]/foo/bar':
'ftp://user:[email protected]/foo/bar',
'http://USER:[email protected]/foo/bar':
'http://USER:[email protected]/foo/bar',
'http://www.example.com./':
'http://www.example.com/',
'-':
'-',
'пример.испытание/Служебная:Search/Test':
'http://xn--e1afmkfd.xn--80akhbyknj4f/%D0%A1%D0%BB%D1%83%D0%B6%D0%B5%D0%B1%D0%BD%D0%B0%D1%8F:Search/Test',
'http://lifehacker.com/#!5753509/hello-world-this-is-the-new-lifehacker':
'http://lifehacker.com/?_escaped_fragment_=5753509/hello-world-this-is-the-new-lifehacker',
}
def testcase2(original, normalized):
class test(unittest.TestCase):
def runTest(self):
assert url_normalize.url_normalize(original) == normalized, (original, normalized, url_normalize.url_normalize(original))
return test()
for (original, normalized) in tests2.items():
suite.addTest(testcase2(original, normalized))
def run_test():
""" execute tests """
unittest.TextTestRunner().run(suite)
if __name__ == "__main__":
run_test()