blob: e7a8317323c6a495a59e00cb06bce333a72389af [file] [log] [blame]
Skip Montanaro6ec967d2002-03-23 05:32:10 +00001#! /usr/bin/env python
2
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Skip Montanaro6ec967d2002-03-23 05:32:10 +00004import unittest
Fred Drakea4d18a02001-01-05 05:57:04 +00005import urlparse
6
Fred Drakea4d18a02001-01-05 05:57:04 +00007RFC1808_BASE = "http://a/b/c/d;p?q#f"
Skip Montanaro6ec967d2002-03-23 05:32:10 +00008RFC2396_BASE = "http://a/b/c/d;p?q"
Facundo Batista67d19812008-08-14 16:51:00 +00009RFC3986_BASE = "http://a/b/c/d;p?q"
Fred Drakea4d18a02001-01-05 05:57:04 +000010
Facundo Batistac585df92008-09-03 22:35:50 +000011# A list of test cases. Each test case is a a two-tuple that contains
12# a string with the query and a dictionary with the expected result.
13
14parse_qsl_test_cases = [
15 ("", []),
16 ("&", []),
17 ("&&", []),
18 ("=", [('', '')]),
19 ("=a", [('', 'a')]),
20 ("a", [('a', '')]),
21 ("a=", [('a', '')]),
22 ("a=", [('a', '')]),
23 ("&a=b", [('a', 'b')]),
24 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
25 ("a=1&a=2", [('a', '1'), ('a', '2')]),
26]
27
Skip Montanaro6ec967d2002-03-23 05:32:10 +000028class UrlParseTestCase(unittest.TestCase):
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000029
30 def checkRoundtrips(self, url, parsed, split):
31 result = urlparse.urlparse(url)
32 self.assertEqual(result, parsed)
Fred Drakead5177c2006-04-01 22:14:43 +000033 t = (result.scheme, result.netloc, result.path,
34 result.params, result.query, result.fragment)
35 self.assertEqual(t, parsed)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000036 # put it back together and it should be the same
37 result2 = urlparse.urlunparse(result)
38 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000039 self.assertEqual(result2, result.geturl())
40
41 # the result of geturl() is a fixpoint; we can always parse it
42 # again to get the same result:
43 result3 = urlparse.urlparse(result.geturl())
44 self.assertEqual(result3.geturl(), result.geturl())
45 self.assertEqual(result3, result)
46 self.assertEqual(result3.scheme, result.scheme)
47 self.assertEqual(result3.netloc, result.netloc)
48 self.assertEqual(result3.path, result.path)
49 self.assertEqual(result3.params, result.params)
50 self.assertEqual(result3.query, result.query)
51 self.assertEqual(result3.fragment, result.fragment)
52 self.assertEqual(result3.username, result.username)
53 self.assertEqual(result3.password, result.password)
54 self.assertEqual(result3.hostname, result.hostname)
55 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000056
57 # check the roundtrip using urlsplit() as well
58 result = urlparse.urlsplit(url)
59 self.assertEqual(result, split)
Fred Drakead5177c2006-04-01 22:14:43 +000060 t = (result.scheme, result.netloc, result.path,
61 result.query, result.fragment)
62 self.assertEqual(t, split)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000063 result2 = urlparse.urlunsplit(result)
64 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000065 self.assertEqual(result2, result.geturl())
66
67 # check the fixpoint property of re-parsing the result of geturl()
68 result3 = urlparse.urlsplit(result.geturl())
69 self.assertEqual(result3.geturl(), result.geturl())
70 self.assertEqual(result3, result)
71 self.assertEqual(result3.scheme, result.scheme)
72 self.assertEqual(result3.netloc, result.netloc)
73 self.assertEqual(result3.path, result.path)
74 self.assertEqual(result3.query, result.query)
75 self.assertEqual(result3.fragment, result.fragment)
76 self.assertEqual(result3.username, result.username)
77 self.assertEqual(result3.password, result.password)
78 self.assertEqual(result3.hostname, result.hostname)
79 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000080
Facundo Batistac585df92008-09-03 22:35:50 +000081 def test_qsl(self):
82 for orig, expect in parse_qsl_test_cases:
83 result = urlparse.parse_qsl(orig, keep_blank_values=True)
84 self.assertEqual(result, expect, "Error parsing %s" % repr(orig))
85
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000086 def test_roundtrips(self):
87 testcases = [
Fred Drake70705652002-10-16 21:02:36 +000088 ('file:///tmp/junk.txt',
89 ('file', '', '/tmp/junk.txt', '', '', ''),
90 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000091 ('imap://mail.python.org/mbox1',
92 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
93 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +000094 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000095 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
96 '', '', ''),
97 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
98 '', '')),
Senthil Kumaran5e95e762009-03-30 21:51:50 +000099 ('nfs://server/path/to/file.txt',
100 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
101 ('nfs', 'server', '/path/to/file.txt', '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000102 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
103 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
104 '', '', ''),
105 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
106 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000107 ]
108 for url, parsed, split in testcases:
109 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000110
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000111 def test_http_roundtrips(self):
112 # urlparse.urlsplit treats 'http:' as an optimized special case,
113 # so we test both 'http:' and 'https:' in all the following.
114 # Three cheers for white box knowledge!
115 testcases = [
116 ('://www.python.org',
117 ('www.python.org', '', '', '', ''),
118 ('www.python.org', '', '', '')),
119 ('://www.python.org#abc',
120 ('www.python.org', '', '', '', 'abc'),
121 ('www.python.org', '', '', 'abc')),
122 ('://www.python.org?q=abc',
123 ('www.python.org', '', '', 'q=abc', ''),
124 ('www.python.org', '', 'q=abc', '')),
125 ('://www.python.org/#abc',
126 ('www.python.org', '/', '', '', 'abc'),
127 ('www.python.org', '/', '', 'abc')),
128 ('://a/b/c/d;p?q#f',
129 ('a', '/b/c/d', 'p', 'q', 'f'),
130 ('a', '/b/c/d;p', 'q', 'f')),
131 ]
132 for scheme in ('http', 'https'):
133 for url, parsed, split in testcases:
134 url = scheme + url
135 parsed = (scheme,) + parsed
136 split = (scheme,) + split
137 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000138
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000139 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000140 self.assertEqual(urlparse.urljoin(base, relurl), expected,
141 (base, relurl, expected))
142
143 def test_unparse_parse(self):
144 for u in ['Python', './Python']:
Fred Drake70705652002-10-16 21:02:36 +0000145 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000146 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000147
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000148 def test_RFC1808(self):
149 # "normal" cases from RFC 1808:
150 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
151 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
152 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
153 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
154 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
155 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000156 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
157 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
158 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
159 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
160 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
161 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000162 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
163 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
164 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
165 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
166 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
167 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
168 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
169 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
170 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
171 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000172
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000173 # "abnormal" cases from RFC 1808:
174 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
175 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
176 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
177 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
178 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
179 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
180 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
181 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
182 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
183 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
184 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
185 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
186 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000187
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000188 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
189 # so we'll not actually run these tests (which expect 1808 behavior).
190 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
191 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000192
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000193 def test_RFC2396(self):
194 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000195
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000196
197 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
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/g')
202 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
203 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
204 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
205 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
206 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
207 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
208 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
209 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
210 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
211 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
212 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
213 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
214 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
215 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
216 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
217 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
218 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
219 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
220 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
221 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
222 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
223 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
224 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
225 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
226 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
227 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
228 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
229 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
230 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
231 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
232 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
233 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
234 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
235 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
236
Facundo Batista67d19812008-08-14 16:51:00 +0000237 #The following scenarios have been updated in RFC3986
238 #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
239 #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
240
241 def test_RFC3986(self):
242 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
243 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
244
Fred Drake70705652002-10-16 21:02:36 +0000245 def test_urldefrag(self):
246 for url, defrag, frag in [
247 ('http://python.org#frag', 'http://python.org', 'frag'),
248 ('http://python.org', 'http://python.org', ''),
249 ('http://python.org/#frag', 'http://python.org/', 'frag'),
250 ('http://python.org/', 'http://python.org/', ''),
251 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
252 ('http://python.org/?q', 'http://python.org/?q', ''),
253 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
254 ('http://python.org/p?q', 'http://python.org/p?q', ''),
255 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
256 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
257 ]:
258 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
259
Fred Drakead5177c2006-04-01 22:14:43 +0000260 def test_urlsplit_attributes(self):
261 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
262 p = urlparse.urlsplit(url)
263 self.assertEqual(p.scheme, "http")
264 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
265 self.assertEqual(p.path, "/doc/")
266 self.assertEqual(p.query, "")
267 self.assertEqual(p.fragment, "frag")
268 self.assertEqual(p.username, None)
269 self.assertEqual(p.password, None)
270 self.assertEqual(p.hostname, "www.python.org")
271 self.assertEqual(p.port, None)
272 # geturl() won't return exactly the original URL in this case
273 # since the scheme is always case-normalized
274 #self.assertEqual(p.geturl(), url)
275
276 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
277 p = urlparse.urlsplit(url)
278 self.assertEqual(p.scheme, "http")
279 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
280 self.assertEqual(p.path, "/doc/")
281 self.assertEqual(p.query, "query=yes")
282 self.assertEqual(p.fragment, "frag")
283 self.assertEqual(p.username, "User")
284 self.assertEqual(p.password, "Pass")
285 self.assertEqual(p.hostname, "www.python.org")
286 self.assertEqual(p.port, 80)
287 self.assertEqual(p.geturl(), url)
288
Guido van Rossumced4eb02008-01-05 01:21:57 +0000289 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000290 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000291 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000292
293 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
294 p = urlparse.urlsplit(url)
295 self.assertEqual(p.scheme, "http")
296 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
297 self.assertEqual(p.path, "/doc/")
298 self.assertEqual(p.query, "query=yes")
299 self.assertEqual(p.fragment, "frag")
300 self.assertEqual(p.username, "User@example.com")
301 self.assertEqual(p.password, "Pass")
302 self.assertEqual(p.hostname, "www.python.org")
303 self.assertEqual(p.port, 80)
304 self.assertEqual(p.geturl(), url)
305
306
Fred Drakead5177c2006-04-01 22:14:43 +0000307 def test_attributes_bad_port(self):
308 """Check handling of non-integer ports."""
309 p = urlparse.urlsplit("http://www.example.net:foo")
310 self.assertEqual(p.netloc, "www.example.net:foo")
311 self.assertRaises(ValueError, lambda: p.port)
312
313 p = urlparse.urlparse("http://www.example.net:foo")
314 self.assertEqual(p.netloc, "www.example.net:foo")
315 self.assertRaises(ValueError, lambda: p.port)
316
317 def test_attributes_without_netloc(self):
318 # This example is straight from RFC 3261. It looks like it
319 # should allow the username, hostname, and port to be filled
320 # in, but doesn't. Since it's a URI and doesn't use the
321 # scheme://netloc syntax, the netloc and related attributes
322 # should be left empty.
323 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
324 p = urlparse.urlsplit(uri)
325 self.assertEqual(p.netloc, "")
326 self.assertEqual(p.username, None)
327 self.assertEqual(p.password, None)
328 self.assertEqual(p.hostname, None)
329 self.assertEqual(p.port, None)
330 self.assertEqual(p.geturl(), uri)
331
332 p = urlparse.urlparse(uri)
333 self.assertEqual(p.netloc, "")
334 self.assertEqual(p.username, None)
335 self.assertEqual(p.password, None)
336 self.assertEqual(p.hostname, None)
337 self.assertEqual(p.port, None)
338 self.assertEqual(p.geturl(), uri)
339
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000340 def test_caching(self):
341 # Test case for bug #1313119
342 uri = "http://example.com/doc/"
343 unicode_uri = unicode(uri)
344
345 urlparse.urlparse(unicode_uri)
346 p = urlparse.urlparse(uri)
347 self.assertEqual(type(p.scheme), type(uri))
348 self.assertEqual(type(p.hostname), type(uri))
349 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000350
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000351 def test_noslash(self):
352 # Issue 1637: http://foo.com?query is legal
353 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
354 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
355
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000356def test_main():
357 test_support.run_unittest(UrlParseTestCase)
358
359if __name__ == "__main__":
360 test_main()