|
1 #! /usr/bin/env python |
|
2 |
|
3 from test import test_support |
|
4 import unittest |
|
5 import urlparse |
|
6 |
|
7 RFC1808_BASE = "http://a/b/c/d;p?q#f" |
|
8 RFC2396_BASE = "http://a/b/c/d;p?q" |
|
9 |
|
10 class UrlParseTestCase(unittest.TestCase): |
|
11 |
|
12 def checkRoundtrips(self, url, parsed, split): |
|
13 result = urlparse.urlparse(url) |
|
14 self.assertEqual(result, parsed) |
|
15 t = (result.scheme, result.netloc, result.path, |
|
16 result.params, result.query, result.fragment) |
|
17 self.assertEqual(t, parsed) |
|
18 # put it back together and it should be the same |
|
19 result2 = urlparse.urlunparse(result) |
|
20 self.assertEqual(result2, url) |
|
21 self.assertEqual(result2, result.geturl()) |
|
22 |
|
23 # the result of geturl() is a fixpoint; we can always parse it |
|
24 # again to get the same result: |
|
25 result3 = urlparse.urlparse(result.geturl()) |
|
26 self.assertEqual(result3.geturl(), result.geturl()) |
|
27 self.assertEqual(result3, result) |
|
28 self.assertEqual(result3.scheme, result.scheme) |
|
29 self.assertEqual(result3.netloc, result.netloc) |
|
30 self.assertEqual(result3.path, result.path) |
|
31 self.assertEqual(result3.params, result.params) |
|
32 self.assertEqual(result3.query, result.query) |
|
33 self.assertEqual(result3.fragment, result.fragment) |
|
34 self.assertEqual(result3.username, result.username) |
|
35 self.assertEqual(result3.password, result.password) |
|
36 self.assertEqual(result3.hostname, result.hostname) |
|
37 self.assertEqual(result3.port, result.port) |
|
38 |
|
39 # check the roundtrip using urlsplit() as well |
|
40 result = urlparse.urlsplit(url) |
|
41 self.assertEqual(result, split) |
|
42 t = (result.scheme, result.netloc, result.path, |
|
43 result.query, result.fragment) |
|
44 self.assertEqual(t, split) |
|
45 result2 = urlparse.urlunsplit(result) |
|
46 self.assertEqual(result2, url) |
|
47 self.assertEqual(result2, result.geturl()) |
|
48 |
|
49 # check the fixpoint property of re-parsing the result of geturl() |
|
50 result3 = urlparse.urlsplit(result.geturl()) |
|
51 self.assertEqual(result3.geturl(), result.geturl()) |
|
52 self.assertEqual(result3, result) |
|
53 self.assertEqual(result3.scheme, result.scheme) |
|
54 self.assertEqual(result3.netloc, result.netloc) |
|
55 self.assertEqual(result3.path, result.path) |
|
56 self.assertEqual(result3.query, result.query) |
|
57 self.assertEqual(result3.fragment, result.fragment) |
|
58 self.assertEqual(result3.username, result.username) |
|
59 self.assertEqual(result3.password, result.password) |
|
60 self.assertEqual(result3.hostname, result.hostname) |
|
61 self.assertEqual(result3.port, result.port) |
|
62 |
|
63 def test_roundtrips(self): |
|
64 testcases = [ |
|
65 ('file:///tmp/junk.txt', |
|
66 ('file', '', '/tmp/junk.txt', '', '', ''), |
|
67 ('file', '', '/tmp/junk.txt', '', '')), |
|
68 ('imap://mail.python.org/mbox1', |
|
69 ('imap', 'mail.python.org', '/mbox1', '', '', ''), |
|
70 ('imap', 'mail.python.org', '/mbox1', '', '')), |
|
71 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf', |
|
72 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
|
73 '', '', ''), |
|
74 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
|
75 '', '')), |
|
76 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/', |
|
77 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
|
78 '', '', ''), |
|
79 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
|
80 '', '')) |
|
81 ] |
|
82 for url, parsed, split in testcases: |
|
83 self.checkRoundtrips(url, parsed, split) |
|
84 |
|
85 def test_http_roundtrips(self): |
|
86 # urlparse.urlsplit treats 'http:' as an optimized special case, |
|
87 # so we test both 'http:' and 'https:' in all the following. |
|
88 # Three cheers for white box knowledge! |
|
89 testcases = [ |
|
90 ('://www.python.org', |
|
91 ('www.python.org', '', '', '', ''), |
|
92 ('www.python.org', '', '', '')), |
|
93 ('://www.python.org#abc', |
|
94 ('www.python.org', '', '', '', 'abc'), |
|
95 ('www.python.org', '', '', 'abc')), |
|
96 ('://www.python.org?q=abc', |
|
97 ('www.python.org', '', '', 'q=abc', ''), |
|
98 ('www.python.org', '', 'q=abc', '')), |
|
99 ('://www.python.org/#abc', |
|
100 ('www.python.org', '/', '', '', 'abc'), |
|
101 ('www.python.org', '/', '', 'abc')), |
|
102 ('://a/b/c/d;p?q#f', |
|
103 ('a', '/b/c/d', 'p', 'q', 'f'), |
|
104 ('a', '/b/c/d;p', 'q', 'f')), |
|
105 ] |
|
106 for scheme in ('http', 'https'): |
|
107 for url, parsed, split in testcases: |
|
108 url = scheme + url |
|
109 parsed = (scheme,) + parsed |
|
110 split = (scheme,) + split |
|
111 self.checkRoundtrips(url, parsed, split) |
|
112 |
|
113 def checkJoin(self, base, relurl, expected): |
|
114 self.assertEqual(urlparse.urljoin(base, relurl), expected, |
|
115 (base, relurl, expected)) |
|
116 |
|
117 def test_unparse_parse(self): |
|
118 for u in ['Python', './Python']: |
|
119 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u) |
|
120 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u) |
|
121 |
|
122 def test_RFC1808(self): |
|
123 # "normal" cases from RFC 1808: |
|
124 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h') |
|
125 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g') |
|
126 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g') |
|
127 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/') |
|
128 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g') |
|
129 self.checkJoin(RFC1808_BASE, '//g', 'http://g') |
|
130 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y') |
|
131 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
|
132 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s') |
|
133 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s') |
|
134 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
|
135 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
|
136 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x') |
|
137 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
|
138 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/') |
|
139 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/') |
|
140 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/') |
|
141 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/') |
|
142 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g') |
|
143 self.checkJoin(RFC1808_BASE, '../..', 'http://a/') |
|
144 self.checkJoin(RFC1808_BASE, '../../', 'http://a/') |
|
145 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g') |
|
146 |
|
147 # "abnormal" cases from RFC 1808: |
|
148 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f') |
|
149 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g') |
|
150 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g') |
|
151 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g') |
|
152 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g') |
|
153 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.') |
|
154 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g') |
|
155 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..') |
|
156 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g') |
|
157 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g') |
|
158 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/') |
|
159 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h') |
|
160 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h') |
|
161 |
|
162 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), |
|
163 # so we'll not actually run these tests (which expect 1808 behavior). |
|
164 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g') |
|
165 #self.checkJoin(RFC1808_BASE, 'http:', 'http:') |
|
166 |
|
167 def test_RFC2396(self): |
|
168 # cases from RFC 2396 |
|
169 |
|
170 self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y') |
|
171 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x') |
|
172 |
|
173 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h') |
|
174 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g') |
|
175 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g') |
|
176 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/') |
|
177 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g') |
|
178 self.checkJoin(RFC2396_BASE, '//g', 'http://g') |
|
179 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y') |
|
180 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s') |
|
181 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s') |
|
182 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
|
183 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x') |
|
184 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
|
185 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/') |
|
186 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/') |
|
187 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/') |
|
188 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/') |
|
189 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g') |
|
190 self.checkJoin(RFC2396_BASE, '../..', 'http://a/') |
|
191 self.checkJoin(RFC2396_BASE, '../../', 'http://a/') |
|
192 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g') |
|
193 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE) |
|
194 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g') |
|
195 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g') |
|
196 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g') |
|
197 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g') |
|
198 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.') |
|
199 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g') |
|
200 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..') |
|
201 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g') |
|
202 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g') |
|
203 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/') |
|
204 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h') |
|
205 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h') |
|
206 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y') |
|
207 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y') |
|
208 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
|
209 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x') |
|
210 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
|
211 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x') |
|
212 |
|
213 def test_urldefrag(self): |
|
214 for url, defrag, frag in [ |
|
215 ('http://python.org#frag', 'http://python.org', 'frag'), |
|
216 ('http://python.org', 'http://python.org', ''), |
|
217 ('http://python.org/#frag', 'http://python.org/', 'frag'), |
|
218 ('http://python.org/', 'http://python.org/', ''), |
|
219 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'), |
|
220 ('http://python.org/?q', 'http://python.org/?q', ''), |
|
221 ('http://python.org/p#frag', 'http://python.org/p', 'frag'), |
|
222 ('http://python.org/p?q', 'http://python.org/p?q', ''), |
|
223 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'), |
|
224 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''), |
|
225 ]: |
|
226 self.assertEqual(urlparse.urldefrag(url), (defrag, frag)) |
|
227 |
|
228 def test_urlsplit_attributes(self): |
|
229 url = "HTTP://WWW.PYTHON.ORG/doc/#frag" |
|
230 p = urlparse.urlsplit(url) |
|
231 self.assertEqual(p.scheme, "http") |
|
232 self.assertEqual(p.netloc, "WWW.PYTHON.ORG") |
|
233 self.assertEqual(p.path, "/doc/") |
|
234 self.assertEqual(p.query, "") |
|
235 self.assertEqual(p.fragment, "frag") |
|
236 self.assertEqual(p.username, None) |
|
237 self.assertEqual(p.password, None) |
|
238 self.assertEqual(p.hostname, "www.python.org") |
|
239 self.assertEqual(p.port, None) |
|
240 # geturl() won't return exactly the original URL in this case |
|
241 # since the scheme is always case-normalized |
|
242 #self.assertEqual(p.geturl(), url) |
|
243 |
|
244 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag" |
|
245 p = urlparse.urlsplit(url) |
|
246 self.assertEqual(p.scheme, "http") |
|
247 self.assertEqual(p.netloc, "User:Pass@www.python.org:080") |
|
248 self.assertEqual(p.path, "/doc/") |
|
249 self.assertEqual(p.query, "query=yes") |
|
250 self.assertEqual(p.fragment, "frag") |
|
251 self.assertEqual(p.username, "User") |
|
252 self.assertEqual(p.password, "Pass") |
|
253 self.assertEqual(p.hostname, "www.python.org") |
|
254 self.assertEqual(p.port, 80) |
|
255 self.assertEqual(p.geturl(), url) |
|
256 |
|
257 def test_attributes_bad_port(self): |
|
258 """Check handling of non-integer ports.""" |
|
259 p = urlparse.urlsplit("http://www.example.net:foo") |
|
260 self.assertEqual(p.netloc, "www.example.net:foo") |
|
261 self.assertRaises(ValueError, lambda: p.port) |
|
262 |
|
263 p = urlparse.urlparse("http://www.example.net:foo") |
|
264 self.assertEqual(p.netloc, "www.example.net:foo") |
|
265 self.assertRaises(ValueError, lambda: p.port) |
|
266 |
|
267 def test_attributes_without_netloc(self): |
|
268 # This example is straight from RFC 3261. It looks like it |
|
269 # should allow the username, hostname, and port to be filled |
|
270 # in, but doesn't. Since it's a URI and doesn't use the |
|
271 # scheme://netloc syntax, the netloc and related attributes |
|
272 # should be left empty. |
|
273 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15" |
|
274 p = urlparse.urlsplit(uri) |
|
275 self.assertEqual(p.netloc, "") |
|
276 self.assertEqual(p.username, None) |
|
277 self.assertEqual(p.password, None) |
|
278 self.assertEqual(p.hostname, None) |
|
279 self.assertEqual(p.port, None) |
|
280 self.assertEqual(p.geturl(), uri) |
|
281 |
|
282 p = urlparse.urlparse(uri) |
|
283 self.assertEqual(p.netloc, "") |
|
284 self.assertEqual(p.username, None) |
|
285 self.assertEqual(p.password, None) |
|
286 self.assertEqual(p.hostname, None) |
|
287 self.assertEqual(p.port, None) |
|
288 self.assertEqual(p.geturl(), uri) |
|
289 |
|
290 def test_caching(self): |
|
291 # Test case for bug #1313119 |
|
292 uri = "http://example.com/doc/" |
|
293 unicode_uri = unicode(uri) |
|
294 |
|
295 urlparse.urlparse(unicode_uri) |
|
296 p = urlparse.urlparse(uri) |
|
297 self.assertEqual(type(p.scheme), type(uri)) |
|
298 self.assertEqual(type(p.hostname), type(uri)) |
|
299 self.assertEqual(type(p.path), type(uri)) |
|
300 |
|
301 def test_noslash(self): |
|
302 # Issue 1637: http://foo.com?query is legal |
|
303 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"), |
|
304 ('http', 'example.com', '', '', 'blahblah=/foo', '')) |
|
305 |
|
306 def test_main(): |
|
307 test_support.run_unittest(UrlParseTestCase) |
|
308 |
|
309 if __name__ == "__main__": |
|
310 test_main() |