blob: 73e4de580bf1ed00e055b50ab193bd0fb5af1a36 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Skip Montanaro6ec967d2002-03-23 05:32:10 +00002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Skip Montanaro6ec967d2002-03-23 05:32:10 +00004import unittest
Jeremy Hylton1afc1692008-06-18 20:49:58 +00005import urllib.parse
Fred Drakea4d18a02001-01-05 05:57:04 +00006
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 Kumarandd3820f2010-05-07 04:19:23 +00009RFC3986_BASE = 'http://a/b/c/d;p?q'
Senthil Kumaranaa69d4d2010-07-14 10:21:22 +000010SIMPLE_BASE = 'http://a/b/c/d'
Fred Drakea4d18a02001-01-05 05:57:04 +000011
Facundo Batistac469d4c2008-09-03 22:49:01 +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')]),
Nick Coghlan9fc443c2010-11-30 15:48:08 +000027 (b"", []),
28 (b"&", []),
29 (b"&&", []),
30 (b"=", [(b'', b'')]),
31 (b"=a", [(b'', b'a')]),
32 (b"a", [(b'a', b'')]),
33 (b"a=", [(b'a', b'')]),
34 (b"a=", [(b'a', b'')]),
35 (b"&a=b", [(b'a', b'b')]),
36 (b"a=a+b&b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
37 (b"a=1&a=2", [(b'a', b'1'), (b'a', b'2')]),
Facundo Batistac469d4c2008-09-03 22:49:01 +000038]
39
Skip Montanaro6ec967d2002-03-23 05:32:10 +000040class UrlParseTestCase(unittest.TestCase):
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000041
42 def checkRoundtrips(self, url, parsed, split):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000043 result = urllib.parse.urlparse(url)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000044 self.assertEqual(result, parsed)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045 t = (result.scheme, result.netloc, result.path,
46 result.params, result.query, result.fragment)
47 self.assertEqual(t, parsed)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000048 # put it back together and it should be the same
Jeremy Hylton1afc1692008-06-18 20:49:58 +000049 result2 = urllib.parse.urlunparse(result)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000050 self.assertEqual(result2, url)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051 self.assertEqual(result2, result.geturl())
52
53 # the result of geturl() is a fixpoint; we can always parse it
54 # again to get the same result:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000055 result3 = urllib.parse.urlparse(result.geturl())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056 self.assertEqual(result3.geturl(), result.geturl())
57 self.assertEqual(result3, result)
58 self.assertEqual(result3.scheme, result.scheme)
59 self.assertEqual(result3.netloc, result.netloc)
60 self.assertEqual(result3.path, result.path)
61 self.assertEqual(result3.params, result.params)
62 self.assertEqual(result3.query, result.query)
63 self.assertEqual(result3.fragment, result.fragment)
64 self.assertEqual(result3.username, result.username)
65 self.assertEqual(result3.password, result.password)
66 self.assertEqual(result3.hostname, result.hostname)
67 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000068
69 # check the roundtrip using urlsplit() as well
Jeremy Hylton1afc1692008-06-18 20:49:58 +000070 result = urllib.parse.urlsplit(url)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000071 self.assertEqual(result, split)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 t = (result.scheme, result.netloc, result.path,
73 result.query, result.fragment)
74 self.assertEqual(t, split)
Jeremy Hylton1afc1692008-06-18 20:49:58 +000075 result2 = urllib.parse.urlunsplit(result)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000076 self.assertEqual(result2, url)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 self.assertEqual(result2, result.geturl())
78
79 # check the fixpoint property of re-parsing the result of geturl()
Jeremy Hylton1afc1692008-06-18 20:49:58 +000080 result3 = urllib.parse.urlsplit(result.geturl())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 self.assertEqual(result3.geturl(), result.geturl())
82 self.assertEqual(result3, result)
83 self.assertEqual(result3.scheme, result.scheme)
84 self.assertEqual(result3.netloc, result.netloc)
85 self.assertEqual(result3.path, result.path)
86 self.assertEqual(result3.query, result.query)
87 self.assertEqual(result3.fragment, result.fragment)
88 self.assertEqual(result3.username, result.username)
89 self.assertEqual(result3.password, result.password)
90 self.assertEqual(result3.hostname, result.hostname)
91 self.assertEqual(result3.port, result.port)
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000092
Facundo Batistac469d4c2008-09-03 22:49:01 +000093 def test_qsl(self):
94 for orig, expect in parse_qsl_test_cases:
95 result = urllib.parse.parse_qsl(orig, keep_blank_values=True)
96 self.assertEqual(result, expect, "Error parsing %s" % repr(orig))
97
98
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000099 def test_roundtrips(self):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000100 str_cases = [
Fred Drake70705652002-10-16 21:02:36 +0000101 ('file:///tmp/junk.txt',
102 ('file', '', '/tmp/junk.txt', '', '', ''),
103 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +0000104 ('imap://mail.python.org/mbox1',
105 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
106 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +0000107 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000108 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
109 '', '', ''),
110 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
111 '', '')),
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000112 ('nfs://server/path/to/file.txt',
113 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
114 ('nfs', 'server', '/path/to/file.txt', '', '')),
Fred Drake50747fc2005-07-29 15:56:32 +0000115 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
116 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
117 '', '', ''),
118 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
Senthil Kumaranead169d2010-05-13 03:37:23 +0000119 '', '')),
120 ('git+ssh://git@github.com/user/project.git',
121 ('git+ssh', 'git@github.com','/user/project.git',
122 '','',''),
123 ('git+ssh', 'git@github.com','/user/project.git',
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000124 '', '')),
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000125 ]
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000126 def _encode(t):
127 return (t[0].encode('ascii'),
128 tuple(x.encode('ascii') for x in t[1]),
129 tuple(x.encode('ascii') for x in t[2]))
130 bytes_cases = [_encode(x) for x in str_cases]
131 for url, parsed, split in str_cases + bytes_cases:
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000132 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +0000133
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000134 def test_http_roundtrips(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000135 # urllib.parse.urlsplit treats 'http:' as an optimized special case,
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000136 # so we test both 'http:' and 'https:' in all the following.
137 # Three cheers for white box knowledge!
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000138 str_cases = [
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +0000139 ('://www.python.org',
140 ('www.python.org', '', '', '', ''),
141 ('www.python.org', '', '', '')),
142 ('://www.python.org#abc',
143 ('www.python.org', '', '', '', 'abc'),
144 ('www.python.org', '', '', 'abc')),
145 ('://www.python.org?q=abc',
146 ('www.python.org', '', '', 'q=abc', ''),
147 ('www.python.org', '', 'q=abc', '')),
148 ('://www.python.org/#abc',
149 ('www.python.org', '/', '', '', 'abc'),
150 ('www.python.org', '/', '', 'abc')),
151 ('://a/b/c/d;p?q#f',
152 ('a', '/b/c/d', 'p', 'q', 'f'),
153 ('a', '/b/c/d;p', 'q', 'f')),
154 ]
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000155 def _encode(t):
156 return (t[0].encode('ascii'),
157 tuple(x.encode('ascii') for x in t[1]),
158 tuple(x.encode('ascii') for x in t[2]))
159 bytes_cases = [_encode(x) for x in str_cases]
160 str_schemes = ('http', 'https')
161 bytes_schemes = (b'http', b'https')
162 str_tests = str_schemes, str_cases
163 bytes_tests = bytes_schemes, bytes_cases
164 for schemes, test_cases in (str_tests, bytes_tests):
165 for scheme in schemes:
166 for url, parsed, split in test_cases:
167 url = scheme + url
168 parsed = (scheme,) + parsed
169 split = (scheme,) + split
170 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +0000171
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000172 def checkJoin(self, base, relurl, expected):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000173 str_components = (base, relurl, expected)
174 self.assertEqual(urllib.parse.urljoin(base, relurl), expected)
175 bytes_components = baseb, relurlb, expectedb = [
176 x.encode('ascii') for x in str_components]
177 self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb)
Guido van Rossumbbc05682002-10-14 19:59:54 +0000178
179 def test_unparse_parse(self):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000180 str_cases = ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]
181 bytes_cases = [x.encode('ascii') for x in str_cases]
182 for u in str_cases + bytes_cases:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000183 self.assertEqual(urllib.parse.urlunsplit(urllib.parse.urlsplit(u)), u)
184 self.assertEqual(urllib.parse.urlunparse(urllib.parse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +0000185
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000186 def test_RFC1808(self):
187 # "normal" cases from RFC 1808:
188 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
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/g')
193 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000194 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
195 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
196 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
197 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
198 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
199 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000200 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
201 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
202 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
203 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
204 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
205 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
206 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
207 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
208 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
209 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000210
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000211 # "abnormal" cases from RFC 1808:
212 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
213 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
214 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
215 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
216 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
217 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
218 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
219 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
220 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
221 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
222 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
223 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
224 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000225
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000226 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
227 # so we'll not actually run these tests (which expect 1808 behavior).
228 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
229 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000230
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000231 def test_RFC2396(self):
232 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000233
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000234
235 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
236 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
237 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
238 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
239 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
240 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
241 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
242 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
243 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
244 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
245 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
246 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
247 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
248 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
249 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
250 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
251 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
252 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
253 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
254 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
255 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
256 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
257 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
258 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
259 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
260 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
261 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
262 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
263 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
264 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
265 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
266 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
267 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
268 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
269 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
270 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
271 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
272 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
273 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
274
Facundo Batista23e38562008-08-14 16:55:14 +0000275 def test_RFC3986(self):
Senthil Kumarandd3820f2010-05-07 04:19:23 +0000276 # Test cases from RFC3986
Facundo Batista23e38562008-08-14 16:55:14 +0000277 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
278 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Senthil Kumarandd3820f2010-05-07 04:19:23 +0000279 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
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/c/g/')
283 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
284 self.checkJoin(RFC3986_BASE, '//g','http://g')
285 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
286 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
287 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
288 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
289 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
290 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
291 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
292 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
293 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
294 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
295 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
296 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
297 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
298 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
299 self.checkJoin(RFC3986_BASE, '../..','http://a/')
300 self.checkJoin(RFC3986_BASE, '../../','http://a/')
301 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
302
303 #Abnormal Examples
304
305 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
306 # Tests are here for reference.
307
308 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
309 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
310 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
311 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
312
313 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
314 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
315 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
316 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
317 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
318 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
319 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
320 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
321 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
322 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
323 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
324 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
325 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
326 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
327 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
328 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser
Facundo Batista23e38562008-08-14 16:55:14 +0000329
Senthil Kumaranaa69d4d2010-07-14 10:21:22 +0000330 def test_urljoins(self):
331 self.checkJoin(SIMPLE_BASE, 'g:h','g: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, 'g','http://a/b/c/g')
335 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
336 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
337 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
338 self.checkJoin(SIMPLE_BASE, '//g','http://g')
339 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
340 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
341 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
342 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
343 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
344 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
345 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
346 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
347 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
348 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
349 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
350 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
351 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
352 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
353 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
354 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
355 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
356 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
357 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
358 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
359 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
360
Senthil Kumaranad02d232010-04-16 03:02:13 +0000361 def test_RFC2732(self):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000362 str_cases = [
Senthil Kumaranad02d232010-04-16 03:02:13 +0000363 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
364 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
365 ('http://[::1]:5432/foo/', '::1', 5432),
366 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
367 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
368 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
369 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
370 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
371 ('http://[::ffff:12.34.56.78]:5432/foo/',
372 '::ffff:12.34.56.78', 5432),
373 ('http://Test.python.org/foo/', 'test.python.org', None),
374 ('http://12.34.56.78/foo/', '12.34.56.78', None),
375 ('http://[::1]/foo/', '::1', None),
376 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
377 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
378 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
379 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
380 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
381 ('http://[::ffff:12.34.56.78]/foo/',
382 '::ffff:12.34.56.78', None),
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000383 ]
384 def _encode(t):
385 return t[0].encode('ascii'), t[1].encode('ascii'), t[2]
386 bytes_cases = [_encode(x) for x in str_cases]
387 for url, hostname, port in str_cases + bytes_cases:
Senthil Kumaranad02d232010-04-16 03:02:13 +0000388 urlparsed = urllib.parse.urlparse(url)
389 self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
390
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000391 str_cases = [
Senthil Kumaranad02d232010-04-16 03:02:13 +0000392 'http://::12.34.56.78]/',
393 'http://[::1/foo/',
Senthil Kumaran7a1e09f2010-04-22 12:19:46 +0000394 'ftp://[::1/foo/bad]/bad',
Senthil Kumaran2eaef052010-04-20 20:42:50 +0000395 'http://[::1/foo/bad]/bad',
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000396 'http://[::ffff:12.34.56.78']
397 bytes_cases = [x.encode('ascii') for x in str_cases]
398 for invalid_url in str_cases + bytes_cases:
Senthil Kumaran7a1e09f2010-04-22 12:19:46 +0000399 self.assertRaises(ValueError, urllib.parse.urlparse, invalid_url)
Senthil Kumaranad02d232010-04-16 03:02:13 +0000400
Fred Drake70705652002-10-16 21:02:36 +0000401 def test_urldefrag(self):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000402 str_cases = [
Fred Drake70705652002-10-16 21:02:36 +0000403 ('http://python.org#frag', 'http://python.org', 'frag'),
404 ('http://python.org', 'http://python.org', ''),
405 ('http://python.org/#frag', 'http://python.org/', 'frag'),
406 ('http://python.org/', 'http://python.org/', ''),
407 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
408 ('http://python.org/?q', 'http://python.org/?q', ''),
409 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
410 ('http://python.org/p?q', 'http://python.org/p?q', ''),
411 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
412 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000413 ]
414 def _encode(t):
415 return type(t)(x.encode('ascii') for x in t)
416 bytes_cases = [_encode(x) for x in str_cases]
417 for url, defrag, frag in str_cases + bytes_cases:
418 result = urllib.parse.urldefrag(url)
419 self.assertEqual(result.geturl(), url)
420 self.assertEqual(result, (defrag, frag))
421 self.assertEqual(result.url, defrag)
422 self.assertEqual(result.fragment, frag)
Fred Drake70705652002-10-16 21:02:36 +0000423
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 def test_urlsplit_attributes(self):
425 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000426 p = urllib.parse.urlsplit(url)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427 self.assertEqual(p.scheme, "http")
428 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
429 self.assertEqual(p.path, "/doc/")
430 self.assertEqual(p.query, "")
431 self.assertEqual(p.fragment, "frag")
432 self.assertEqual(p.username, None)
433 self.assertEqual(p.password, None)
434 self.assertEqual(p.hostname, "www.python.org")
435 self.assertEqual(p.port, None)
436 # geturl() won't return exactly the original URL in this case
437 # since the scheme is always case-normalized
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000438 # We handle this by ignoring the first 4 characters of the URL
439 self.assertEqual(p.geturl()[4:], url[4:])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440
441 url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000442 p = urllib.parse.urlsplit(url)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 self.assertEqual(p.scheme, "http")
444 self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
445 self.assertEqual(p.path, "/doc/")
446 self.assertEqual(p.query, "query=yes")
447 self.assertEqual(p.fragment, "frag")
448 self.assertEqual(p.username, "User")
449 self.assertEqual(p.password, "Pass")
450 self.assertEqual(p.hostname, "www.python.org")
451 self.assertEqual(p.port, 80)
452 self.assertEqual(p.geturl(), url)
453
Christian Heimesfaf2f632008-01-06 16:59:19 +0000454 # Addressing issue1698, which suggests Username can contain
455 # "@" characters. Though not RFC compliant, many ftp sites allow
456 # and request email addresses as usernames.
457
458 url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000459 p = urllib.parse.urlsplit(url)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000460 self.assertEqual(p.scheme, "http")
461 self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
462 self.assertEqual(p.path, "/doc/")
463 self.assertEqual(p.query, "query=yes")
464 self.assertEqual(p.fragment, "frag")
465 self.assertEqual(p.username, "User@example.com")
466 self.assertEqual(p.password, "Pass")
467 self.assertEqual(p.hostname, "www.python.org")
468 self.assertEqual(p.port, 80)
469 self.assertEqual(p.geturl(), url)
470
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000471 # And check them all again, only with bytes this time
472 url = b"HTTP://WWW.PYTHON.ORG/doc/#frag"
473 p = urllib.parse.urlsplit(url)
474 self.assertEqual(p.scheme, b"http")
475 self.assertEqual(p.netloc, b"WWW.PYTHON.ORG")
476 self.assertEqual(p.path, b"/doc/")
477 self.assertEqual(p.query, b"")
478 self.assertEqual(p.fragment, b"frag")
479 self.assertEqual(p.username, None)
480 self.assertEqual(p.password, None)
481 self.assertEqual(p.hostname, b"www.python.org")
482 self.assertEqual(p.port, None)
483 self.assertEqual(p.geturl()[4:], url[4:])
484
485 url = b"http://User:Pass@www.python.org:080/doc/?query=yes#frag"
486 p = urllib.parse.urlsplit(url)
487 self.assertEqual(p.scheme, b"http")
488 self.assertEqual(p.netloc, b"User:Pass@www.python.org:080")
489 self.assertEqual(p.path, b"/doc/")
490 self.assertEqual(p.query, b"query=yes")
491 self.assertEqual(p.fragment, b"frag")
492 self.assertEqual(p.username, b"User")
493 self.assertEqual(p.password, b"Pass")
494 self.assertEqual(p.hostname, b"www.python.org")
495 self.assertEqual(p.port, 80)
496 self.assertEqual(p.geturl(), url)
497
498 url = b"http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
499 p = urllib.parse.urlsplit(url)
500 self.assertEqual(p.scheme, b"http")
501 self.assertEqual(p.netloc, b"User@example.com:Pass@www.python.org:080")
502 self.assertEqual(p.path, b"/doc/")
503 self.assertEqual(p.query, b"query=yes")
504 self.assertEqual(p.fragment, b"frag")
505 self.assertEqual(p.username, b"User@example.com")
506 self.assertEqual(p.password, b"Pass")
507 self.assertEqual(p.hostname, b"www.python.org")
508 self.assertEqual(p.port, 80)
509 self.assertEqual(p.geturl(), url)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000510
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511 def test_attributes_bad_port(self):
512 """Check handling of non-integer ports."""
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000513 p = urllib.parse.urlsplit("http://www.example.net:foo")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 self.assertEqual(p.netloc, "www.example.net:foo")
515 self.assertRaises(ValueError, lambda: p.port)
516
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000517 p = urllib.parse.urlparse("http://www.example.net:foo")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 self.assertEqual(p.netloc, "www.example.net:foo")
519 self.assertRaises(ValueError, lambda: p.port)
520
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000521 # Once again, repeat ourselves to test bytes
522 p = urllib.parse.urlsplit(b"http://www.example.net:foo")
523 self.assertEqual(p.netloc, b"www.example.net:foo")
524 self.assertRaises(ValueError, lambda: p.port)
525
526 p = urllib.parse.urlparse(b"http://www.example.net:foo")
527 self.assertEqual(p.netloc, b"www.example.net:foo")
528 self.assertRaises(ValueError, lambda: p.port)
529
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 def test_attributes_without_netloc(self):
531 # This example is straight from RFC 3261. It looks like it
532 # should allow the username, hostname, and port to be filled
533 # in, but doesn't. Since it's a URI and doesn't use the
534 # scheme://netloc syntax, the netloc and related attributes
535 # should be left empty.
536 uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000537 p = urllib.parse.urlsplit(uri)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 self.assertEqual(p.netloc, "")
539 self.assertEqual(p.username, None)
540 self.assertEqual(p.password, None)
541 self.assertEqual(p.hostname, None)
542 self.assertEqual(p.port, None)
543 self.assertEqual(p.geturl(), uri)
544
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000545 p = urllib.parse.urlparse(uri)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 self.assertEqual(p.netloc, "")
547 self.assertEqual(p.username, None)
548 self.assertEqual(p.password, None)
549 self.assertEqual(p.hostname, None)
550 self.assertEqual(p.port, None)
551 self.assertEqual(p.geturl(), uri)
552
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000553 # You guessed it, repeating the test with bytes input
554 uri = b"sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
555 p = urllib.parse.urlsplit(uri)
556 self.assertEqual(p.netloc, b"")
557 self.assertEqual(p.username, None)
558 self.assertEqual(p.password, None)
559 self.assertEqual(p.hostname, None)
560 self.assertEqual(p.port, None)
561 self.assertEqual(p.geturl(), uri)
562
563 p = urllib.parse.urlparse(uri)
564 self.assertEqual(p.netloc, b"")
565 self.assertEqual(p.username, None)
566 self.assertEqual(p.password, None)
567 self.assertEqual(p.hostname, None)
568 self.assertEqual(p.port, None)
569 self.assertEqual(p.geturl(), uri)
570
Christian Heimesfaf2f632008-01-06 16:59:19 +0000571 def test_noslash(self):
572 # Issue 1637: http://foo.com?query is legal
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000573 self.assertEqual(urllib.parse.urlparse("http://example.com?blahblah=/foo"),
Christian Heimesfaf2f632008-01-06 16:59:19 +0000574 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000575 self.assertEqual(urllib.parse.urlparse(b"http://example.com?blahblah=/foo"),
576 (b'http', b'example.com', b'', b'', b'blahblah=/foo', b''))
Christian Heimesfaf2f632008-01-06 16:59:19 +0000577
Senthil Kumaran84c7d9f2010-08-04 04:50:44 +0000578 def test_withoutscheme(self):
579 # Test urlparse without scheme
580 # Issue 754016: urlparse goes wrong with IP:port without scheme
581 # RFC 1808 specifies that netloc should start with //, urlparse expects
582 # the same, otherwise it classifies the portion of url as path.
583 self.assertEqual(urllib.parse.urlparse("path"),
584 ('','','path','','',''))
585 self.assertEqual(urllib.parse.urlparse("//www.python.org:80"),
586 ('','www.python.org:80','','','',''))
587 self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
588 ('http','www.python.org:80','','','',''))
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000589 # Repeat for bytes input
590 self.assertEqual(urllib.parse.urlparse(b"path"),
591 (b'',b'',b'path',b'',b'',b''))
592 self.assertEqual(urllib.parse.urlparse(b"//www.python.org:80"),
593 (b'',b'www.python.org:80',b'',b'',b'',b''))
594 self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
595 (b'http',b'www.python.org:80',b'',b'',b'',b''))
Senthil Kumaran84c7d9f2010-08-04 04:50:44 +0000596
597 def test_portseparator(self):
598 # Issue 754016 makes changes for port separator ':' from scheme separator
599 self.assertEqual(urllib.parse.urlparse("path:80"),
600 ('','','path:80','','',''))
601 self.assertEqual(urllib.parse.urlparse("http:"),('http','','','','',''))
602 self.assertEqual(urllib.parse.urlparse("https:"),('https','','','','',''))
603 self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
604 ('http','www.python.org:80','','','',''))
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000605 # As usual, need to check bytes input as well
606 self.assertEqual(urllib.parse.urlparse(b"path:80"),
607 (b'',b'',b'path:80',b'',b'',b''))
608 self.assertEqual(urllib.parse.urlparse(b"http:"),(b'http',b'',b'',b'',b'',b''))
609 self.assertEqual(urllib.parse.urlparse(b"https:"),(b'https',b'',b'',b'',b'',b''))
610 self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
611 (b'http',b'www.python.org:80',b'',b'',b'',b''))
Senthil Kumaran84c7d9f2010-08-04 04:50:44 +0000612
Facundo Batista2ac5de22008-07-07 18:24:11 +0000613 def test_usingsys(self):
614 # Issue 3314: sys module is used in the error
615 self.assertRaises(TypeError, urllib.parse.urlencode, "foo")
616
Senthil Kumaran6be85c52010-02-19 07:42:50 +0000617 def test_anyscheme(self):
618 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
Ezio Melotti5e15efa2010-02-19 14:49:02 +0000619 self.assertEqual(urllib.parse.urlparse("s3://foo.com/stuff"),
620 ('s3', 'foo.com', '/stuff', '', '', ''))
621 self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"),
622 ('x-newscheme', 'foo.com', '/stuff', '', '', ''))
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000623 # And for bytes...
624 self.assertEqual(urllib.parse.urlparse(b"s3://foo.com/stuff"),
625 (b's3', b'foo.com', b'/stuff', b'', b'', b''))
626 self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff"),
627 (b'x-newscheme', b'foo.com', b'/stuff', b'', b'', b''))
628
629 def test_mixed_types_rejected(self):
630 # Several functions that process either strings or ASCII encoded bytes
631 # accept multiple arguments. Check they reject mixed type input
Ezio Melottied3a7d22010-12-01 02:32:32 +0000632 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000633 urllib.parse.urlparse("www.python.org", b"http")
Ezio Melottied3a7d22010-12-01 02:32:32 +0000634 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000635 urllib.parse.urlparse(b"www.python.org", "http")
Ezio Melottied3a7d22010-12-01 02:32:32 +0000636 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000637 urllib.parse.urlsplit("www.python.org", b"http")
Ezio Melottied3a7d22010-12-01 02:32:32 +0000638 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000639 urllib.parse.urlsplit(b"www.python.org", "http")
Ezio Melottied3a7d22010-12-01 02:32:32 +0000640 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000641 urllib.parse.urlunparse(( b"http", "www.python.org","","","",""))
Ezio Melottied3a7d22010-12-01 02:32:32 +0000642 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000643 urllib.parse.urlunparse(("http", b"www.python.org","","","",""))
Ezio Melottied3a7d22010-12-01 02:32:32 +0000644 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000645 urllib.parse.urlunsplit((b"http", "www.python.org","","",""))
Ezio Melottied3a7d22010-12-01 02:32:32 +0000646 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000647 urllib.parse.urlunsplit(("http", b"www.python.org","","",""))
Ezio Melottied3a7d22010-12-01 02:32:32 +0000648 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000649 urllib.parse.urljoin("http://python.org", b"http://python.org")
Ezio Melottied3a7d22010-12-01 02:32:32 +0000650 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
Nick Coghlan9fc443c2010-11-30 15:48:08 +0000651 urllib.parse.urljoin(b"http://python.org", "http://python.org")
652
653 def _check_result_type(self, str_type):
654 num_args = len(str_type._fields)
655 bytes_type = str_type._encoded_counterpart
656 self.assertIs(bytes_type._decoded_counterpart, str_type)
657 str_args = ('',) * num_args
658 bytes_args = (b'',) * num_args
659 str_result = str_type(*str_args)
660 bytes_result = bytes_type(*bytes_args)
661 encoding = 'ascii'
662 errors = 'strict'
663 self.assertEqual(str_result, str_args)
664 self.assertEqual(bytes_result.decode(), str_args)
665 self.assertEqual(bytes_result.decode(), str_result)
666 self.assertEqual(bytes_result.decode(encoding), str_args)
667 self.assertEqual(bytes_result.decode(encoding), str_result)
668 self.assertEqual(bytes_result.decode(encoding, errors), str_args)
669 self.assertEqual(bytes_result.decode(encoding, errors), str_result)
670 self.assertEqual(bytes_result, bytes_args)
671 self.assertEqual(str_result.encode(), bytes_args)
672 self.assertEqual(str_result.encode(), bytes_result)
673 self.assertEqual(str_result.encode(encoding), bytes_args)
674 self.assertEqual(str_result.encode(encoding), bytes_result)
675 self.assertEqual(str_result.encode(encoding, errors), bytes_args)
676 self.assertEqual(str_result.encode(encoding, errors), bytes_result)
677
678 def test_result_pairs(self):
679 # Check encoding and decoding between result pairs
680 result_types = [
681 urllib.parse.DefragResult,
682 urllib.parse.SplitResult,
683 urllib.parse.ParseResult,
684 ]
685 for result_type in result_types:
686 self._check_result_type(result_type)
687
Senthil Kumaran6be85c52010-02-19 07:42:50 +0000688
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000689def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000690 support.run_unittest(UrlParseTestCase)
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000691
692if __name__ == "__main__":
693 test_main()