blob: b3ad7cd513bbd8bcf3af7105dba1f8d7eb942f3a [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test import test_support
Skip Montanaro6ec967d2002-03-23 05:32:10 +00002import unittest
Fred Drakea4d18a02001-01-05 05:57:04 +00003import urlparse
4
Fred Drakea4d18a02001-01-05 05:57:04 +00005RFC1808_BASE = "http://a/b/c/d;p?q#f"
Skip Montanaro6ec967d2002-03-23 05:32:10 +00006RFC2396_BASE = "http://a/b/c/d;p?q"
Senthil Kumarane41bb0b2010-05-07 04:07:29 +00007RFC3986_BASE = 'http://a/b/c/d;p?q'
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +00008SIMPLE_BASE = 'http://a/b/c/d'
Fred Drakea4d18a02001-01-05 05:57:04 +00009
Ezio Melotti1e87da12011-10-19 10:39:35 +030010# A list of test cases. Each test case is a two-tuple that contains
Facundo Batistac585df92008-09-03 22:35:50 +000011# a string with the query and a dictionary with the expected result.
12
13parse_qsl_test_cases = [
14 ("", []),
15 ("&", []),
16 ("&&", []),
17 ("=", [('', '')]),
18 ("=a", [('', 'a')]),
19 ("a", [('a', '')]),
20 ("a=", [('a', '')]),
21 ("a=", [('a', '')]),
22 ("&a=b", [('a', 'b')]),
23 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
24 ("a=1&a=2", [('a', '1'), ('a', '2')]),
25]
26
Skip Montanaro6ec967d2002-03-23 05:32:10 +000027class UrlParseTestCase(unittest.TestCase):
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000028
29 def checkRoundtrips(self, url, parsed, split):
30 result = urlparse.urlparse(url)
31 self.assertEqual(result, parsed)
Fred Drakead5177c2006-04-01 22:14:43 +000032 t = (result.scheme, result.netloc, result.path,
33 result.params, result.query, result.fragment)
34 self.assertEqual(t, parsed)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000035 # put it back together and it should be the same
36 result2 = urlparse.urlunparse(result)
37 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000038 self.assertEqual(result2, result.geturl())
39
40 # the result of geturl() is a fixpoint; we can always parse it
41 # again to get the same result:
42 result3 = urlparse.urlparse(result.geturl())
43 self.assertEqual(result3.geturl(), result.geturl())
44 self.assertEqual(result3, result)
45 self.assertEqual(result3.scheme, result.scheme)
46 self.assertEqual(result3.netloc, result.netloc)
47 self.assertEqual(result3.path, result.path)
48 self.assertEqual(result3.params, result.params)
49 self.assertEqual(result3.query, result.query)
50 self.assertEqual(result3.fragment, result.fragment)
51 self.assertEqual(result3.username, result.username)
52 self.assertEqual(result3.password, result.password)
53 self.assertEqual(result3.hostname, result.hostname)
54 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000055
56 # check the roundtrip using urlsplit() as well
57 result = urlparse.urlsplit(url)
58 self.assertEqual(result, split)
Fred Drakead5177c2006-04-01 22:14:43 +000059 t = (result.scheme, result.netloc, result.path,
60 result.query, result.fragment)
61 self.assertEqual(t, split)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000062 result2 = urlparse.urlunsplit(result)
63 self.assertEqual(result2, url)
Fred Drakead5177c2006-04-01 22:14:43 +000064 self.assertEqual(result2, result.geturl())
65
66 # check the fixpoint property of re-parsing the result of geturl()
67 result3 = urlparse.urlsplit(result.geturl())
68 self.assertEqual(result3.geturl(), result.geturl())
69 self.assertEqual(result3, result)
70 self.assertEqual(result3.scheme, result.scheme)
71 self.assertEqual(result3.netloc, result.netloc)
72 self.assertEqual(result3.path, result.path)
73 self.assertEqual(result3.query, result.query)
74 self.assertEqual(result3.fragment, result.fragment)
75 self.assertEqual(result3.username, result.username)
76 self.assertEqual(result3.password, result.password)
77 self.assertEqual(result3.hostname, result.hostname)
78 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000079
Facundo Batistac585df92008-09-03 22:35:50 +000080 def test_qsl(self):
81 for orig, expect in parse_qsl_test_cases:
82 result = urlparse.parse_qsl(orig, keep_blank_values=True)
Senthil Kumaran578617a2011-07-23 18:41:43 +080083 self.assertEqual(result, expect, "Error parsing %r" % orig)
84 expect_without_blanks = [v for v in expect if len(v[1])]
85 result = urlparse.parse_qsl(orig, keep_blank_values=False)
86 self.assertEqual(result, expect_without_blanks,
87 "Error parsing %r" % orig)
88
Facundo Batistac585df92008-09-03 22:35:50 +000089
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000090 def test_roundtrips(self):
91 testcases = [
Fred Drake70705652002-10-16 21:02:36 +000092 ('file:///tmp/junk.txt',
93 ('file', '', '/tmp/junk.txt', '', '', ''),
94 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000095 ('imap://mail.python.org/mbox1',
96 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
97 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +000098 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000099 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
100 '', '', ''),
101 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
102 '', '')),
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000103 ('nfs://server/path/to/file.txt',
104 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
105 ('nfs', 'server', '/path/to/file.txt', '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000106 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
107 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
108 '', '', ''),
109 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaran81a04502010-05-13 03:25:21 +0000110 '', '')),
111 ('git+ssh://git@github.com/user/project.git',
112 ('git+ssh', 'git@github.com','/user/project.git',
113 '','',''),
114 ('git+ssh', 'git@github.com','/user/project.git',
115 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000116 ]
117 for url, parsed, split in testcases:
118 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000119
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000120 def test_http_roundtrips(self):
121 # urlparse.urlsplit treats 'http:' as an optimized special case,
122 # so we test both 'http:' and 'https:' in all the following.
123 # Three cheers for white box knowledge!
124 testcases = [
125 ('://www.python.org',
126 ('www.python.org', '', '', '', ''),
127 ('www.python.org', '', '', '')),
128 ('://www.python.org#abc',
129 ('www.python.org', '', '', '', 'abc'),
130 ('www.python.org', '', '', 'abc')),
131 ('://www.python.org?q=abc',
132 ('www.python.org', '', '', 'q=abc', ''),
133 ('www.python.org', '', 'q=abc', '')),
134 ('://www.python.org/#abc',
135 ('www.python.org', '/', '', '', 'abc'),
136 ('www.python.org', '/', '', 'abc')),
137 ('://a/b/c/d;p?q#f',
138 ('a', '/b/c/d', 'p', 'q', 'f'),
139 ('a', '/b/c/d;p', 'q', 'f')),
140 ]
141 for scheme in ('http', 'https'):
142 for url, parsed, split in testcases:
143 url = scheme + url
144 parsed = (scheme,) + parsed
145 split = (scheme,) + split
146 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000147
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000148 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000149 self.assertEqual(urlparse.urljoin(base, relurl), expected,
150 (base, relurl, expected))
151
152 def test_unparse_parse(self):
Senthil Kumarand10b65e2010-04-12 06:50:24 +0000153 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
Fred Drake70705652002-10-16 21:02:36 +0000154 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000155 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000156
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000157 def test_RFC1808(self):
158 # "normal" cases from RFC 1808:
159 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
160 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
161 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
162 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
163 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
164 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000165 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
166 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
167 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
168 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
169 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
170 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000171 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
172 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
173 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
174 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
175 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
176 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
177 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
178 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
179 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
180 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000181
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000182 # "abnormal" cases from RFC 1808:
183 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
184 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
185 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
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/b/c/g.')
189 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.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/g')
193 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
194 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
195 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000196
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000197 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
198 # so we'll not actually run these tests (which expect 1808 behavior).
199 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
200 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000201
Senthil Kumaranddaea1c2011-04-15 18:07:33 +0800202 def test_RFC2368(self):
203 # Issue 11467: path that starts with a number is not parsed correctly
204 self.assertEqual(urlparse.urlparse('mailto:1337@example.org'),
205 ('mailto', '', '1337@example.org', '', '', ''))
206
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000207 def test_RFC2396(self):
208 # cases from RFC 2396
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000209 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
210 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
211 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
212 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
213 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
214 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
215 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
216 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
217 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
218 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
219 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
220 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
221 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
222 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
223 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
224 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
225 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
226 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
227 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
228 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
229 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
230 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
231 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
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/b/c/g.')
235 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.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/g')
239 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
240 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
241 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
242 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
243 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
244 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
245 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
246 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
247 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
248
Facundo Batista67d19812008-08-14 16:51:00 +0000249 def test_RFC3986(self):
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000250 # Test cases from RFC3986
Facundo Batista67d19812008-08-14 16:51:00 +0000251 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
252 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000253 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
254 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
255 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
256 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
257 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
258 self.checkJoin(RFC3986_BASE, '//g','http://g')
259 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
260 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
261 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
262 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
263 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
264 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
265 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
266 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
267 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
268 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
269 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
270 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
271 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
272 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
273 self.checkJoin(RFC3986_BASE, '../..','http://a/')
274 self.checkJoin(RFC3986_BASE, '../../','http://a/')
275 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
276
277 #Abnormal Examples
278
279 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
280 # Tests are here for reference.
281
282 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
283 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
284 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
285 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
286
287 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
288 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
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/g')
292 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
293 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
294 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
295 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
296 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
297 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
298 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
299 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
300 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
301 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
302 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') # relaxed parser
Facundo Batista67d19812008-08-14 16:51:00 +0000303
Senthil Kumaran5c7fd6e2010-12-17 04:56:02 +0000304 # Test for issue9721
305 self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
306
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +0000307 def test_urljoins(self):
308 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
309 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
310 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
311 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
312 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
313 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
314 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
315 self.checkJoin(SIMPLE_BASE, '//g','http://g')
316 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
317 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
318 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
319 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
320 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
321 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
322 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
323 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
324 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
325 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
326 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
327 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
328 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
329 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
330 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
331 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
332 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
333 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
334 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
335 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
336 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
Senthil Kumaran578617a2011-07-23 18:41:43 +0800337 self.checkJoin('http:///', '..','http:///')
338 self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
339 self.checkJoin('', 'http://a/./g', 'http://a/./g')
Senthil Kumaranf432aec2011-08-03 18:31:59 +0800340 self.checkJoin('svn://pathtorepo/dir1','dir2','svn://pathtorepo/dir2')
Senthil Kumaran28696fe2011-08-03 22:06:05 +0800341 self.checkJoin('svn+ssh://pathtorepo/dir1','dir2','svn+ssh://pathtorepo/dir2')
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +0000342
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000343 def test_RFC2732(self):
344 for url, hostname, port in [
345 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
346 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
347 ('http://[::1]:5432/foo/', '::1', 5432),
348 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
349 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
350 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
351 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
352 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
353 ('http://[::ffff:12.34.56.78]:5432/foo/',
354 '::ffff:12.34.56.78', 5432),
355 ('http://Test.python.org/foo/', 'test.python.org', None),
356 ('http://12.34.56.78/foo/', '12.34.56.78', None),
357 ('http://[::1]/foo/', '::1', None),
358 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
359 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
360 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
361 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
362 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
363 ('http://[::ffff:12.34.56.78]/foo/',
364 '::ffff:12.34.56.78', None),
Serhiy Storchaka326b5ab2014-01-18 18:30:09 +0200365 ('http://Test.python.org:/foo/', 'test.python.org', None),
366 ('http://12.34.56.78:/foo/', '12.34.56.78', None),
367 ('http://[::1]:/foo/', '::1', None),
368 ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None),
369 ('http://[dead:beef::]:/foo/', 'dead:beef::', None),
370 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/',
371 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
372 ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None),
373 ('http://[::ffff:12.34.56.78]:/foo/',
374 '::ffff:12.34.56.78', None),
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000375 ]:
376 urlparsed = urlparse.urlparse(url)
377 self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
378
379 for invalid_url in [
380 'http://::12.34.56.78]/',
381 'http://[::1/foo/',
Senthil Kumaran39824612010-04-22 12:10:13 +0000382 'ftp://[::1/foo/bad]/bad',
Senthil Kumaran241a0432010-04-20 20:37:59 +0000383 'http://[::1/foo/bad]/bad',
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000384 'http://[::ffff:12.34.56.78']:
Senthil Kumaran39824612010-04-22 12:10:13 +0000385 self.assertRaises(ValueError, urlparse.urlparse, invalid_url)
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000386
Fred Drake70705652002-10-16 21:02:36 +0000387 def test_urldefrag(self):
388 for url, defrag, frag in [
389 ('http://python.org#frag', 'http://python.org', 'frag'),
390 ('http://python.org', 'http://python.org', ''),
391 ('http://python.org/#frag', 'http://python.org/', 'frag'),
392 ('http://python.org/', 'http://python.org/', ''),
393 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
394 ('http://python.org/?q', 'http://python.org/?q', ''),
395 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
396 ('http://python.org/p?q', 'http://python.org/p?q', ''),
397 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
398 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
399 ]:
400 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
401
Fred Drakead5177c2006-04-01 22:14:43 +0000402 def test_urlsplit_attributes(self):
403 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
404 p = urlparse.urlsplit(url)
405 self.assertEqual(p.scheme, "http")
406 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
407 self.assertEqual(p.path, "/doc/")
408 self.assertEqual(p.query, "")
409 self.assertEqual(p.fragment, "frag")
410 self.assertEqual(p.username, None)
411 self.assertEqual(p.password, None)
412 self.assertEqual(p.hostname, "www.python.org")
413 self.assertEqual(p.port, None)
414 # geturl() won't return exactly the original URL in this case
415 # since the scheme is always case-normalized
416 #self.assertEqual(p.geturl(), url)
417
418 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
419 p = urlparse.urlsplit(url)
420 self.assertEqual(p.scheme, "http")
421 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
422 self.assertEqual(p.path, "/doc/")
423 self.assertEqual(p.query, "query=yes")
424 self.assertEqual(p.fragment, "frag")
425 self.assertEqual(p.username, "User")
426 self.assertEqual(p.password, "Pass")
427 self.assertEqual(p.hostname, "www.python.org")
428 self.assertEqual(p.port, 80)
429 self.assertEqual(p.geturl(), url)
430
Guido van Rossumced4eb02008-01-05 01:21:57 +0000431 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000432 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000433 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000434
435 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
436 p = urlparse.urlsplit(url)
437 self.assertEqual(p.scheme, "http")
438 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
439 self.assertEqual(p.path, "/doc/")
440 self.assertEqual(p.query, "query=yes")
441 self.assertEqual(p.fragment, "frag")
442 self.assertEqual(p.username, "User@example.com")
443 self.assertEqual(p.password, "Pass")
444 self.assertEqual(p.hostname, "www.python.org")
445 self.assertEqual(p.port, 80)
446 self.assertEqual(p.geturl(), url)
447
Senthil Kumaran37484dc2012-05-24 21:54:34 +0800448 # Verify an illegal port of value greater than 65535 is set as None
449 url = "http://www.python.org:65536"
450 p = urlparse.urlsplit(url)
451 self.assertEqual(p.port, None)
452
Ezio Melotti6d9c1b12012-05-19 17:12:17 +0300453 def test_issue14072(self):
454 p1 = urlparse.urlsplit('tel:+31-641044153')
455 self.assertEqual(p1.scheme, 'tel')
456 self.assertEqual(p1.path, '+31-641044153')
Senthil Kumaran1974baa2012-12-24 13:56:54 -0800457
Ezio Melotti6d9c1b12012-05-19 17:12:17 +0300458 p2 = urlparse.urlsplit('tel:+31641044153')
459 self.assertEqual(p2.scheme, 'tel')
460 self.assertEqual(p2.path, '+31641044153')
Guido van Rossumced4eb02008-01-05 01:21:57 +0000461
Senthil Kumaran1974baa2012-12-24 13:56:54 -0800462 # Assert for urlparse
463 p1 = urlparse.urlparse('tel:+31-641044153')
464 self.assertEqual(p1.scheme, 'tel')
465 self.assertEqual(p1.path, '+31-641044153')
466
467 p2 = urlparse.urlparse('tel:+31641044153')
468 self.assertEqual(p2.scheme, 'tel')
469 self.assertEqual(p2.path, '+31641044153')
470
471
472 def test_telurl_params(self):
473 p1 = urlparse.urlparse('tel:123-4;phone-context=+1-650-516')
474 self.assertEqual(p1.scheme, 'tel')
475 self.assertEqual(p1.path, '123-4')
476 self.assertEqual(p1.params, 'phone-context=+1-650-516')
477
478 p1 = urlparse.urlparse('tel:+1-201-555-0123')
479 self.assertEqual(p1.scheme, 'tel')
480 self.assertEqual(p1.path, '+1-201-555-0123')
481 self.assertEqual(p1.params, '')
482
483 p1 = urlparse.urlparse('tel:7042;phone-context=example.com')
484 self.assertEqual(p1.scheme, 'tel')
485 self.assertEqual(p1.path, '7042')
486 self.assertEqual(p1.params, 'phone-context=example.com')
487
488 p1 = urlparse.urlparse('tel:863-1234;phone-context=+1-914-555')
489 self.assertEqual(p1.scheme, 'tel')
490 self.assertEqual(p1.path, '863-1234')
491 self.assertEqual(p1.params, 'phone-context=+1-914-555')
492
493
Fred Drakead5177c2006-04-01 22:14:43 +0000494 def test_attributes_bad_port(self):
495 """Check handling of non-integer ports."""
496 p = urlparse.urlsplit("http://www.example.net:foo")
497 self.assertEqual(p.netloc, "www.example.net:foo")
498 self.assertRaises(ValueError, lambda: p.port)
499
500 p = urlparse.urlparse("http://www.example.net:foo")
501 self.assertEqual(p.netloc, "www.example.net:foo")
502 self.assertRaises(ValueError, lambda: p.port)
503
504 def test_attributes_without_netloc(self):
505 # This example is straight from RFC 3261. It looks like it
506 # should allow the username, hostname, and port to be filled
507 # in, but doesn't. Since it's a URI and doesn't use the
508 # scheme://netloc syntax, the netloc and related attributes
509 # should be left empty.
510 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
511 p = urlparse.urlsplit(uri)
512 self.assertEqual(p.netloc, "")
513 self.assertEqual(p.username, None)
514 self.assertEqual(p.password, None)
515 self.assertEqual(p.hostname, None)
516 self.assertEqual(p.port, None)
517 self.assertEqual(p.geturl(), uri)
518
519 p = urlparse.urlparse(uri)
520 self.assertEqual(p.netloc, "")
521 self.assertEqual(p.username, None)
522 self.assertEqual(p.password, None)
523 self.assertEqual(p.hostname, None)
524 self.assertEqual(p.port, None)
525 self.assertEqual(p.geturl(), uri)
526
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000527 def test_caching(self):
528 # Test case for bug #1313119
529 uri = "http://example.com/doc/"
530 unicode_uri = unicode(uri)
531
532 urlparse.urlparse(unicode_uri)
533 p = urlparse.urlparse(uri)
534 self.assertEqual(type(p.scheme), type(uri))
535 self.assertEqual(type(p.hostname), type(uri))
536 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000537
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000538 def test_noslash(self):
539 # Issue 1637: http://foo.com?query is legal
540 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
541 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
542
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000543 def test_anyscheme(self):
544 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
545 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
546 ('s3','foo.com','/stuff','','',''))
547 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
548 ('x-newscheme','foo.com','/stuff','','',''))
Senthil Kumaranea24dda2012-05-19 08:10:40 +0800549 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query#fragment"),
550 ('x-newscheme','foo.com','/stuff','','query','fragment'))
551 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query"),
552 ('x-newscheme','foo.com','/stuff','','query',''))
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000553
Senthil Kumaran0b5019f2010-08-04 04:45:31 +0000554 def test_withoutscheme(self):
555 # Test urlparse without scheme
556 # Issue 754016: urlparse goes wrong with IP:port without scheme
557 # RFC 1808 specifies that netloc should start with //, urlparse expects
558 # the same, otherwise it classifies the portion of url as path.
559 self.assertEqual(urlparse.urlparse("path"),
560 ('','','path','','',''))
561 self.assertEqual(urlparse.urlparse("//www.python.org:80"),
562 ('','www.python.org:80','','','',''))
563 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
564 ('http','www.python.org:80','','','',''))
565
566 def test_portseparator(self):
567 # Issue 754016 makes changes for port separator ':' from scheme separator
568 self.assertEqual(urlparse.urlparse("path:80"),
569 ('','','path:80','','',''))
570 self.assertEqual(urlparse.urlparse("http:"),('http','','','','',''))
571 self.assertEqual(urlparse.urlparse("https:"),('https','','','','',''))
572 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
573 ('http','www.python.org:80','','','',''))
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000574
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000575def test_main():
576 test_support.run_unittest(UrlParseTestCase)
577
578if __name__ == "__main__":
579 test_main()