blob: 82440176f42beddedffe888123ebee84d2e51021 [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
Facundo Batistac585df92008-09-03 22:35:50 +000012# A list of test cases. Each test case is a a two-tuple that contains
13# 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)
85 self.assertEqual(result, expect, "Error parsing %s" % repr(orig))
86
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000087 def test_roundtrips(self):
88 testcases = [
Fred Drake70705652002-10-16 21:02:36 +000089 ('file:///tmp/junk.txt',
90 ('file', '', '/tmp/junk.txt', '', '', ''),
91 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000092 ('imap://mail.python.org/mbox1',
93 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
94 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +000095 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000096 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
97 '', '', ''),
98 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
99 '', '')),
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000100 ('nfs://server/path/to/file.txt',
101 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
102 ('nfs', 'server', '/path/to/file.txt', '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000103 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
104 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
105 '', '', ''),
106 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaran81a04502010-05-13 03:25:21 +0000107 '', '')),
108 ('git+ssh://git@github.com/user/project.git',
109 ('git+ssh', 'git@github.com','/user/project.git',
110 '','',''),
111 ('git+ssh', 'git@github.com','/user/project.git',
112 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000113 ]
114 for url, parsed, split in testcases:
115 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000116
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000117 def test_http_roundtrips(self):
118 # urlparse.urlsplit treats 'http:' as an optimized special case,
119 # so we test both 'http:' and 'https:' in all the following.
120 # Three cheers for white box knowledge!
121 testcases = [
122 ('://www.python.org',
123 ('www.python.org', '', '', '', ''),
124 ('www.python.org', '', '', '')),
125 ('://www.python.org#abc',
126 ('www.python.org', '', '', '', 'abc'),
127 ('www.python.org', '', '', 'abc')),
128 ('://www.python.org?q=abc',
129 ('www.python.org', '', '', 'q=abc', ''),
130 ('www.python.org', '', 'q=abc', '')),
131 ('://www.python.org/#abc',
132 ('www.python.org', '/', '', '', 'abc'),
133 ('www.python.org', '/', '', 'abc')),
134 ('://a/b/c/d;p?q#f',
135 ('a', '/b/c/d', 'p', 'q', 'f'),
136 ('a', '/b/c/d;p', 'q', 'f')),
137 ]
138 for scheme in ('http', 'https'):
139 for url, parsed, split in testcases:
140 url = scheme + url
141 parsed = (scheme,) + parsed
142 split = (scheme,) + split
143 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000144
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000145 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000146 self.assertEqual(urlparse.urljoin(base, relurl), expected,
147 (base, relurl, expected))
148
149 def test_unparse_parse(self):
Senthil Kumarand10b65e2010-04-12 06:50:24 +0000150 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
Fred Drake70705652002-10-16 21:02:36 +0000151 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000152 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000153
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000154 def test_RFC1808(self):
155 # "normal" cases from RFC 1808:
156 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
157 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
158 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
159 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
160 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
161 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000162 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
163 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
164 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
165 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
166 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
167 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000168 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
169 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
170 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
171 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
172 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
173 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
174 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
175 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
176 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
177 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000178
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000179 # "abnormal" cases from RFC 1808:
180 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
181 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
182 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
183 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
184 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
185 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
186 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
187 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
188 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
189 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
190 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
191 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
192 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000193
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000194 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
195 # so we'll not actually run these tests (which expect 1808 behavior).
196 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
197 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000198
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000199 def test_RFC2396(self):
200 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000201
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000202
203 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
204 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
205 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
206 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
207 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
208 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
209 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
210 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
211 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
212 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
213 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
214 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
215 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
216 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
217 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
218 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
219 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
220 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
221 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
222 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
223 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
224 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
225 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
226 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
227 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
228 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
229 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
230 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
231 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
232 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
233 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
234 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
235 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
236 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
237 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
238 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
239 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
240 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
241 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
242
Facundo Batista67d19812008-08-14 16:51:00 +0000243 def test_RFC3986(self):
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000244 # Test cases from RFC3986
Facundo Batista67d19812008-08-14 16:51:00 +0000245 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
246 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarane41bb0b2010-05-07 04:07:29 +0000247 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
248 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
249 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
250 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
251 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
252 self.checkJoin(RFC3986_BASE, '//g','http://g')
253 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
254 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
255 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
256 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
257 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
258 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
259 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
260 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
261 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
262 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
263 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
264 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
265 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
266 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
267 self.checkJoin(RFC3986_BASE, '../..','http://a/')
268 self.checkJoin(RFC3986_BASE, '../../','http://a/')
269 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
270
271 #Abnormal Examples
272
273 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
274 # Tests are here for reference.
275
276 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
277 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
278 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
279 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
280
281 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
282 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
283 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
284 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
285 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
286 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
287 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
288 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
289 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
290 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
291 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
292 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
293 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
294 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
295 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
296 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') # relaxed parser
Facundo Batista67d19812008-08-14 16:51:00 +0000297
Senthil Kumaran9a5bc1d2010-07-14 10:39:35 +0000298 def test_urljoins(self):
299 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
300 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
301 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
302 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
303 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
304 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
305 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
306 self.checkJoin(SIMPLE_BASE, '//g','http://g')
307 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
308 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
309 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
310 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
311 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
312 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
313 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
314 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
315 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
316 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
317 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
318 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
319 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
320 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
321 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
322 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
323 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
324 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
325 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
326 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
327 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
328
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000329 def test_RFC2732(self):
330 for url, hostname, port in [
331 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
332 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
333 ('http://[::1]:5432/foo/', '::1', 5432),
334 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
335 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
336 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
337 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
338 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
339 ('http://[::ffff:12.34.56.78]:5432/foo/',
340 '::ffff:12.34.56.78', 5432),
341 ('http://Test.python.org/foo/', 'test.python.org', None),
342 ('http://12.34.56.78/foo/', '12.34.56.78', None),
343 ('http://[::1]/foo/', '::1', None),
344 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
345 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
346 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
347 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
348 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
349 ('http://[::ffff:12.34.56.78]/foo/',
350 '::ffff:12.34.56.78', None),
351 ]:
352 urlparsed = urlparse.urlparse(url)
353 self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
354
355 for invalid_url in [
356 'http://::12.34.56.78]/',
357 'http://[::1/foo/',
Senthil Kumaran39824612010-04-22 12:10:13 +0000358 'ftp://[::1/foo/bad]/bad',
Senthil Kumaran241a0432010-04-20 20:37:59 +0000359 'http://[::1/foo/bad]/bad',
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000360 'http://[::ffff:12.34.56.78']:
Senthil Kumaran39824612010-04-22 12:10:13 +0000361 self.assertRaises(ValueError, urlparse.urlparse, invalid_url)
Senthil Kumaran8c6d9d72010-04-16 02:46:46 +0000362
Fred Drake70705652002-10-16 21:02:36 +0000363 def test_urldefrag(self):
364 for url, defrag, frag in [
365 ('http://python.org#frag', 'http://python.org', 'frag'),
366 ('http://python.org', 'http://python.org', ''),
367 ('http://python.org/#frag', 'http://python.org/', 'frag'),
368 ('http://python.org/', 'http://python.org/', ''),
369 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
370 ('http://python.org/?q', 'http://python.org/?q', ''),
371 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
372 ('http://python.org/p?q', 'http://python.org/p?q', ''),
373 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
374 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
375 ]:
376 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
377
Fred Drakead5177c2006-04-01 22:14:43 +0000378 def test_urlsplit_attributes(self):
379 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
380 p = urlparse.urlsplit(url)
381 self.assertEqual(p.scheme, "http")
382 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
383 self.assertEqual(p.path, "/doc/")
384 self.assertEqual(p.query, "")
385 self.assertEqual(p.fragment, "frag")
386 self.assertEqual(p.username, None)
387 self.assertEqual(p.password, None)
388 self.assertEqual(p.hostname, "www.python.org")
389 self.assertEqual(p.port, None)
390 # geturl() won't return exactly the original URL in this case
391 # since the scheme is always case-normalized
392 #self.assertEqual(p.geturl(), url)
393
394 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
395 p = urlparse.urlsplit(url)
396 self.assertEqual(p.scheme, "http")
397 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
398 self.assertEqual(p.path, "/doc/")
399 self.assertEqual(p.query, "query=yes")
400 self.assertEqual(p.fragment, "frag")
401 self.assertEqual(p.username, "User")
402 self.assertEqual(p.password, "Pass")
403 self.assertEqual(p.hostname, "www.python.org")
404 self.assertEqual(p.port, 80)
405 self.assertEqual(p.geturl(), url)
406
Guido van Rossumced4eb02008-01-05 01:21:57 +0000407 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000408 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000409 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000410
411 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
412 p = urlparse.urlsplit(url)
413 self.assertEqual(p.scheme, "http")
414 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
415 self.assertEqual(p.path, "/doc/")
416 self.assertEqual(p.query, "query=yes")
417 self.assertEqual(p.fragment, "frag")
418 self.assertEqual(p.username, "User@example.com")
419 self.assertEqual(p.password, "Pass")
420 self.assertEqual(p.hostname, "www.python.org")
421 self.assertEqual(p.port, 80)
422 self.assertEqual(p.geturl(), url)
423
424
Fred Drakead5177c2006-04-01 22:14:43 +0000425 def test_attributes_bad_port(self):
426 """Check handling of non-integer ports."""
427 p = urlparse.urlsplit("http://www.example.net:foo")
428 self.assertEqual(p.netloc, "www.example.net:foo")
429 self.assertRaises(ValueError, lambda: p.port)
430
431 p = urlparse.urlparse("http://www.example.net:foo")
432 self.assertEqual(p.netloc, "www.example.net:foo")
433 self.assertRaises(ValueError, lambda: p.port)
434
435 def test_attributes_without_netloc(self):
436 # This example is straight from RFC 3261. It looks like it
437 # should allow the username, hostname, and port to be filled
438 # in, but doesn't. Since it's a URI and doesn't use the
439 # scheme://netloc syntax, the netloc and related attributes
440 # should be left empty.
441 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
442 p = urlparse.urlsplit(uri)
443 self.assertEqual(p.netloc, "")
444 self.assertEqual(p.username, None)
445 self.assertEqual(p.password, None)
446 self.assertEqual(p.hostname, None)
447 self.assertEqual(p.port, None)
448 self.assertEqual(p.geturl(), uri)
449
450 p = urlparse.urlparse(uri)
451 self.assertEqual(p.netloc, "")
452 self.assertEqual(p.username, None)
453 self.assertEqual(p.password, None)
454 self.assertEqual(p.hostname, None)
455 self.assertEqual(p.port, None)
456 self.assertEqual(p.geturl(), uri)
457
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000458 def test_caching(self):
459 # Test case for bug #1313119
460 uri = "http://example.com/doc/"
461 unicode_uri = unicode(uri)
462
463 urlparse.urlparse(unicode_uri)
464 p = urlparse.urlparse(uri)
465 self.assertEqual(type(p.scheme), type(uri))
466 self.assertEqual(type(p.hostname), type(uri))
467 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000468
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000469 def test_noslash(self):
470 # Issue 1637: http://foo.com?query is legal
471 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
472 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
473
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000474 def test_anyscheme(self):
475 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
476 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
477 ('s3','foo.com','/stuff','','',''))
478 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
479 ('x-newscheme','foo.com','/stuff','','',''))
480
Senthil Kumaran0b5019f2010-08-04 04:45:31 +0000481 def test_withoutscheme(self):
482 # Test urlparse without scheme
483 # Issue 754016: urlparse goes wrong with IP:port without scheme
484 # RFC 1808 specifies that netloc should start with //, urlparse expects
485 # the same, otherwise it classifies the portion of url as path.
486 self.assertEqual(urlparse.urlparse("path"),
487 ('','','path','','',''))
488 self.assertEqual(urlparse.urlparse("//www.python.org:80"),
489 ('','www.python.org:80','','','',''))
490 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
491 ('http','www.python.org:80','','','',''))
492
493 def test_portseparator(self):
494 # Issue 754016 makes changes for port separator ':' from scheme separator
495 self.assertEqual(urlparse.urlparse("path:80"),
496 ('','','path:80','','',''))
497 self.assertEqual(urlparse.urlparse("http:"),('http','','','','',''))
498 self.assertEqual(urlparse.urlparse("https:"),('https','','','','',''))
499 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
500 ('http','www.python.org:80','','','',''))
Senthil Kumaran4e78de82010-02-19 07:32:48 +0000501
502
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000503def test_main():
504 test_support.run_unittest(UrlParseTestCase)
505
506if __name__ == "__main__":
507 test_main()