blob: c437ee0d14eb37f215762b28eafdd1f3db12c9c8 [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'
Senthil Kumarand76ec0f2010-07-14 10:58:12 +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 '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000100 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
101 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
102 '', '', ''),
103 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaranb87d04f2010-05-13 03:32:26 +0000104 '', '')),
105 ('git+ssh://git@github.com/user/project.git',
106 ('git+ssh', 'git@github.com','/user/project.git',
107 '','',''),
108 ('git+ssh', 'git@github.com','/user/project.git',
109 '', ''))
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000110 ]
111 for url, parsed, split in testcases:
112 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000113
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000114 def test_http_roundtrips(self):
115 # urlparse.urlsplit treats 'http:' as an optimized special case,
116 # so we test both 'http:' and 'https:' in all the following.
117 # Three cheers for white box knowledge!
118 testcases = [
119 ('://www.python.org',
120 ('www.python.org', '', '', '', ''),
121 ('www.python.org', '', '', '')),
122 ('://www.python.org#abc',
123 ('www.python.org', '', '', '', 'abc'),
124 ('www.python.org', '', '', 'abc')),
125 ('://www.python.org?q=abc',
126 ('www.python.org', '', '', 'q=abc', ''),
127 ('www.python.org', '', 'q=abc', '')),
128 ('://www.python.org/#abc',
129 ('www.python.org', '/', '', '', 'abc'),
130 ('www.python.org', '/', '', 'abc')),
131 ('://a/b/c/d;p?q#f',
132 ('a', '/b/c/d', 'p', 'q', 'f'),
133 ('a', '/b/c/d;p', 'q', 'f')),
134 ]
135 for scheme in ('http', 'https'):
136 for url, parsed, split in testcases:
137 url = scheme + url
138 parsed = (scheme,) + parsed
139 split = (scheme,) + split
140 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000141
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000142 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +0000143 self.assertEqual(urlparse.urljoin(base, relurl), expected,
144 (base, relurl, expected))
145
146 def test_unparse_parse(self):
Senthil Kumaran2ffceb62010-04-12 06:56:24 +0000147 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
Fred Drake70705652002-10-16 21:02:36 +0000148 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000149 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000150
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000151 def test_RFC1808(self):
152 # "normal" cases from RFC 1808:
153 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
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/b/c/g/')
157 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
158 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000159 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
160 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
161 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
162 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
163 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
164 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000165 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
166 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
167 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
168 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
169 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
170 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
171 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
172 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
173 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
174 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000175
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000176 # "abnormal" cases from RFC 1808:
177 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
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/../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/c/..g')
186 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
187 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
188 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
189 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000190
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000191 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
192 # so we'll not actually run these tests (which expect 1808 behavior).
193 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
194 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000195
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000196 def test_RFC2396(self):
197 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000198
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000199
200 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
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/b/c/g/')
204 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
205 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
206 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
207 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
208 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
209 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
210 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
211 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
212 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
213 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
214 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
215 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
216 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
217 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
218 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
219 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
220 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
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/../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/c/..g')
229 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
230 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
231 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
232 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
233 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
234 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
235 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
236 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
237 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
238 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
239
Facundo Batista67d19812008-08-14 16:51:00 +0000240 def test_RFC3986(self):
Senthil Kumarancdf48a82010-05-07 04:15:56 +0000241 # Test cases from RFC3986
Facundo Batista67d19812008-08-14 16:51:00 +0000242 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
243 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarancdf48a82010-05-07 04:15:56 +0000244 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
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/b/c/g/')
248 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
249 self.checkJoin(RFC3986_BASE, '//g','http://g')
250 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
251 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
252 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
253 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
254 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
255 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
256 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
257 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
258 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
259 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
260 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
261 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
262 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
263 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
264 self.checkJoin(RFC3986_BASE, '../..','http://a/')
265 self.checkJoin(RFC3986_BASE, '../../','http://a/')
266 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
267
268 #Abnormal Examples
269
270 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
271 # Tests are here for reference.
272
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 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
277
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/c/..g')
282 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
283 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
284 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
285 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
286 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
287 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
288 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
289 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
290 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
291 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
292 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
293 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser
Facundo Batista67d19812008-08-14 16:51:00 +0000294
Senthil Kumarand76ec0f2010-07-14 10:58:12 +0000295 def test_urljoins(self):
296 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
297 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
298 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
299 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
300 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
301 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
302 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
303 self.checkJoin(SIMPLE_BASE, '//g','http://g')
304 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
305 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
306 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
307 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
308 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
309 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
310 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
311 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
312 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
313 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
314 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
315 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
316 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
317 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
318 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
319 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
320 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
321 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
322 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
323 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
324 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
325
Fred Drake70705652002-10-16 21:02:36 +0000326 def test_urldefrag(self):
327 for url, defrag, frag in [
328 ('http://python.org#frag', 'http://python.org', 'frag'),
329 ('http://python.org', 'http://python.org', ''),
330 ('http://python.org/#frag', 'http://python.org/', 'frag'),
331 ('http://python.org/', 'http://python.org/', ''),
332 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
333 ('http://python.org/?q', 'http://python.org/?q', ''),
334 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
335 ('http://python.org/p?q', 'http://python.org/p?q', ''),
336 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
337 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
338 ]:
339 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
340
Fred Drakead5177c2006-04-01 22:14:43 +0000341 def test_urlsplit_attributes(self):
342 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
343 p = urlparse.urlsplit(url)
344 self.assertEqual(p.scheme, "http")
345 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
346 self.assertEqual(p.path, "/doc/")
347 self.assertEqual(p.query, "")
348 self.assertEqual(p.fragment, "frag")
349 self.assertEqual(p.username, None)
350 self.assertEqual(p.password, None)
351 self.assertEqual(p.hostname, "www.python.org")
352 self.assertEqual(p.port, None)
353 # geturl() won't return exactly the original URL in this case
354 # since the scheme is always case-normalized
355 #self.assertEqual(p.geturl(), url)
356
357 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
358 p = urlparse.urlsplit(url)
359 self.assertEqual(p.scheme, "http")
360 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
361 self.assertEqual(p.path, "/doc/")
362 self.assertEqual(p.query, "query=yes")
363 self.assertEqual(p.fragment, "frag")
364 self.assertEqual(p.username, "User")
365 self.assertEqual(p.password, "Pass")
366 self.assertEqual(p.hostname, "www.python.org")
367 self.assertEqual(p.port, 80)
368 self.assertEqual(p.geturl(), url)
369
Guido van Rossumced4eb02008-01-05 01:21:57 +0000370 # Addressing issue1698, which suggests Username can contain
Andrew M. Kuchling05899142008-01-05 15:13:49 +0000371 # "@" characters. Though not RFC compliant, many ftp sites allow
Fred Drakef7476c42008-01-05 04:38:38 +0000372 # and request email addresses as usernames.
Guido van Rossumced4eb02008-01-05 01:21:57 +0000373
374 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
375 p = urlparse.urlsplit(url)
376 self.assertEqual(p.scheme, "http")
377 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
378 self.assertEqual(p.path, "/doc/")
379 self.assertEqual(p.query, "query=yes")
380 self.assertEqual(p.fragment, "frag")
381 self.assertEqual(p.username, "User@example.com")
382 self.assertEqual(p.password, "Pass")
383 self.assertEqual(p.hostname, "www.python.org")
384 self.assertEqual(p.port, 80)
385 self.assertEqual(p.geturl(), url)
386
387
Fred Drakead5177c2006-04-01 22:14:43 +0000388 def test_attributes_bad_port(self):
389 """Check handling of non-integer ports."""
390 p = urlparse.urlsplit("http://www.example.net:foo")
391 self.assertEqual(p.netloc, "www.example.net:foo")
392 self.assertRaises(ValueError, lambda: p.port)
393
394 p = urlparse.urlparse("http://www.example.net:foo")
395 self.assertEqual(p.netloc, "www.example.net:foo")
396 self.assertRaises(ValueError, lambda: p.port)
397
398 def test_attributes_without_netloc(self):
399 # This example is straight from RFC 3261. It looks like it
400 # should allow the username, hostname, and port to be filled
401 # in, but doesn't. Since it's a URI and doesn't use the
402 # scheme://netloc syntax, the netloc and related attributes
403 # should be left empty.
404 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
405 p = urlparse.urlsplit(uri)
406 self.assertEqual(p.netloc, "")
407 self.assertEqual(p.username, None)
408 self.assertEqual(p.password, None)
409 self.assertEqual(p.hostname, None)
410 self.assertEqual(p.port, None)
411 self.assertEqual(p.geturl(), uri)
412
413 p = urlparse.urlparse(uri)
414 self.assertEqual(p.netloc, "")
415 self.assertEqual(p.username, None)
416 self.assertEqual(p.password, None)
417 self.assertEqual(p.hostname, None)
418 self.assertEqual(p.port, None)
419 self.assertEqual(p.geturl(), uri)
420
Alexandre Vassalotti2f9ca292007-12-13 17:58:23 +0000421 def test_caching(self):
422 # Test case for bug #1313119
423 uri = "http://example.com/doc/"
424 unicode_uri = unicode(uri)
425
426 urlparse.urlparse(unicode_uri)
427 p = urlparse.urlparse(uri)
428 self.assertEqual(type(p.scheme), type(uri))
429 self.assertEqual(type(p.hostname), type(uri))
430 self.assertEqual(type(p.path), type(uri))
Fred Drakead5177c2006-04-01 22:14:43 +0000431
Guido van Rossumc6a04c22008-01-05 22:19:06 +0000432 def test_noslash(self):
433 # Issue 1637: http://foo.com?query is legal
434 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
435 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
436
Senthil Kumaranaaa210e2010-02-19 07:39:41 +0000437 def test_anyscheme(self):
438 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
439 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
440 ('s3','foo.com','/stuff','','',''))
441 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
442 ('x-newscheme','foo.com','/stuff','','',''))
443
444
445
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000446def test_main():
447 test_support.run_unittest(UrlParseTestCase)
448
449if __name__ == "__main__":
450 test_main()