blob: 0225e3baadfac5669fbb94317d88ea6b92754b9f [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 Kumarancdf48a82010-05-07 04:15:56 +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 '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +000099 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
100 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
101 '', '', ''),
102 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaranb87d04f2010-05-13 03:32:26 +0000103 '', '')),
104 ('git+ssh://git@github.com/user/project.git',
105 ('git+ssh', 'git@github.com','/user/project.git',
106 '','',''),
107 ('git+ssh', 'git@github.com','/user/project.git',
108 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000109 ]
110 for url, parsed, split in testcases:
111 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000112
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000113 def test_http_roundtrips(self):
114 # urlparse.urlsplit treats 'http:' as an optimized special case,
115 # so we test both 'http:' and 'https:' in all the following.
116 # Three cheers for white box knowledge!
117 testcases = [
118 ('://www.python.org',
119 ('www.python.org', '', '', '', ''),
120 ('www.python.org', '', '', '')),
121 ('://www.python.org#abc',
122 ('www.python.org', '', '', '', 'abc'),
123 ('www.python.org', '', '', 'abc')),
124 ('://www.python.org?q=abc',
125 ('www.python.org', '', '', 'q=abc', ''),
126 ('www.python.org', '', 'q=abc', '')),
127 ('://www.python.org/#abc',
128 ('www.python.org', '/', '', '', 'abc'),
129 ('www.python.org', '/', '', 'abc')),
130 ('://a/b/c/d;p?q#f',
131 ('a', '/b/c/d', 'p', 'q', 'f'),
132 ('a', '/b/c/d;p', 'q', 'f')),
133 ]
134 for scheme in ('http', 'https'):
135 for url, parsed, split in testcases:
136 url = scheme + url
137 parsed = (scheme,) + parsed
138 split = (scheme,) + split
139 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000140
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000141 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000142 self.assertEqual(urlparse.urljoin(base, relurl), expected,
143 (base, relurl, expected))
144
145 def test_unparse_parse(self):
Senthil Kumaran2ffceb62010-04-12 06:56:24 +0000146 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
Fred Drake70705652002-10-16 21:02:36 +0000147 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000148 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000149
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000150 def test_RFC1808(self):
151 # "normal" cases from RFC 1808:
152 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
153 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
154 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
155 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
156 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
157 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000158 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
159 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
160 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
161 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
162 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
163 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000164 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
165 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
166 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
167 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
168 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
169 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
170 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
171 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
172 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
173 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000174
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000175 # "abnormal" cases from RFC 1808:
176 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
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/./g')
180 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../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/c/g..')
184 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
185 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
186 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
187 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
188 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000189
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000190 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
191 # so we'll not actually run these tests (which expect 1808 behavior).
192 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
193 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000194
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000195 def test_RFC2396(self):
196 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000197
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000198
199 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
200 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
201 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
202 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
203 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
204 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
205 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
206 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
207 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
208 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
209 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
210 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
211 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
212 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
213 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
214 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
215 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
216 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
217 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
218 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
219 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
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/./g')
223 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../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/c/g..')
227 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
228 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
229 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
230 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
231 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
232 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
233 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
234 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
235 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
236 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
237 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
238
Facundo Batista67d19812008-08-14 16:51:00 +0000239 def test_RFC3986(self):
Senthil Kumarancdf48a82010-05-07 04:15:56 +0000240 # Test cases from RFC3986
Facundo Batista67d19812008-08-14 16:51:00 +0000241 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
242 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarancdf48a82010-05-07 04:15:56 +0000243 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
244 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
245 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
246 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
247 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
248 self.checkJoin(RFC3986_BASE, '//g','http://g')
249 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
250 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
251 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
252 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
253 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
254 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
255 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
256 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
257 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
258 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
259 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
260 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
261 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
262 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
263 self.checkJoin(RFC3986_BASE, '../..','http://a/')
264 self.checkJoin(RFC3986_BASE, '../../','http://a/')
265 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
266
267 #Abnormal Examples
268
269 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
270 # Tests are here for reference.
271
272 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
273 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
274 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
275 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
276
277 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
278 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
279 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
280 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
281 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
282 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
283 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
284 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
285 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
286 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
287 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
288 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
289 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
290 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
291 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
292 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser
Facundo Batista67d19812008-08-14 16:51:00 +0000293
Fred Drake70705652002-10-16 21:02:36 +0000294 def test_urldefrag(self):
295 for url, defrag, frag in [
296 ('http://python.org#frag', 'http://python.org', 'frag'),
297 ('http://python.org', 'http://python.org', ''),
298 ('http://python.org/#frag', 'http://python.org/', 'frag'),
299 ('http://python.org/', 'http://python.org/', ''),
300 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
301 ('http://python.org/?q', 'http://python.org/?q', ''),
302 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
303 ('http://python.org/p?q', 'http://python.org/p?q', ''),
304 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
305 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
306 ]:
307 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
308
Fred Drakead5177c2006-04-01 22:14:43 +0000309 def test_urlsplit_attributes(self):
310 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
311 p = urlparse.urlsplit(url)
312 self.assertEqual(p.scheme, "http")
313 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
314 self.assertEqual(p.path, "/doc/")
315 self.assertEqual(p.query, "")
316 self.assertEqual(p.fragment, "frag")
317 self.assertEqual(p.username, None)
318 self.assertEqual(p.password, None)
319 self.assertEqual(p.hostname, "www.python.org")
320 self.assertEqual(p.port, None)
321 # geturl() won't return exactly the original URL in this case
322 # since the scheme is always case-normalized
323 #self.assertEqual(p.geturl(), url)
324
325 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
326 p = urlparse.urlsplit(url)
327 self.assertEqual(p.scheme, "http")
328 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
329 self.assertEqual(p.path, "/doc/")
330 self.assertEqual(p.query, "query=yes")
331 self.assertEqual(p.fragment, "frag")
332 self.assertEqual(p.username, "User")
333 self.assertEqual(p.password, "Pass")
334 self.assertEqual(p.hostname, "www.python.org")
335 self.assertEqual(p.port, 80)
336 self.assertEqual(p.geturl(), url)
337
Guido van Rossumced4eb02008-01-05 01:21:57 +0000338 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000339 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000340 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000341
342 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
343 p = urlparse.urlsplit(url)
344 self.assertEqual(p.scheme, "http")
345 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
346 self.assertEqual(p.path, "/doc/")
347 self.assertEqual(p.query, "query=yes")
348 self.assertEqual(p.fragment, "frag")
349 self.assertEqual(p.username, "User@example.com")
350 self.assertEqual(p.password, "Pass")
351 self.assertEqual(p.hostname, "www.python.org")
352 self.assertEqual(p.port, 80)
353 self.assertEqual(p.geturl(), url)
354
355
Fred Drakead5177c2006-04-01 22:14:43 +0000356 def test_attributes_bad_port(self):
357 """Check handling of non-integer ports."""
358 p = urlparse.urlsplit("http://www.example.net:foo")
359 self.assertEqual(p.netloc, "www.example.net:foo")
360 self.assertRaises(ValueError, lambda: p.port)
361
362 p = urlparse.urlparse("http://www.example.net:foo")
363 self.assertEqual(p.netloc, "www.example.net:foo")
364 self.assertRaises(ValueError, lambda: p.port)
365
366 def test_attributes_without_netloc(self):
367 # This example is straight from RFC 3261. It looks like it
368 # should allow the username, hostname, and port to be filled
369 # in, but doesn't. Since it's a URI and doesn't use the
370 # scheme://netloc syntax, the netloc and related attributes
371 # should be left empty.
372 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
373 p = urlparse.urlsplit(uri)
374 self.assertEqual(p.netloc, "")
375 self.assertEqual(p.username, None)
376 self.assertEqual(p.password, None)
377 self.assertEqual(p.hostname, None)
378 self.assertEqual(p.port, None)
379 self.assertEqual(p.geturl(), uri)
380
381 p = urlparse.urlparse(uri)
382 self.assertEqual(p.netloc, "")
383 self.assertEqual(p.username, None)
384 self.assertEqual(p.password, None)
385 self.assertEqual(p.hostname, None)
386 self.assertEqual(p.port, None)
387 self.assertEqual(p.geturl(), uri)
388
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000389 def test_caching(self):
390 # Test case for bug #1313119
391 uri = "http://example.com/doc/"
392 unicode_uri = unicode(uri)
393
394 urlparse.urlparse(unicode_uri)
395 p = urlparse.urlparse(uri)
396 self.assertEqual(type(p.scheme), type(uri))
397 self.assertEqual(type(p.hostname), type(uri))
398 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000399
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000400 def test_noslash(self):
401 # Issue 1637: http://foo.com?query is legal
402 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
403 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
404
Senthil Kumaranaaa210e2010-02-19 07:39:41 +0000405 def test_anyscheme(self):
406 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
407 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
408 ('s3','foo.com','/stuff','','',''))
409 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
410 ('x-newscheme','foo.com','/stuff','','',''))
411
412
413
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000414def test_main():
415 test_support.run_unittest(UrlParseTestCase)
416
417if __name__ == "__main__":
418 test_main()