blob: 39a897a1a91cead77cbd8f23c4f6b714792357be [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"
Senthil Kumarane41bb0b2010-05-07 04:07:29 +00009RFC3986_BASE = 'http://a/b/c/d;p?q'
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +000010SIMPLE_BASE = 'http://a/b/c/d'
Fred Drakea4d18a02001-01-05 05:57:04 +000011
Ezio Melotti1e87da12011-10-19 10:39:35 +030012# A list of test cases. Each test case is a two-tuple that contains
Facundo Batistac585df92008-09-03 22:35:50 +000013# a string with the query and a dictionary with the expected result.
14
15parse_qsl_test_cases = [
16 ("", []),
17 ("&", []),
18 ("&&", []),
19 ("=", [('', '')]),
20 ("=a", [('', 'a')]),
21 ("a", [('a', '')]),
22 ("a=", [('a', '')]),
23 ("a=", [('a', '')]),
24 ("&a=b", [('a', 'b')]),
25 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
26 ("a=1&a=2", [('a', '1'), ('a', '2')]),
27]
28
Skip Montanaro6ec967d2002-03-23 05:32:10 +000029class UrlParseTestCase(unittest.TestCase):
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000030
31 def checkRoundtrips(self, url, parsed, split):
32 result = urlparse.urlparse(url)
33 self.assertEqual(result, parsed)
Fred Drakead5177c2006-04-01 22:14:43 +000034 t = (result.scheme, result.netloc, result.path,
35 result.params, result.query, result.fragment)
36 self.assertEqual(t, parsed)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000037 # put it back together and it should be the same
38 result2 = urlparse.urlunparse(result)
39 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000040 self.assertEqual(result2, result.geturl())
41
42 # the result of geturl() is a fixpoint; we can always parse it
43 # again to get the same result:
44 result3 = urlparse.urlparse(result.geturl())
45 self.assertEqual(result3.geturl(), result.geturl())
46 self.assertEqual(result3, result)
47 self.assertEqual(result3.scheme, result.scheme)
48 self.assertEqual(result3.netloc, result.netloc)
49 self.assertEqual(result3.path, result.path)
50 self.assertEqual(result3.params, result.params)
51 self.assertEqual(result3.query, result.query)
52 self.assertEqual(result3.fragment, result.fragment)
53 self.assertEqual(result3.username, result.username)
54 self.assertEqual(result3.password, result.password)
55 self.assertEqual(result3.hostname, result.hostname)
56 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000057
58 # check the roundtrip using urlsplit() as well
59 result = urlparse.urlsplit(url)
60 self.assertEqual(result, split)
Fred Drakead5177c2006-04-01 22:14:43 +000061 t = (result.scheme, result.netloc, result.path,
62 result.query, result.fragment)
63 self.assertEqual(t, split)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000064 result2 = urlparse.urlunsplit(result)
65 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000066 self.assertEqual(result2, result.geturl())
67
68 # check the fixpoint property of re-parsing the result of geturl()
69 result3 = urlparse.urlsplit(result.geturl())
70 self.assertEqual(result3.geturl(), result.geturl())
71 self.assertEqual(result3, result)
72 self.assertEqual(result3.scheme, result.scheme)
73 self.assertEqual(result3.netloc, result.netloc)
74 self.assertEqual(result3.path, result.path)
75 self.assertEqual(result3.query, result.query)
76 self.assertEqual(result3.fragment, result.fragment)
77 self.assertEqual(result3.username, result.username)
78 self.assertEqual(result3.password, result.password)
79 self.assertEqual(result3.hostname, result.hostname)
80 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000081
Facundo Batistac585df92008-09-03 22:35:50 +000082 def test_qsl(self):
83 for orig, expect in parse_qsl_test_cases:
84 result = urlparse.parse_qsl(orig, keep_blank_values=True)
Senthil Kumaran578617a2011-07-23 18:41:43 +080085 self.assertEqual(result, expect, "Error parsing %r" % orig)
86 expect_without_blanks = [v for v in expect if len(v[1])]
87 result = urlparse.parse_qsl(orig, keep_blank_values=False)
88 self.assertEqual(result, expect_without_blanks,
89 "Error parsing %r" % orig)
90
Facundo Batistac585df92008-09-03 22:35:50 +000091
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000092 def test_roundtrips(self):
93 testcases = [
Fred Drake70705652002-10-16 21:02:36 +000094 ('file:///tmp/junk.txt',
95 ('file', '', '/tmp/junk.txt', '', '', ''),
96 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000097 ('imap://mail.python.org/mbox1',
98 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
99 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +0000100 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000101 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
102 '', '', ''),
103 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
104 '', '')),
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000105 ('nfs://server/path/to/file.txt',
106 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
107 ('nfs', 'server', '/path/to/file.txt', '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000108 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
109 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
110 '', '', ''),
111 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaran81a04502010-05-13 03:25:21 +0000112 '', '')),
113 ('git+ssh://git@github.com/user/project.git',
114 ('git+ssh', 'git@github.com','/user/project.git',
115 '','',''),
116 ('git+ssh', 'git@github.com','/user/project.git',
117 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000118 ]
119 for url, parsed, split in testcases:
120 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000121
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000122 def test_http_roundtrips(self):
123 # urlparse.urlsplit treats 'http:' as an optimized special case,
124 # so we test both 'http:' and 'https:' in all the following.
125 # Three cheers for white box knowledge!
126 testcases = [
127 ('://www.python.org',
128 ('www.python.org', '', '', '', ''),
129 ('www.python.org', '', '', '')),
130 ('://www.python.org#abc',
131 ('www.python.org', '', '', '', 'abc'),
132 ('www.python.org', '', '', 'abc')),
133 ('://www.python.org?q=abc',
134 ('www.python.org', '', '', 'q=abc', ''),
135 ('www.python.org', '', 'q=abc', '')),
136 ('://www.python.org/#abc',
137 ('www.python.org', '/', '', '', 'abc'),
138 ('www.python.org', '/', '', 'abc')),
139 ('://a/b/c/d;p?q#f',
140 ('a', '/b/c/d', 'p', 'q', 'f'),
141 ('a', '/b/c/d;p', 'q', 'f')),
142 ]
143 for scheme in ('http', 'https'):
144 for url, parsed, split in testcases:
145 url = scheme + url
146 parsed = (scheme,) + parsed
147 split = (scheme,) + split
148 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000149
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000150 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000151 self.assertEqual(urlparse.urljoin(base, relurl), expected,
152 (base, relurl, expected))
153
154 def test_unparse_parse(self):
Senthil Kumarand10b65e2010-04-12 06:50:24 +0000155 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
Fred Drake70705652002-10-16 21:02:36 +0000156 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000157 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000158
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000159 def test_RFC1808(self):
160 # "normal" cases from RFC 1808:
161 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
162 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
163 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
164 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
165 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
166 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000167 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
168 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
169 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
170 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
171 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
172 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000173 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
174 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
175 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
176 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
177 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
178 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
179 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
180 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
181 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
182 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000183
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000184 # "abnormal" cases from RFC 1808:
185 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
186 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
187 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
188 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
189 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
190 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
191 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
192 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
193 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
194 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
195 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
196 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
197 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000198
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000199 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
200 # so we'll not actually run these tests (which expect 1808 behavior).
201 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
202 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000203
Senthil Kumaranddaea1c2011-04-15 18:07:33 +0800204 def test_RFC2368(self):
205 # Issue 11467: path that starts with a number is not parsed correctly
206 self.assertEqual(urlparse.urlparse('mailto:1337@example.org'),
207 ('mailto', '', '1337@example.org', '', '', ''))
208
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000209 def test_RFC2396(self):
210 # cases from RFC 2396
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000211 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
212 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
213 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
214 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
215 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
216 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
217 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
218 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
219 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
220 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
221 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
222 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
223 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
224 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
225 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
226 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
227 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
228 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
229 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
230 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
231 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
232 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
233 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
234 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
235 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
236 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
237 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
238 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
239 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
240 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
241 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
242 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
243 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
244 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
245 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
246 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
247 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
248 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
249 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
250
Facundo Batista67d19812008-08-14 16:51:00 +0000251 def test_RFC3986(self):
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000252 # Test cases from RFC3986
Facundo Batista67d19812008-08-14 16:51:00 +0000253 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
254 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000255 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
256 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
257 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
258 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
259 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
260 self.checkJoin(RFC3986_BASE, '//g','http://g')
261 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
262 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
263 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
264 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
265 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
266 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
267 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
268 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
269 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
270 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
271 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
272 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
273 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
274 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
275 self.checkJoin(RFC3986_BASE, '../..','http://a/')
276 self.checkJoin(RFC3986_BASE, '../../','http://a/')
277 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
278
279 #Abnormal Examples
280
281 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
282 # Tests are here for reference.
283
284 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
285 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
286 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
287 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
288
289 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
290 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
291 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
292 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
293 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
294 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
295 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
296 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
297 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
298 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
299 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
300 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
301 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
302 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
303 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
304 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') # relaxed parser
Facundo Batista67d19812008-08-14 16:51:00 +0000305
Senthil Kumaran5c7fd6e2010-12-17 04:56:02 +0000306 # Test for issue9721
307 self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
308
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +0000309 def test_urljoins(self):
310 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
311 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
312 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
313 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
314 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
315 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
316 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
317 self.checkJoin(SIMPLE_BASE, '//g','http://g')
318 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
319 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
320 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
321 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
322 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
323 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
324 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
325 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
326 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
327 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
328 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
329 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
330 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
331 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
332 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
333 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
334 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
335 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
336 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
337 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
338 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
Senthil Kumaran578617a2011-07-23 18:41:43 +0800339 self.checkJoin('http:///', '..','http:///')
340 self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
341 self.checkJoin('', 'http://a/./g', 'http://a/./g')
Senthil Kumaranf432aec2011-08-03 18:31:59 +0800342 self.checkJoin('svn://pathtorepo/dir1','dir2','svn://pathtorepo/dir2')
Senthil Kumaran28696fe2011-08-03 22:06:05 +0800343 self.checkJoin('svn+ssh://pathtorepo/dir1','dir2','svn+ssh://pathtorepo/dir2')
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +0000344
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000345 def test_RFC2732(self):
346 for url, hostname, port in [
347 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
348 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
349 ('http://[::1]:5432/foo/', '::1', 5432),
350 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
351 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
352 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
353 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
354 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
355 ('http://[::ffff:12.34.56.78]:5432/foo/',
356 '::ffff:12.34.56.78', 5432),
357 ('http://Test.python.org/foo/', 'test.python.org', None),
358 ('http://12.34.56.78/foo/', '12.34.56.78', None),
359 ('http://[::1]/foo/', '::1', None),
360 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
361 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
362 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
363 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
364 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
365 ('http://[::ffff:12.34.56.78]/foo/',
366 '::ffff:12.34.56.78', None),
367 ]:
368 urlparsed = urlparse.urlparse(url)
369 self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
370
371 for invalid_url in [
372 'http://::12.34.56.78]/',
373 'http://[::1/foo/',
Senthil Kumaran39824612010-04-22 12:10:13 +0000374 'ftp://[::1/foo/bad]/bad',
Senthil Kumaran241a0432010-04-20 20:37:59 +0000375 'http://[::1/foo/bad]/bad',
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000376 'http://[::ffff:12.34.56.78']:
Senthil Kumaran39824612010-04-22 12:10:13 +0000377 self.assertRaises(ValueError, urlparse.urlparse, invalid_url)
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000378
Fred Drake70705652002-10-16 21:02:36 +0000379 def test_urldefrag(self):
380 for url, defrag, frag in [
381 ('http://python.org#frag', 'http://python.org', 'frag'),
382 ('http://python.org', 'http://python.org', ''),
383 ('http://python.org/#frag', 'http://python.org/', 'frag'),
384 ('http://python.org/', 'http://python.org/', ''),
385 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
386 ('http://python.org/?q', 'http://python.org/?q', ''),
387 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
388 ('http://python.org/p?q', 'http://python.org/p?q', ''),
389 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
390 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
391 ]:
392 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
393
Fred Drakead5177c2006-04-01 22:14:43 +0000394 def test_urlsplit_attributes(self):
395 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
396 p = urlparse.urlsplit(url)
397 self.assertEqual(p.scheme, "http")
398 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
399 self.assertEqual(p.path, "/doc/")
400 self.assertEqual(p.query, "")
401 self.assertEqual(p.fragment, "frag")
402 self.assertEqual(p.username, None)
403 self.assertEqual(p.password, None)
404 self.assertEqual(p.hostname, "www.python.org")
405 self.assertEqual(p.port, None)
406 # geturl() won't return exactly the original URL in this case
407 # since the scheme is always case-normalized
408 #self.assertEqual(p.geturl(), url)
409
410 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
411 p = urlparse.urlsplit(url)
412 self.assertEqual(p.scheme, "http")
413 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
414 self.assertEqual(p.path, "/doc/")
415 self.assertEqual(p.query, "query=yes")
416 self.assertEqual(p.fragment, "frag")
417 self.assertEqual(p.username, "User")
418 self.assertEqual(p.password, "Pass")
419 self.assertEqual(p.hostname, "www.python.org")
420 self.assertEqual(p.port, 80)
421 self.assertEqual(p.geturl(), url)
422
Guido van Rossumced4eb02008-01-05 01:21:57 +0000423 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000424 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000425 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000426
427 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
428 p = urlparse.urlsplit(url)
429 self.assertEqual(p.scheme, "http")
430 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
431 self.assertEqual(p.path, "/doc/")
432 self.assertEqual(p.query, "query=yes")
433 self.assertEqual(p.fragment, "frag")
434 self.assertEqual(p.username, "User@example.com")
435 self.assertEqual(p.password, "Pass")
436 self.assertEqual(p.hostname, "www.python.org")
437 self.assertEqual(p.port, 80)
438 self.assertEqual(p.geturl(), url)
439
440
Fred Drakead5177c2006-04-01 22:14:43 +0000441 def test_attributes_bad_port(self):
442 """Check handling of non-integer ports."""
443 p = urlparse.urlsplit("http://www.example.net:foo")
444 self.assertEqual(p.netloc, "www.example.net:foo")
445 self.assertRaises(ValueError, lambda: p.port)
446
447 p = urlparse.urlparse("http://www.example.net:foo")
448 self.assertEqual(p.netloc, "www.example.net:foo")
449 self.assertRaises(ValueError, lambda: p.port)
450
451 def test_attributes_without_netloc(self):
452 # This example is straight from RFC 3261. It looks like it
453 # should allow the username, hostname, and port to be filled
454 # in, but doesn't. Since it's a URI and doesn't use the
455 # scheme://netloc syntax, the netloc and related attributes
456 # should be left empty.
457 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
458 p = urlparse.urlsplit(uri)
459 self.assertEqual(p.netloc, "")
460 self.assertEqual(p.username, None)
461 self.assertEqual(p.password, None)
462 self.assertEqual(p.hostname, None)
463 self.assertEqual(p.port, None)
464 self.assertEqual(p.geturl(), uri)
465
466 p = urlparse.urlparse(uri)
467 self.assertEqual(p.netloc, "")
468 self.assertEqual(p.username, None)
469 self.assertEqual(p.password, None)
470 self.assertEqual(p.hostname, None)
471 self.assertEqual(p.port, None)
472 self.assertEqual(p.geturl(), uri)
473
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000474 def test_caching(self):
475 # Test case for bug #1313119
476 uri = "http://example.com/doc/"
477 unicode_uri = unicode(uri)
478
479 urlparse.urlparse(unicode_uri)
480 p = urlparse.urlparse(uri)
481 self.assertEqual(type(p.scheme), type(uri))
482 self.assertEqual(type(p.hostname), type(uri))
483 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000484
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000485 def test_noslash(self):
486 # Issue 1637: http://foo.com?query is legal
487 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
488 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
489
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000490 def test_anyscheme(self):
491 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
492 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
493 ('s3','foo.com','/stuff','','',''))
494 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
495 ('x-newscheme','foo.com','/stuff','','',''))
496
Senthil Kumaran0b5019f2010-08-04 04:45:31 +0000497 def test_withoutscheme(self):
498 # Test urlparse without scheme
499 # Issue 754016: urlparse goes wrong with IP:port without scheme
500 # RFC 1808 specifies that netloc should start with //, urlparse expects
501 # the same, otherwise it classifies the portion of url as path.
502 self.assertEqual(urlparse.urlparse("path"),
503 ('','','path','','',''))
504 self.assertEqual(urlparse.urlparse("//www.python.org:80"),
505 ('','www.python.org:80','','','',''))
506 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
507 ('http','www.python.org:80','','','',''))
508
509 def test_portseparator(self):
510 # Issue 754016 makes changes for port separator ':' from scheme separator
511 self.assertEqual(urlparse.urlparse("path:80"),
512 ('','','path:80','','',''))
513 self.assertEqual(urlparse.urlparse("http:"),('http','','','','',''))
514 self.assertEqual(urlparse.urlparse("https:"),('https','','','','',''))
515 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
516 ('http','www.python.org:80','','','',''))
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000517
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000518def test_main():
519 test_support.run_unittest(UrlParseTestCase)
520
521if __name__ == "__main__":
522 test_main()