Debian Codegraph @

Lib/test/test_urlparse.py

raw
1 import sys
2 import unicodedata
3 import unittest
4 import urllib.parse
5 from test import support
6
7 RFC1808_BASE = "http://a/b/c/d;p?q#f"
8 RFC2396_BASE = "http://a/b/c/d;p?q"
9 RFC3986_BASE = 'http://a/b/c/d;p?q'
10 SIMPLE_BASE = 'http://a/b/c/d'
11
12 # Each parse_qsl testcase is a two-tuple that contains
13 # a string with the query and a list with the expected result.
14
15 parse_qsl_test_cases = [
16 ("", []),
17 ("&", []),
18 ("&&", []),
19 ("=", [('', '')]),
20 ("=a", [('', 'a')]),
21 ("a", [('a', '')]),
22 ("a=", [('a', '')]),
23 ("a=b=c", [('a', 'b=c')]),
24 ("a%3Db=c", [('a=b', 'c')]),
25 ("a=b&c=d", [('a', 'b'), ('c', 'd')]),
26 ("a=b%26c=d", [('a', 'b&c=d')]),
27 ("&a=b", [('a', 'b')]),
28 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
29 ("a=1&a=2", [('a', '1'), ('a', '2')]),
30 (b"", []),
31 (b"&", []),
32 (b"&&", []),
33 (b"=", [(b'', b'')]),
34 (b"=a", [(b'', b'a')]),
35 (b"a", [(b'a', b'')]),
36 (b"a=", [(b'a', b'')]),
37 (b"a=b=c", [(b'a', b'b=c')]),
38 (b"a%3Db=c", [(b'a=b', b'c')]),
39 (b"a=b&c=d", [(b'a', b'b'), (b'c', b'd')]),
40 (b"a=b%26c=d", [(b'a', b'b&c=d')]),
41 (b"&a=b", [(b'a', b'b')]),
42 (b"a=a+b&b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
43 (b"a=1&a=2", [(b'a', b'1'), (b'a', b'2')]),
44 (";a=b", [(';a', 'b')]),
45 ("a=a+b;b=b+c", [('a', 'a b;b=b c')]),
46 (b";a=b", [(b';a', b'b')]),
47 (b"a=a+b;b=b+c", [(b'a', b'a b;b=b c')]),
48
49 ("\u0141=\xE9", [('\u0141', '\xE9')]),
50 ("%C5%81=%C3%A9", [('\u0141', '\xE9')]),
51 ("%81=%A9", [('\ufffd', '\ufffd')]),
52 (b"\xc5\x81=\xc3\xa9", [(b'\xc5\x81', b'\xc3\xa9')]),
53 (b"%C5%81=%C3%A9", [(b'\xc5\x81', b'\xc3\xa9')]),
54 (b"\x81=\xA9", [(b'\x81', b'\xa9')]),
55 (b"%81=%A9", [(b'\x81', b'\xa9')]),
56 ]
57
58 # Each parse_qs testcase is a two-tuple that contains
59 # a string with the query and a dictionary with the expected result.
60
61 parse_qs_test_cases = [
62 ("", {}),
63 ("&", {}),
64 ("&&", {}),
65 ("=", {'': ['']}),
66 ("=a", {'': ['a']}),
67 ("a", {'a': ['']}),
68 ("a=", {'a': ['']}),
69 ("a=b=c", {'a': ['b=c']}),
70 ("a%3Db=c", {'a=b': ['c']}),
71 ("a=b&c=d", {'a': ['b'], 'c': ['d']}),
72 ("a=b%26c=d", {'a': ['b&c=d']}),
73 ("&a=b", {'a': ['b']}),
74 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
75 ("a=1&a=2", {'a': ['1', '2']}),
76 (b"", {}),
77 (b"&", {}),
78 (b"&&", {}),
79 (b"=", {b'': [b'']}),
80 (b"=a", {b'': [b'a']}),
81 (b"a", {b'a': [b'']}),
82 (b"a=", {b'a': [b'']}),
83 (b"a=b=c", {b'a': [b'b=c']}),
84 (b"a%3Db=c", {b'a=b': [b'c']}),
85 (b"a=b&c=d", {b'a': [b'b'], b'c': [b'd']}),
86 (b"a=b%26c=d", {b'a': [b'b&c=d']}),
87 (b"&a=b", {b'a': [b'b']}),
88 (b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
89 (b"a=1&a=2", {b'a': [b'1', b'2']}),
90 (";a=b", {';a': ['b']}),
91 ("a=a+b;b=b+c", {'a': ['a b;b=b c']}),
92 (b";a=b", {b';a': [b'b']}),
93 (b"a=a+b;b=b+c", {b'a':[ b'a b;b=b c']}),
94 (b"a=a%E2%80%99b", {b'a': [b'a\xe2\x80\x99b']}),
95
96 ("\u0141=\xE9", {'\u0141': ['\xE9']}),
97 ("%C5%81=%C3%A9", {'\u0141': ['\xE9']}),
98 ("%81=%A9", {'\ufffd': ['\ufffd']}),
99 (b"\xc5\x81=\xc3\xa9", {b'\xc5\x81': [b'\xc3\xa9']}),
100 (b"%C5%81=%C3%A9", {b'\xc5\x81': [b'\xc3\xa9']}),
101 (b"\x81=\xA9", {b'\x81': [b'\xa9']}),
102 (b"%81=%A9", {b'\x81': [b'\xa9']}),
103 ]
104
105 class UrlParseTestCase(unittest.TestCase):
106
107 def checkRoundtrips(self, url, parsed, split, url2=None):
108 if url2 is None:
109 url2 = url
110 result = urllib.parse.urlparse(url)
111 self.assertSequenceEqual(result, parsed)
112 t = (result.scheme, result.netloc, result.path,
113 result.params, result.query, result.fragment)
114 self.assertSequenceEqual(t, parsed)
115 # put it back together and it should be the same
116 result2 = urllib.parse.urlunparse(result)
117 self.assertSequenceEqual(result2, url2)
118 self.assertSequenceEqual(result2, result.geturl())
119
120 # the result of geturl() is a fixpoint; we can always parse it
121 # again to get the same result:
122 result3 = urllib.parse.urlparse(result.geturl())
123 self.assertEqual(result3.geturl(), result.geturl())
124 self.assertSequenceEqual(result3, result)
125 self.assertEqual(result3.scheme, result.scheme)
126 self.assertEqual(result3.netloc, result.netloc)
127 self.assertEqual(result3.path, result.path)
128 self.assertEqual(result3.params, result.params)
129 self.assertEqual(result3.query, result.query)
130 self.assertEqual(result3.fragment, result.fragment)
131 self.assertEqual(result3.username, result.username)
132 self.assertEqual(result3.password, result.password)
133 self.assertEqual(result3.hostname, result.hostname)
134 self.assertEqual(result3.port, result.port)
135
136 # check the roundtrip using urlsplit() as well
137 result = urllib.parse.urlsplit(url)
138 self.assertSequenceEqual(result, split)
139 t = (result.scheme, result.netloc, result.path,
140 result.query, result.fragment)
141 self.assertSequenceEqual(t, split)
142 result2 = urllib.parse.urlunsplit(result)
143 self.assertSequenceEqual(result2, url2)
144 self.assertSequenceEqual(result2, result.geturl())
145
146 # check the fixpoint property of re-parsing the result of geturl()
147 result3 = urllib.parse.urlsplit(result.geturl())
148 self.assertEqual(result3.geturl(), result.geturl())
149 self.assertSequenceEqual(result3, result)
150 self.assertEqual(result3.scheme, result.scheme)
151 self.assertEqual(result3.netloc, result.netloc)
152 self.assertEqual(result3.path, result.path)
153 self.assertEqual(result3.query, result.query)
154 self.assertEqual(result3.fragment, result.fragment)
155 self.assertEqual(result3.username, result.username)
156 self.assertEqual(result3.password, result.password)
157 self.assertEqual(result3.hostname, result.hostname)
158 self.assertEqual(result3.port, result.port)
159
160 @support.subTests('orig,expect', parse_qsl_test_cases)
161 def test_qsl(self, orig, expect):
162 result = urllib.parse.parse_qsl(orig, keep_blank_values=True)
163 self.assertEqual(result, expect)
164 expect_without_blanks = [v for v in expect if len(v[1])]
165 result = urllib.parse.parse_qsl(orig, keep_blank_values=False)
166 self.assertEqual(result, expect_without_blanks)
167
168 @support.subTests('orig,expect', parse_qs_test_cases)
169 def test_qs(self, orig, expect):
170 result = urllib.parse.parse_qs(orig, keep_blank_values=True)
171 self.assertEqual(result, expect)
172 expect_without_blanks = {v: expect[v]
173 for v in expect if len(expect[v][0])}
174 result = urllib.parse.parse_qs(orig, keep_blank_values=False)
175 self.assertEqual(result, expect_without_blanks)
176
177 @support.subTests('bytes', (False, True))
178 @support.subTests('url,parsed,split', [
179 ('path/to/file',
180 ('', '', 'path/to/file', '', '', ''),
181 ('', '', 'path/to/file', '', '')),
182 ('/path/to/file',
183 ('', '', '/path/to/file', '', '', ''),
184 ('', '', '/path/to/file', '', '')),
185 ('//path/to/file',
186 ('', 'path', '/to/file', '', '', ''),
187 ('', 'path', '/to/file', '', '')),
188 ('////path/to/file',
189 ('', '', '//path/to/file', '', '', ''),
190 ('', '', '//path/to/file', '', '')),
191 ('/////path/to/file',
192 ('', '', '///path/to/file', '', '', ''),
193 ('', '', '///path/to/file', '', '')),
194 ('scheme:path/to/file',
195 ('scheme', '', 'path/to/file', '', '', ''),
196 ('scheme', '', 'path/to/file', '', '')),
197 ('scheme:/path/to/file',
198 ('scheme', '', '/path/to/file', '', '', ''),
199 ('scheme', '', '/path/to/file', '', '')),
200 ('scheme://path/to/file',
201 ('scheme', 'path', '/to/file', '', '', ''),
202 ('scheme', 'path', '/to/file', '', '')),
203 ('scheme:////path/to/file',
204 ('scheme', '', '//path/to/file', '', '', ''),
205 ('scheme', '', '//path/to/file', '', '')),
206 ('scheme://///path/to/file',
207 ('scheme', '', '///path/to/file', '', '', ''),
208 ('scheme', '', '///path/to/file', '', '')),
209 ('file:tmp/junk.txt',
210 ('file', '', 'tmp/junk.txt', '', '', ''),
211 ('file', '', 'tmp/junk.txt', '', '')),
212 ('file:///tmp/junk.txt',
213 ('file', '', '/tmp/junk.txt', '', '', ''),
214 ('file', '', '/tmp/junk.txt', '', '')),
215 ('file:////tmp/junk.txt',
216 ('file', '', '//tmp/junk.txt', '', '', ''),
217 ('file', '', '//tmp/junk.txt', '', '')),
218 ('file://///tmp/junk.txt',
219 ('file', '', '///tmp/junk.txt', '', '', ''),
220 ('file', '', '///tmp/junk.txt', '', '')),
221 ('http:tmp/junk.txt',
222 ('http', '', 'tmp/junk.txt', '', '', ''),
223 ('http', '', 'tmp/junk.txt', '', '')),
224 ('http://example.com/tmp/junk.txt',
225 ('http', 'example.com', '/tmp/junk.txt', '', '', ''),
226 ('http', 'example.com', '/tmp/junk.txt', '', '')),
227 ('http:///example.com/tmp/junk.txt',
228 ('http', '', '/example.com/tmp/junk.txt', '', '', ''),
229 ('http', '', '/example.com/tmp/junk.txt', '', '')),
230 ('http:////example.com/tmp/junk.txt',
231 ('http', '', '//example.com/tmp/junk.txt', '', '', ''),
232 ('http', '', '//example.com/tmp/junk.txt', '', '')),
233 ('imap://mail.python.org/mbox1',
234 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
235 ('imap', 'mail.python.org', '/mbox1', '', '')),
236 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
237 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
238 '', '', ''),
239 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
240 '', '')),
241 ('nfs://server/path/to/file.txt',
242 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
243 ('nfs', 'server', '/path/to/file.txt', '', '')),
244 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
245 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
246 '', '', ''),
247 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
248 '', '')),
249 ('git+ssh://[email protected]/user/project.git',
250 ('git+ssh', '[email protected]','/user/project.git',
251 '','',''),
252 ('git+ssh', '[email protected]','/user/project.git',
253 '', '')),
254 ('itms-services://?action=download-manifest&url=https://example.com/app',
255 ('itms-services', '', '', '',
256 'action=download-manifest&url=https://example.com/app', ''),
257 ('itms-services', '', '',
258 'action=download-manifest&url=https://example.com/app', '')),
259 ('+scheme:path/to/file',
260 ('', '', '+scheme:path/to/file', '', '', ''),
261 ('', '', '+scheme:path/to/file', '', '')),
262 ('sch_me:path/to/file',
263 ('', '', 'sch_me:path/to/file', '', '', ''),
264 ('', '', 'sch_me:path/to/file', '', '')),
265 ('schème:path/to/file',
266 ('', '', 'schème:path/to/file', '', '', ''),
267 ('', '', 'schème:path/to/file', '', '')),
268 ])
269 def test_roundtrips(self, bytes, url, parsed, split):
270 if bytes:
271 if not url.isascii():
272 self.skipTest('non-ASCII bytes')
273 url = str_encode(url)
274 parsed = tuple_encode(parsed)
275 split = tuple_encode(split)
276 self.checkRoundtrips(url, parsed, split)
277
278 @support.subTests('bytes', (False, True))
279 @support.subTests('url,url2,parsed,split', [
280 ('///path/to/file',
281 '/path/to/file',
282 ('', '', '/path/to/file', '', '', ''),
283 ('', '', '/path/to/file', '', '')),
284 ('scheme:///path/to/file',
285 'scheme:/path/to/file',
286 ('scheme', '', '/path/to/file', '', '', ''),
287 ('scheme', '', '/path/to/file', '', '')),
288 ('file:/tmp/junk.txt',
289 'file:///tmp/junk.txt',
290 ('file', '', '/tmp/junk.txt', '', '', ''),
291 ('file', '', '/tmp/junk.txt', '', '')),
292 ('http:/tmp/junk.txt',
293 'http:///tmp/junk.txt',
294 ('http', '', '/tmp/junk.txt', '', '', ''),
295 ('http', '', '/tmp/junk.txt', '', '')),
296 ('https:/tmp/junk.txt',
297 'https:///tmp/junk.txt',
298 ('https', '', '/tmp/junk.txt', '', '', ''),
299 ('https', '', '/tmp/junk.txt', '', '')),
300 ])
301 def test_roundtrips_normalization(self, bytes, url, url2, parsed, split):
302 if bytes:
303 url = str_encode(url)
304 url2 = str_encode(url2)
305 parsed = tuple_encode(parsed)
306 split = tuple_encode(split)
307 self.checkRoundtrips(url, parsed, split, url2)
308
309 @support.subTests('bytes', (False, True))
310 @support.subTests('scheme', ('http', 'https'))
311 @support.subTests('url,parsed,split', [
312 ('://www.python.org',
313 ('www.python.org', '', '', '', ''),
314 ('www.python.org', '', '', '')),
315 ('://www.python.org#abc',
316 ('www.python.org', '', '', '', 'abc'),
317 ('www.python.org', '', '', 'abc')),
318 ('://www.python.org?q=abc',
319 ('www.python.org', '', '', 'q=abc', ''),
320 ('www.python.org', '', 'q=abc', '')),
321 ('://www.python.org/#abc',
322 ('www.python.org', '/', '', '', 'abc'),
323 ('www.python.org', '/', '', 'abc')),
324 ('://a/b/c/d;p?q#f',
325 ('a', '/b/c/d', 'p', 'q', 'f'),
326 ('a', '/b/c/d;p', 'q', 'f')),
327 ])
328 def test_http_roundtrips(self, bytes, scheme, url, parsed, split):
329 # urllib.parse.urlsplit treats 'http:' as an optimized special case,
330 # so we test both 'http:' and 'https:' in all the following.
331 # Three cheers for white box knowledge!
332 if bytes:
333 scheme = str_encode(scheme)
334 url = str_encode(url)
335 parsed = tuple_encode(parsed)
336 split = tuple_encode(split)
337 url = scheme + url
338 parsed = (scheme,) + parsed
339 split = (scheme,) + split
340 self.checkRoundtrips(url, parsed, split)
341
342 def checkJoin(self, base, relurl, expected, *, relroundtrip=True):
343 with self.subTest(base=base, relurl=relurl):
344 self.assertEqual(urllib.parse.urljoin(base, relurl), expected)
345 baseb = base.encode('ascii')
346 relurlb = relurl.encode('ascii')
347 expectedb = expected.encode('ascii')
348 self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb)
349
350 if relroundtrip:
351 relurl = urllib.parse.urlunsplit(urllib.parse.urlsplit(relurl))
352 self.assertEqual(urllib.parse.urljoin(base, relurl), expected)
353 relurlb = urllib.parse.urlunsplit(urllib.parse.urlsplit(relurlb))
354 self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb)
355
356 @support.subTests('bytes', (False, True))
357 @support.subTests('u', ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',])
358 def test_unparse_parse(self, bytes, u):
359 if bytes:
360 u = str_encode(u)
361 self.assertEqual(urllib.parse.urlunsplit(urllib.parse.urlsplit(u)), u)
362 self.assertEqual(urllib.parse.urlunparse(urllib.parse.urlparse(u)), u)
363
364 def test_RFC1808(self):
365 # "normal" cases from RFC 1808:
366 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
367 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
368 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
369 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
370 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
371 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
372 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
373 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
374 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
375 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
376 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
377 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
378 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
379 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
380 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
381 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
382 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
383 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
384 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
385 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
386 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
387 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
388
389 # "abnormal" cases from RFC 1808:
390 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
391 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
392 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
393 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
394 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
395 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
396 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
397 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
398 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
399
400 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
401 # so we'll not actually run these tests (which expect 1808 behavior).
402 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
403 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
404
405 # XXX: The following tests are no longer compatible with RFC3986
406 # self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
407 # self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
408 # self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
409 # self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
410
411
412 def test_RFC2368(self):
413 # Issue 11467: path that starts with a number is not parsed correctly
414 self.assertEqual(urllib.parse.urlparse('mailto:[email protected]'),
415 ('mailto', '', '[email protected]', '', '', ''))
416
417 def test_RFC2396(self):
418 # cases from RFC 2396
419
420 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
421 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
422 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
423 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
424 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
425 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
426 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
427 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
428 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
429 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
430 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
431 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
432 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
433 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
434 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
435 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
436 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
437 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
438 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
439 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
440 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
441 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
442 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
443 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
444 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
445 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
446 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
447 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
448 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
449 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
450 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
451 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
452 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
453 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
454 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
455
456 # XXX: The following tests are no longer compatible with RFC3986
457 # self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
458 # self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
459 # self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
460 # self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
461
462 def test_RFC3986(self):
463 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
464 self.checkJoin(RFC3986_BASE, ';x', 'http://a/b/c/;x')
465 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
466 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
467 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
468 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
469 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
470 self.checkJoin(RFC3986_BASE, '//g','http://g')
471 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
472 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
473 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
474 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
475 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
476 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
477 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
478 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
479 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
480 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
481 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
482 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
483 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
484 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
485 self.checkJoin(RFC3986_BASE, '../..','http://a/')
486 self.checkJoin(RFC3986_BASE, '../../','http://a/')
487 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
488 self.checkJoin(RFC3986_BASE, '../../../g', 'http://a/g')
489
490 # Abnormal Examples
491
492 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
493 # Tests are here for reference.
494
495 self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
496 self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
497 self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
498 self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
499 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
500 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
501 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
502 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
503 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
504 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
505 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
506 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
507 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
508 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
509 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
510 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
511 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
512 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
513 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
514 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser
515
516 # Test for issue9721
517 self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
518
519 def test_urljoins(self):
520 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
521 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
522 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
523 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
524 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
525 self.checkJoin(SIMPLE_BASE, '//g','http://g')
526 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
527 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
528 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
529 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
530 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
531 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
532 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
533 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
534 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
535 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
536 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
537 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
538 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
539 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
540 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
541 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
542 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
543 self.checkJoin('http:///', '..','http:///')
544 self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
545 self.checkJoin('', 'http://a/./g', 'http://a/./g')
546 self.checkJoin('svn://pathtorepo/dir1', 'dir2', 'svn://pathtorepo/dir2')
547 self.checkJoin('svn+ssh://pathtorepo/dir1', 'dir2', 'svn+ssh://pathtorepo/dir2')
548 self.checkJoin('ws://a/b','g','ws://a/g')
549 self.checkJoin('wss://a/b','g','wss://a/g')
550
551 # XXX: The following tests are no longer compatible with RFC3986
552 # self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
553 # self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
554
555 # test for issue22118 duplicate slashes
556 self.checkJoin(SIMPLE_BASE + '/', 'foo', SIMPLE_BASE + '/foo')
557
558 # Non-RFC-defined tests, covering variations of base and trailing
559 # slashes
560 self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/')
561 self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/')
562 self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/')
563 self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/')
564 self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g')
565 self.checkJoin('http://a/b/', '../../f/g/', 'http://a/f/g/')
566
567 # issue 23703: don't duplicate filename
568 self.checkJoin('a', 'b', 'b')
569
570 # Test with empty (but defined) components.
571 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
572 self.checkJoin(RFC1808_BASE, '#', 'http://a/b/c/d;p?q#', relroundtrip=False)
573 self.checkJoin(RFC1808_BASE, '#z', 'http://a/b/c/d;p?q#z')
574 self.checkJoin(RFC1808_BASE, '?', 'http://a/b/c/d;p?', relroundtrip=False)
575 self.checkJoin(RFC1808_BASE, '?#z', 'http://a/b/c/d;p?#z', relroundtrip=False)
576 self.checkJoin(RFC1808_BASE, '?y', 'http://a/b/c/d;p?y')
577 self.checkJoin(RFC1808_BASE, ';', 'http://a/b/c/;')
578 self.checkJoin(RFC1808_BASE, ';?y', 'http://a/b/c/;?y')
579 self.checkJoin(RFC1808_BASE, ';#z', 'http://a/b/c/;#z')
580 self.checkJoin(RFC1808_BASE, ';x', 'http://a/b/c/;x')
581 self.checkJoin(RFC1808_BASE, '/w', 'http://a/w')
582 self.checkJoin(RFC1808_BASE, '//', 'http://a/b/c/d;p?q#f')
583 self.checkJoin(RFC1808_BASE, '//#z', 'http://a/b/c/d;p?q#z')
584 self.checkJoin(RFC1808_BASE, '//?y', 'http://a/b/c/d;p?y')
585 self.checkJoin(RFC1808_BASE, '//;x', 'http://;x')
586 self.checkJoin(RFC1808_BASE, '///w', 'http://a/w')
587 self.checkJoin(RFC1808_BASE, '//v', 'http://v')
588 # For backward compatibility with RFC1630, the scheme name is allowed
589 # to be present in a relative reference if it is the same as the base
590 # URI scheme.
591 self.checkJoin(RFC1808_BASE, 'http:', 'http://a/b/c/d;p?q#f')
592 self.checkJoin(RFC1808_BASE, 'http:#', 'http://a/b/c/d;p?q#', relroundtrip=False)
593 self.checkJoin(RFC1808_BASE, 'http:#z', 'http://a/b/c/d;p?q#z')
594 self.checkJoin(RFC1808_BASE, 'http:?', 'http://a/b/c/d;p?', relroundtrip=False)
595 self.checkJoin(RFC1808_BASE, 'http:?#z', 'http://a/b/c/d;p?#z', relroundtrip=False)
596 self.checkJoin(RFC1808_BASE, 'http:?y', 'http://a/b/c/d;p?y')
597 self.checkJoin(RFC1808_BASE, 'http:;', 'http://a/b/c/;')
598 self.checkJoin(RFC1808_BASE, 'http:;?y', 'http://a/b/c/;?y')
599 self.checkJoin(RFC1808_BASE, 'http:;#z', 'http://a/b/c/;#z')
600 self.checkJoin(RFC1808_BASE, 'http:;x', 'http://a/b/c/;x')
601 self.checkJoin(RFC1808_BASE, 'http:/w', 'http://a/w')
602 self.checkJoin(RFC1808_BASE, 'http://', 'http://a/b/c/d;p?q#f')
603 self.checkJoin(RFC1808_BASE, 'http://#z', 'http://a/b/c/d;p?q#z')
604 self.checkJoin(RFC1808_BASE, 'http://?y', 'http://a/b/c/d;p?y')
605 self.checkJoin(RFC1808_BASE, 'http://;x', 'http://;x')
606 self.checkJoin(RFC1808_BASE, 'http:///w', 'http://a/w')
607 self.checkJoin(RFC1808_BASE, 'http://v', 'http://v')
608 # Different scheme is not ignored.
609 self.checkJoin(RFC1808_BASE, 'https:', 'https:', relroundtrip=False)
610 self.checkJoin(RFC1808_BASE, 'https:#', 'https:#', relroundtrip=False)
611 self.checkJoin(RFC1808_BASE, 'https:#z', 'https:#z', relroundtrip=False)
612 self.checkJoin(RFC1808_BASE, 'https:?', 'https:?', relroundtrip=False)
613 self.checkJoin(RFC1808_BASE, 'https:?y', 'https:?y', relroundtrip=False)
614 self.checkJoin(RFC1808_BASE, 'https:;', 'https:;')
615 self.checkJoin(RFC1808_BASE, 'https:;x', 'https:;x')
616
617 def test_urljoins_relative_base(self):
618 # According to RFC 3986, Section 5.1, a base URI must conform to
619 # the absolute-URI syntax rule (Section 4.3). But urljoin() lacks
620 # a context to establish missed components of the relative base URI.
621 # It still has to return a sensible result for backwards compatibility.
622 # The following tests are figments of the imagination and artifacts
623 # of the current implementation that are not based on any standard.
624 self.checkJoin('', '', '')
625 self.checkJoin('', '//', '//', relroundtrip=False)
626 self.checkJoin('', '//v', '//v')
627 self.checkJoin('', '//v/w', '//v/w')
628 self.checkJoin('', '/w', '/w')
629 self.checkJoin('', '///w', '///w', relroundtrip=False)
630 self.checkJoin('', 'w', 'w')
631
632 self.checkJoin('//', '', '//')
633 self.checkJoin('//', '//', '//')
634 self.checkJoin('//', '//v', '//v')
635 self.checkJoin('//', '//v/w', '//v/w')
636 self.checkJoin('//', '/w', '///w')
637 self.checkJoin('//', '///w', '///w')
638 self.checkJoin('//', 'w', '///w')
639
640 self.checkJoin('//a', '', '//a')
641 self.checkJoin('//a', '//', '//a')
642 self.checkJoin('//a', '//v', '//v')
643 self.checkJoin('//a', '//v/w', '//v/w')
644 self.checkJoin('//a', '/w', '//a/w')
645 self.checkJoin('//a', '///w', '//a/w')
646 self.checkJoin('//a', 'w', '//a/w')
647
648 for scheme in '', 'http:':
649 self.checkJoin('http:', scheme + '', 'http:')
650 self.checkJoin('http:', scheme + '//', 'http:')
651 self.checkJoin('http:', scheme + '//v', 'http://v')
652 self.checkJoin('http:', scheme + '//v/w', 'http://v/w')
653 self.checkJoin('http:', scheme + '/w', 'http:/w')
654 self.checkJoin('http:', scheme + '///w', 'http:/w')
655 self.checkJoin('http:', scheme + 'w', 'http:/w')
656
657 self.checkJoin('http://', scheme + '', 'http://')
658 self.checkJoin('http://', scheme + '//', 'http://')
659 self.checkJoin('http://', scheme + '//v', 'http://v')
660 self.checkJoin('http://', scheme + '//v/w', 'http://v/w')
661 self.checkJoin('http://', scheme + '/w', 'http:///w')
662 self.checkJoin('http://', scheme + '///w', 'http:///w')
663 self.checkJoin('http://', scheme + 'w', 'http:///w')
664
665 self.checkJoin('http://a', scheme + '', 'http://a')
666 self.checkJoin('http://a', scheme + '//', 'http://a')
667 self.checkJoin('http://a', scheme + '//v', 'http://v')
668 self.checkJoin('http://a', scheme + '//v/w', 'http://v/w')
669 self.checkJoin('http://a', scheme + '/w', 'http://a/w')
670 self.checkJoin('http://a', scheme + '///w', 'http://a/w')
671 self.checkJoin('http://a', scheme + 'w', 'http://a/w')
672
673 self.checkJoin('/b/c', '', '/b/c')
674 self.checkJoin('/b/c', '//', '/b/c')
675 self.checkJoin('/b/c', '//v', '//v')
676 self.checkJoin('/b/c', '//v/w', '//v/w')
677 self.checkJoin('/b/c', '/w', '/w')
678 self.checkJoin('/b/c', '///w', '/w')
679 self.checkJoin('/b/c', 'w', '/b/w')
680
681 self.checkJoin('///b/c', '', '///b/c')
682 self.checkJoin('///b/c', '//', '///b/c')
683 self.checkJoin('///b/c', '//v', '//v')
684 self.checkJoin('///b/c', '//v/w', '//v/w')
685 self.checkJoin('///b/c', '/w', '///w')
686 self.checkJoin('///b/c', '///w', '///w')
687 self.checkJoin('///b/c', 'w', '///b/w')
688
689 @support.subTests('bytes', (False, True))
690 @support.subTests('url,hostname,port', [
691 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
692 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
693 ('http://[::1]:5432/foo/', '::1', 5432),
694 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
695 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
696 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
697 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
698 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
699 ('http://[::ffff:12.34.56.78]:5432/foo/',
700 '::ffff:12.34.56.78', 5432),
701 ('http://Test.python.org/foo/', 'test.python.org', None),
702 ('http://12.34.56.78/foo/', '12.34.56.78', None),
703 ('http://[::1]/foo/', '::1', None),
704 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
705 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
706 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
707 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
708 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
709 ('http://[::ffff:12.34.56.78]/foo/',
710 '::ffff:12.34.56.78', None),
711 ('http://Test.python.org:/foo/', 'test.python.org', None),
712 ('http://12.34.56.78:/foo/', '12.34.56.78', None),
713 ('http://[::1]:/foo/', '::1', None),
714 ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None),
715 ('http://[dead:beef::]:/foo/', 'dead:beef::', None),
716 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/',
717 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
718 ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None),
719 ('http://[::ffff:12.34.56.78]:/foo/',
720 '::ffff:12.34.56.78', None),
721 ])
722 def test_RFC2732(self, bytes, url, hostname, port):
723 if bytes:
724 url = str_encode(url)
725 hostname = str_encode(hostname)
726 urlparsed = urllib.parse.urlparse(url)
727 self.assertEqual((urlparsed.hostname, urlparsed.port), (hostname, port))
728
729 @support.subTests('bytes', (False, True))
730 @support.subTests('invalid_url', [
731 'http://::12.34.56.78]/',
732 'http://[::1/foo/',
733 'ftp://[::1/foo/bad]/bad',
734 'http://[::1/foo/bad]/bad',
735 'http://[::ffff:12.34.56.78'])
736 def test_RFC2732_invalid(self, bytes, invalid_url):
737 if bytes:
738 invalid_url = str_encode(invalid_url)
739 self.assertRaises(ValueError, urllib.parse.urlparse, invalid_url)
740
741 @support.subTests('bytes', (False, True))
742 @support.subTests('url,defrag,frag', [
743 ('http://python.org#frag', 'http://python.org', 'frag'),
744 ('http://python.org', 'http://python.org', ''),
745 ('http://python.org/#frag', 'http://python.org/', 'frag'),
746 ('http://python.org/', 'http://python.org/', ''),
747 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
748 ('http://python.org/?q', 'http://python.org/?q', ''),
749 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
750 ('http://python.org/p?q', 'http://python.org/p?q', ''),
751 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
752 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
753 ('http://a/b/c;p?q#f', 'http://a/b/c;p?q', 'f'),
754 ('http://a/b/c;p?q#', 'http://a/b/c;p?q', ''),
755 ('http://a/b/c;p?q', 'http://a/b/c;p?q', ''),
756 ('http://a/b/c;p?#f', 'http://a/b/c;p?', 'f'),
757 ('http://a/b/c;p#f', 'http://a/b/c;p', 'f'),
758 ('http://a/b/c;?q#f', 'http://a/b/c;?q', 'f'),
759 ('http://a/b/c?q#f', 'http://a/b/c?q', 'f'),
760 ('http:///b/c;p?q#f', 'http:///b/c;p?q', 'f'),
761 ('http:b/c;p?q#f', 'http:b/c;p?q', 'f'),
762 ('http:;?q#f', 'http:;?q', 'f'),
763 ('http:?q#f', 'http:?q', 'f'),
764 ('//a/b/c;p?q#f', '//a/b/c;p?q', 'f'),
765 ('://a/b/c;p?q#f', '://a/b/c;p?q', 'f'),
766 ])
767 def test_urldefrag(self, bytes, url, defrag, frag):
768 if bytes:
769 url = str_encode(url)
770 defrag = str_encode(defrag)
771 frag = str_encode(frag)
772 result = urllib.parse.urldefrag(url)
773 hash = '#' if isinstance(url, str) else b'#'
774 self.assertEqual(result.geturl(), url.rstrip(hash))
775 self.assertEqual(result, (defrag, frag))
776 self.assertEqual(result.url, defrag)
777 self.assertEqual(result.fragment, frag)
778
779 def test_urlsplit_scoped_IPv6(self):
780 p = urllib.parse.urlsplit('http://[FE80::822a:a8ff:fe49:470c%tESt]:1234')
781 self.assertEqual(p.hostname, "fe80::822a:a8ff:fe49:470c%tESt")
782 self.assertEqual(p.netloc, '[FE80::822a:a8ff:fe49:470c%tESt]:1234')
783
784 p = urllib.parse.urlsplit(b'http://[FE80::822a:a8ff:fe49:470c%tESt]:1234')
785 self.assertEqual(p.hostname, b"fe80::822a:a8ff:fe49:470c%tESt")
786 self.assertEqual(p.netloc, b'[FE80::822a:a8ff:fe49:470c%tESt]:1234')
787
788 def test_urlsplit_attributes(self):
789 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
790 p = urllib.parse.urlsplit(url)
791 self.assertEqual(p.scheme, "http")
792 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
793 self.assertEqual(p.path, "/doc/")
794 self.assertEqual(p.query, "")
795 self.assertEqual(p.fragment, "frag")
796 self.assertEqual(p.username, None)
797 self.assertEqual(p.password, None)
798 self.assertEqual(p.hostname, "www.python.org")
799 self.assertEqual(p.port, None)
800 # geturl() won't return exactly the original URL in this case
801 # since the scheme is always case-normalized
802 # We handle this by ignoring the first 4 characters of the URL
803 self.assertEqual(p.geturl()[4:], url[4:])
804
805 url = "http://User:[email protected]:080/doc/?query=yes#frag"
806 p = urllib.parse.urlsplit(url)
807 self.assertEqual(p.scheme, "http")
808 self.assertEqual(p.netloc, "User:[email protected]:080")
809 self.assertEqual(p.path, "/doc/")
810 self.assertEqual(p.query, "query=yes")
811 self.assertEqual(p.fragment, "frag")
812 self.assertEqual(p.username, "User")
813 self.assertEqual(p.password, "Pass")
814 self.assertEqual(p.hostname, "www.python.org")
815 self.assertEqual(p.port, 80)
816 self.assertEqual(p.geturl(), url)
817
818 # Addressing issue1698, which suggests Username can contain
819 # "@" characters. Though not RFC compliant, many ftp sites allow
820 # and request email addresses as usernames.
821
822 url = "http://[email protected]:[email protected]:080/doc/?query=yes#frag"
823 p = urllib.parse.urlsplit(url)
824 self.assertEqual(p.scheme, "http")
825 self.assertEqual(p.netloc, "[email protected]:[email protected]:080")
826 self.assertEqual(p.path, "/doc/")
827 self.assertEqual(p.query, "query=yes")
828 self.assertEqual(p.fragment, "frag")
829 self.assertEqual(p.username, "[email protected]")
830 self.assertEqual(p.password, "Pass")
831 self.assertEqual(p.hostname, "www.python.org")
832 self.assertEqual(p.port, 80)
833 self.assertEqual(p.geturl(), url)
834
835 # And check them all again, only with bytes this time
836 url = b"HTTP://WWW.PYTHON.ORG/doc/#frag"
837 p = urllib.parse.urlsplit(url)
838 self.assertEqual(p.scheme, b"http")
839 self.assertEqual(p.netloc, b"WWW.PYTHON.ORG")
840 self.assertEqual(p.path, b"/doc/")
841 self.assertEqual(p.query, b"")
842 self.assertEqual(p.fragment, b"frag")
843 self.assertEqual(p.username, None)
844 self.assertEqual(p.password, None)
845 self.assertEqual(p.hostname, b"www.python.org")
846 self.assertEqual(p.port, None)
847 self.assertEqual(p.geturl()[4:], url[4:])
848
849 url = b"http://User:[email protected]:080/doc/?query=yes#frag"
850 p = urllib.parse.urlsplit(url)
851 self.assertEqual(p.scheme, b"http")
852 self.assertEqual(p.netloc, b"User:[email protected]:080")
853 self.assertEqual(p.path, b"/doc/")
854 self.assertEqual(p.query, b"query=yes")
855 self.assertEqual(p.fragment, b"frag")
856 self.assertEqual(p.username, b"User")
857 self.assertEqual(p.password, b"Pass")
858 self.assertEqual(p.hostname, b"www.python.org")
859 self.assertEqual(p.port, 80)
860 self.assertEqual(p.geturl(), url)
861
862 url = b"http://[email protected]:[email protected]:080/doc/?query=yes#frag"
863 p = urllib.parse.urlsplit(url)
864 self.assertEqual(p.scheme, b"http")
865 self.assertEqual(p.netloc, b"[email protected]:[email protected]:080")
866 self.assertEqual(p.path, b"/doc/")
867 self.assertEqual(p.query, b"query=yes")
868 self.assertEqual(p.fragment, b"frag")
869 self.assertEqual(p.username, b"[email protected]")
870 self.assertEqual(p.password, b"Pass")
871 self.assertEqual(p.hostname, b"www.python.org")
872 self.assertEqual(p.port, 80)
873 self.assertEqual(p.geturl(), url)
874
875 # Verify an illegal port raises ValueError
876 url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"
877 p = urllib.parse.urlsplit(url)
878 with self.assertRaisesRegex(ValueError, "out of range"):
879 p.port
880
881 def test_urlsplit_remove_unsafe_bytes(self):
882 # Remove ASCII tabs and newlines from input
883 url = "http\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
884 p = urllib.parse.urlsplit(url)
885 self.assertEqual(p.scheme, "http")
886 self.assertEqual(p.netloc, "www.python.org")
887 self.assertEqual(p.path, "/javascript:alert('msg')/")
888 self.assertEqual(p.query, "query=something")
889 self.assertEqual(p.fragment, "fragment")
890 self.assertEqual(p.username, None)
891 self.assertEqual(p.password, None)
892 self.assertEqual(p.hostname, "www.python.org")
893 self.assertEqual(p.port, None)
894 self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/?query=something#fragment")
895
896 # Remove ASCII tabs and newlines from input as bytes.
897 url = b"http\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
898 p = urllib.parse.urlsplit(url)
899 self.assertEqual(p.scheme, b"http")
900 self.assertEqual(p.netloc, b"www.python.org")
901 self.assertEqual(p.path, b"/javascript:alert('msg')/")
902 self.assertEqual(p.query, b"query=something")
903 self.assertEqual(p.fragment, b"fragment")
904 self.assertEqual(p.username, None)
905 self.assertEqual(p.password, None)
906 self.assertEqual(p.hostname, b"www.python.org")
907 self.assertEqual(p.port, None)
908 self.assertEqual(p.geturl(), b"http://www.python.org/javascript:alert('msg')/?query=something#fragment")
909
910 # with scheme as cache-key
911 url = "http://www.python.org/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
912 scheme = "ht\ntp"
913 for _ in range(2):
914 p = urllib.parse.urlsplit(url, scheme=scheme)
915 self.assertEqual(p.scheme, "http")
916 self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/?query=something#fragment")
917
918 def test_urlsplit_strip_url(self):
919 noise = bytes(range(0, 0x20 + 1))
920 base_url = "http://User:[email protected]:080/doc/?query=yes#frag"
921
922 url = noise.decode("utf-8") + base_url
923 p = urllib.parse.urlsplit(url)
924 self.assertEqual(p.scheme, "http")
925 self.assertEqual(p.netloc, "User:[email protected]:080")
926 self.assertEqual(p.path, "/doc/")
927 self.assertEqual(p.query, "query=yes")
928 self.assertEqual(p.fragment, "frag")
929 self.assertEqual(p.username, "User")
930 self.assertEqual(p.password, "Pass")
931 self.assertEqual(p.hostname, "www.python.org")
932 self.assertEqual(p.port, 80)
933 self.assertEqual(p.geturl(), base_url)
934
935 url = noise + base_url.encode("utf-8")
936 p = urllib.parse.urlsplit(url)
937 self.assertEqual(p.scheme, b"http")
938 self.assertEqual(p.netloc, b"User:[email protected]:080")
939 self.assertEqual(p.path, b"/doc/")
940 self.assertEqual(p.query, b"query=yes")
941 self.assertEqual(p.fragment, b"frag")
942 self.assertEqual(p.username, b"User")
943 self.assertEqual(p.password, b"Pass")
944 self.assertEqual(p.hostname, b"www.python.org")
945 self.assertEqual(p.port, 80)
946 self.assertEqual(p.geturl(), base_url.encode("utf-8"))
947
948 # Test that trailing space is preserved as some applications rely on
949 # this within query strings.
950 query_spaces_url = "https://www.python.org:88/doc/?query= "
951 p = urllib.parse.urlsplit(noise.decode("utf-8") + query_spaces_url)
952 self.assertEqual(p.scheme, "https")
953 self.assertEqual(p.netloc, "www.python.org:88")
954 self.assertEqual(p.path, "/doc/")
955 self.assertEqual(p.query, "query= ")
956 self.assertEqual(p.port, 88)
957 self.assertEqual(p.geturl(), query_spaces_url)
958
959 p = urllib.parse.urlsplit("www.pypi.org ")
960 # That "hostname" gets considered a "path" due to the
961 # trailing space and our existing logic... YUCK...
962 # and re-assembles via geturl aka unurlsplit into the original.
963 # django.core.validators.URLValidator (at least through v3.2) relies on
964 # this, for better or worse, to catch it in a ValidationError via its
965 # regular expressions.
966 # Here we test the basic round trip concept of such a trailing space.
967 self.assertEqual(urllib.parse.urlunsplit(p), "www.pypi.org ")
968
969 # with scheme as cache-key
970 url = "//www.python.org/"
971 scheme = noise.decode("utf-8") + "https" + noise.decode("utf-8")
972 for _ in range(2):
973 p = urllib.parse.urlsplit(url, scheme=scheme)
974 self.assertEqual(p.scheme, "https")
975 self.assertEqual(p.geturl(), "https://www.python.org/")
976
977 @support.subTests('bytes', (False, True))
978 @support.subTests('parse', (urllib.parse.urlsplit, urllib.parse.urlparse))
979 @support.subTests('port', ("foo", "1.5", "-1", "0x10", "-0", "1_1", " 1", "1 ", "६"))
980 def test_attributes_bad_port(self, bytes, parse, port):
981 """Check handling of invalid ports."""
982 netloc = "www.example.net:" + port
983 url = "http://" + netloc + "/"
984 if bytes:
985 if not (netloc.isascii() and port.isascii()):
986 self.skipTest('non-ASCII bytes')
987 netloc = str_encode(netloc)
988 url = str_encode(url)
989 p = parse(url)
990 self.assertEqual(p.netloc, netloc)
991 with self.assertRaises(ValueError):
992 p.port
993
994 @support.subTests('bytes', (False, True))
995 @support.subTests('parse', (urllib.parse.urlsplit, urllib.parse.urlparse))
996 @support.subTests('scheme', (".", "+", "-", "0", "http&", "६http"))
997 def test_attributes_bad_scheme(self, bytes, parse, scheme):
998 """Check handling of invalid schemes."""
999 url = scheme + "://www.example.net"
1000 if bytes:
1001 if not url.isascii():
1002 self.skipTest('non-ASCII bytes')
1003 url = url.encode("ascii")
1004 p = parse(url)
1005 self.assertEqual(p.scheme, b"" if bytes else "")
1006
1007 def test_attributes_without_netloc(self):
1008 # This example is straight from RFC 3261. It looks like it
1009 # should allow the username, hostname, and port to be filled
1010 # in, but doesn't. Since it's a URI and doesn't use the
1011 # scheme://netloc syntax, the netloc and related attributes
1012 # should be left empty.
1013 uri = "sip:[email protected];maddr=239.255.255.1;ttl=15"
1014 p = urllib.parse.urlsplit(uri)
1015 self.assertEqual(p.netloc, "")
1016 self.assertEqual(p.username, None)
1017 self.assertEqual(p.password, None)
1018 self.assertEqual(p.hostname, None)
1019 self.assertEqual(p.port, None)
1020 self.assertEqual(p.geturl(), uri)
1021
1022 p = urllib.parse.urlparse(uri)
1023 self.assertEqual(p.netloc, "")
1024 self.assertEqual(p.username, None)
1025 self.assertEqual(p.password, None)
1026 self.assertEqual(p.hostname, None)
1027 self.assertEqual(p.port, None)
1028 self.assertEqual(p.geturl(), uri)
1029
1030 # You guessed it, repeating the test with bytes input
1031 uri = b"sip:[email protected];maddr=239.255.255.1;ttl=15"
1032 p = urllib.parse.urlsplit(uri)
1033 self.assertEqual(p.netloc, b"")
1034 self.assertEqual(p.username, None)
1035 self.assertEqual(p.password, None)
1036 self.assertEqual(p.hostname, None)
1037 self.assertEqual(p.port, None)
1038 self.assertEqual(p.geturl(), uri)
1039
1040 p = urllib.parse.urlparse(uri)
1041 self.assertEqual(p.netloc, b"")
1042 self.assertEqual(p.username, None)
1043 self.assertEqual(p.password, None)
1044 self.assertEqual(p.hostname, None)
1045 self.assertEqual(p.port, None)
1046 self.assertEqual(p.geturl(), uri)
1047
1048 def test_noslash(self):
1049 # Issue 1637: http://foo.com?query is legal
1050 self.assertEqual(urllib.parse.urlparse("http://example.com?blahblah=/foo"),
1051 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
1052 self.assertEqual(urllib.parse.urlparse(b"http://example.com?blahblah=/foo"),
1053 (b'http', b'example.com', b'', b'', b'blahblah=/foo', b''))
1054
1055 def test_withoutscheme(self):
1056 # Test urlparse without scheme
1057 # Issue 754016: urlparse goes wrong with IP:port without scheme
1058 # RFC 1808 specifies that netloc should start with //, urlparse expects
1059 # the same, otherwise it classifies the portion of url as path.
1060 self.assertEqual(urllib.parse.urlparse("path"),
1061 ('','','path','','',''))
1062 self.assertEqual(urllib.parse.urlparse("//www.python.org:80"),
1063 ('','www.python.org:80','','','',''))
1064 self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
1065 ('http','www.python.org:80','','','',''))
1066 # Repeat for bytes input
1067 self.assertEqual(urllib.parse.urlparse(b"path"),
1068 (b'',b'',b'path',b'',b'',b''))
1069 self.assertEqual(urllib.parse.urlparse(b"//www.python.org:80"),
1070 (b'',b'www.python.org:80',b'',b'',b'',b''))
1071 self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
1072 (b'http',b'www.python.org:80',b'',b'',b'',b''))
1073
1074 def test_portseparator(self):
1075 # Issue 754016 makes changes for port separator ':' from scheme separator
1076 self.assertEqual(urllib.parse.urlparse("http:80"), ('http','','80','','',''))
1077 self.assertEqual(urllib.parse.urlparse("https:80"), ('https','','80','','',''))
1078 self.assertEqual(urllib.parse.urlparse("path:80"), ('path','','80','','',''))
1079 self.assertEqual(urllib.parse.urlparse("http:"),('http','','','','',''))
1080 self.assertEqual(urllib.parse.urlparse("https:"),('https','','','','',''))
1081 self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
1082 ('http','www.python.org:80','','','',''))
1083 # As usual, need to check bytes input as well
1084 self.assertEqual(urllib.parse.urlparse(b"http:80"), (b'http',b'',b'80',b'',b'',b''))
1085 self.assertEqual(urllib.parse.urlparse(b"https:80"), (b'https',b'',b'80',b'',b'',b''))
1086 self.assertEqual(urllib.parse.urlparse(b"path:80"), (b'path',b'',b'80',b'',b'',b''))
1087 self.assertEqual(urllib.parse.urlparse(b"http:"),(b'http',b'',b'',b'',b'',b''))
1088 self.assertEqual(urllib.parse.urlparse(b"https:"),(b'https',b'',b'',b'',b'',b''))
1089 self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
1090 (b'http',b'www.python.org:80',b'',b'',b'',b''))
1091
1092 def test_usingsys(self):
1093 # Issue 3314: sys module is used in the error
1094 self.assertRaises(TypeError, urllib.parse.urlencode, "foo")
1095
1096 def test_anyscheme(self):
1097 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
1098 self.assertEqual(urllib.parse.urlparse("s3://foo.com/stuff"),
1099 ('s3', 'foo.com', '/stuff', '', '', ''))
1100 self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"),
1101 ('x-newscheme', 'foo.com', '/stuff', '', '', ''))
1102 self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query#fragment"),
1103 ('x-newscheme', 'foo.com', '/stuff', '', 'query', 'fragment'))
1104 self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query"),
1105 ('x-newscheme', 'foo.com', '/stuff', '', 'query', ''))
1106
1107 # And for bytes...
1108 self.assertEqual(urllib.parse.urlparse(b"s3://foo.com/stuff"),
1109 (b's3', b'foo.com', b'/stuff', b'', b'', b''))
1110 self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff"),
1111 (b'x-newscheme', b'foo.com', b'/stuff', b'', b'', b''))
1112 self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query#fragment"),
1113 (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'fragment'))
1114 self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query"),
1115 (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b''))
1116
1117 @support.subTests('func', (urllib.parse.urlparse, urllib.parse.urlsplit))
1118 def test_default_scheme(self, func):
1119 # Exercise the scheme parameter of urlparse() and urlsplit()
1120 result = func("http://example.net/", "ftp")
1121 self.assertEqual(result.scheme, "http")
1122 result = func(b"http://example.net/", b"ftp")
1123 self.assertEqual(result.scheme, b"http")
1124 self.assertEqual(func("path", "ftp").scheme, "ftp")
1125 self.assertEqual(func("path", scheme="ftp").scheme, "ftp")
1126 self.assertEqual(func(b"path", scheme=b"ftp").scheme, b"ftp")
1127 self.assertEqual(func("path").scheme, "")
1128 self.assertEqual(func(b"path").scheme, b"")
1129 self.assertEqual(func(b"path", "").scheme, b"")
1130
1131 @support.subTests('url,attr,expected_frag', (
1132 ("http:#frag", "path", "frag"),
1133 ("//example.net#frag", "path", "frag"),
1134 ("index.html#frag", "path", "frag"),
1135 (";a=b#frag", "params", "frag"),
1136 ("?a=b#frag", "query", "frag"),
1137 ("#frag", "path", "frag"),
1138 ("abc#@frag", "path", "@frag"),
1139 ("//abc#@frag", "path", "@frag"),
1140 ("//abc:80#@frag", "path", "@frag"),
1141 ("//abc#@frag:80", "path", "@frag:80"),
1142 ))
1143 @support.subTests('func', (urllib.parse.urlparse, urllib.parse.urlsplit))
1144 def test_parse_fragments(self, url, attr, expected_frag, func):
1145 # Exercise the allow_fragments parameter of urlparse() and urlsplit()
1146 if attr == "params" and func is urllib.parse.urlsplit:
1147 attr = "path"
1148 result = func(url, allow_fragments=False)
1149 self.assertEqual(result.fragment, "")
1150 self.assertEndsWith(getattr(result, attr),
1151 "#" + expected_frag)
1152 self.assertEqual(func(url, "", False).fragment, "")
1153
1154 result = func(url, allow_fragments=True)
1155 self.assertEqual(result.fragment, expected_frag)
1156 self.assertNotEndsWith(getattr(result, attr), expected_frag)
1157 self.assertEqual(func(url, "", True).fragment,
1158 expected_frag)
1159 self.assertEqual(func(url).fragment, expected_frag)
1160
1161 def test_mixed_types_rejected(self):
1162 # Several functions that process either strings or ASCII encoded bytes
1163 # accept multiple arguments. Check they reject mixed type input
1164 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1165 urllib.parse.urlparse("www.python.org", b"http")
1166 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1167 urllib.parse.urlparse(b"www.python.org", "http")
1168 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1169 urllib.parse.urlsplit("www.python.org", b"http")
1170 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1171 urllib.parse.urlsplit(b"www.python.org", "http")
1172 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1173 urllib.parse.urlunparse(( b"http", "www.python.org","","","",""))
1174 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1175 urllib.parse.urlunparse(("http", b"www.python.org","","","",""))
1176 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1177 urllib.parse.urlunsplit((b"http", "www.python.org","","",""))
1178 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1179 urllib.parse.urlunsplit(("http", b"www.python.org","","",""))
1180 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1181 urllib.parse.urljoin("http://python.org", b"http://python.org")
1182 with self.assertRaisesRegex(TypeError, "Cannot mix str"):
1183 urllib.parse.urljoin(b"http://python.org", "http://python.org")
1184
1185 @support.subTests('result_type', [
1186 urllib.parse.DefragResult,
1187 urllib.parse.SplitResult,
1188 urllib.parse.ParseResult,
1189 ])
1190 def test_result_pairs(self, result_type):
1191 # Check encoding and decoding between result pairs
1192 str_type = result_type
1193 num_args = len(str_type._fields)
1194 bytes_type = str_type._encoded_counterpart
1195 self.assertIs(bytes_type._decoded_counterpart, str_type)
1196 str_args = ('',) * num_args
1197 bytes_args = (b'',) * num_args
1198 str_result = str_type(*str_args)
1199 bytes_result = bytes_type(*bytes_args)
1200 encoding = 'ascii'
1201 errors = 'strict'
1202 self.assertEqual(str_result, str_args)
1203 self.assertEqual(bytes_result.decode(), str_args)
1204 self.assertEqual(bytes_result.decode(), str_result)
1205 self.assertEqual(bytes_result.decode(encoding), str_args)
1206 self.assertEqual(bytes_result.decode(encoding), str_result)
1207 self.assertEqual(bytes_result.decode(encoding, errors), str_args)
1208 self.assertEqual(bytes_result.decode(encoding, errors), str_result)
1209 self.assertEqual(bytes_result, bytes_args)
1210 self.assertEqual(str_result.encode(), bytes_args)
1211 self.assertEqual(str_result.encode(), bytes_result)
1212 self.assertEqual(str_result.encode(encoding), bytes_args)
1213 self.assertEqual(str_result.encode(encoding), bytes_result)
1214 self.assertEqual(str_result.encode(encoding, errors), bytes_args)
1215 self.assertEqual(str_result.encode(encoding, errors), bytes_result)
1216
1217 def test_parse_qs_encoding(self):
1218 result = urllib.parse.parse_qs("key=\u0141%E9", encoding="latin-1")
1219 self.assertEqual(result, {'key': ['\u0141\xE9']})
1220 result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="utf-8")
1221 self.assertEqual(result, {'key': ['\u0141\xE9']})
1222 result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="ascii")
1223 self.assertEqual(result, {'key': ['\u0141\ufffd\ufffd']})
1224 result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii")
1225 self.assertEqual(result, {'key': ['\u0141\ufffd-']})
1226 result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii",
1227 errors="ignore")
1228 self.assertEqual(result, {'key': ['\u0141-']})
1229
1230 def test_parse_qsl_encoding(self):
1231 result = urllib.parse.parse_qsl("key=\u0141%E9", encoding="latin-1")
1232 self.assertEqual(result, [('key', '\u0141\xE9')])
1233 result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="utf-8")
1234 self.assertEqual(result, [('key', '\u0141\xE9')])
1235 result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="ascii")
1236 self.assertEqual(result, [('key', '\u0141\ufffd\ufffd')])
1237 result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii")
1238 self.assertEqual(result, [('key', '\u0141\ufffd-')])
1239 result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii",
1240 errors="ignore")
1241 self.assertEqual(result, [('key', '\u0141-')])
1242
1243 def test_parse_qsl_max_num_fields(self):
1244 with self.assertRaises(ValueError):
1245 urllib.parse.parse_qsl('&'.join(['a=a']*11), max_num_fields=10)
1246 urllib.parse.parse_qsl('&'.join(['a=a']*10), max_num_fields=10)
1247
1248 @support.subTests('orig,expect', [
1249 (";", {}),
1250 (";;", {}),
1251 (";a=b", {'a': ['b']}),
1252 ("a=a+b;b=b+c", {'a': ['a b'], 'b': ['b c']}),
1253 ("a=1;a=2", {'a': ['1', '2']}),
1254 (b";", {}),
1255 (b";;", {}),
1256 (b";a=b", {b'a': [b'b']}),
1257 (b"a=a+b;b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
1258 (b"a=1;a=2", {b'a': [b'1', b'2']}),
1259 ])
1260 def test_parse_qs_separator(self, orig, expect):
1261 result = urllib.parse.parse_qs(orig, separator=';')
1262 self.assertEqual(result, expect)
1263 result_bytes = urllib.parse.parse_qs(orig, separator=b';')
1264 self.assertEqual(result_bytes, expect)
1265
1266 @support.subTests('orig,expect', [
1267 (";", []),
1268 (";;", []),
1269 (";a=b", [('a', 'b')]),
1270 ("a=a+b;b=b+c", [('a', 'a b'), ('b', 'b c')]),
1271 ("a=1;a=2", [('a', '1'), ('a', '2')]),
1272 (b";", []),
1273 (b";;", []),
1274 (b";a=b", [(b'a', b'b')]),
1275 (b"a=a+b;b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
1276 (b"a=1;a=2", [(b'a', b'1'), (b'a', b'2')]),
1277 ])
1278 def test_parse_qsl_separator(self, orig, expect):
1279 result = urllib.parse.parse_qsl(orig, separator=';')
1280 self.assertEqual(result, expect)
1281 result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
1282 self.assertEqual(result_bytes, expect)
1283
1284 def test_parse_qsl_bytes(self):
1285 self.assertEqual(urllib.parse.parse_qsl(b'a=b'), [(b'a', b'b')])
1286 self.assertEqual(urllib.parse.parse_qsl(bytearray(b'a=b')), [(b'a', b'b')])
1287 self.assertEqual(urllib.parse.parse_qsl(memoryview(b'a=b')), [(b'a', b'b')])
1288
1289 def test_parse_qsl_false_value(self):
1290 kwargs = dict(keep_blank_values=True, strict_parsing=True)
1291 for x in '', b'', None, memoryview(b''):
1292 self.assertEqual(urllib.parse.parse_qsl(x, **kwargs), [])
1293 self.assertRaises(ValueError, urllib.parse.parse_qsl, x, separator=1)
1294 for x in 0, 0.0, [], {}:
1295 with self.assertWarns(DeprecationWarning) as cm:
1296 self.assertEqual(urllib.parse.parse_qsl(x, **kwargs), [])
1297 self.assertEqual(cm.filename, __file__)
1298 with self.assertWarns(DeprecationWarning) as cm:
1299 self.assertEqual(urllib.parse.parse_qs(x, **kwargs), {})
1300 self.assertEqual(cm.filename, __file__)
1301 self.assertRaises(ValueError, urllib.parse.parse_qsl, x, separator=1)
1302
1303 def test_parse_qsl_errors(self):
1304 self.assertRaises(TypeError, urllib.parse.parse_qsl, list(b'a=b'))
1305 self.assertRaises(TypeError, urllib.parse.parse_qsl, iter(b'a=b'))
1306 self.assertRaises(TypeError, urllib.parse.parse_qsl, 1)
1307 self.assertRaises(TypeError, urllib.parse.parse_qsl, object())
1308
1309 for separator in '', b'', None, 0, 1, 0.0, 1.5:
1310 with self.assertRaises(ValueError):
1311 urllib.parse.parse_qsl('a=b', separator=separator)
1312 with self.assertRaises(UnicodeEncodeError):
1313 urllib.parse.parse_qsl(b'a=b', separator='\xa6')
1314 with self.assertRaises(UnicodeDecodeError):
1315 urllib.parse.parse_qsl('a=b', separator=b'\xa6')
1316
1317 def test_urlencode_sequences(self):
1318 # Other tests incidentally urlencode things; test non-covered cases:
1319 # Sequence and object values.
1320 result = urllib.parse.urlencode({'a': [1, 2], 'b': (3, 4, 5)}, True)
1321 # we cannot rely on ordering here
1322 assert set(result.split('&')) == {'a=1', 'a=2', 'b=3', 'b=4', 'b=5'}
1323
1324 class Trivial:
1325 def __str__(self):
1326 return 'trivial'
1327
1328 result = urllib.parse.urlencode({'a': Trivial()}, True)
1329 self.assertEqual(result, 'a=trivial')
1330
1331 def test_urlencode_quote_via(self):
1332 result = urllib.parse.urlencode({'a': 'some value'})
1333 self.assertEqual(result, "a=some+value")
1334 result = urllib.parse.urlencode({'a': 'some value/another'},
1335 quote_via=urllib.parse.quote)
1336 self.assertEqual(result, "a=some%20value%2Fanother")
1337 result = urllib.parse.urlencode({'a': 'some value/another'},
1338 safe='/', quote_via=urllib.parse.quote)
1339 self.assertEqual(result, "a=some%20value/another")
1340
1341 def test_quote_from_bytes(self):
1342 self.assertRaises(TypeError, urllib.parse.quote_from_bytes, 'foo')
1343 result = urllib.parse.quote_from_bytes(b'archaeological arcana')
1344 self.assertEqual(result, 'archaeological%20arcana')
1345 result = urllib.parse.quote_from_bytes(b'')
1346 self.assertEqual(result, '')
1347 result = urllib.parse.quote_from_bytes(b'A'*10_000)
1348 self.assertEqual(result, 'A'*10_000)
1349 result = urllib.parse.quote_from_bytes(b'z\x01/ '*253_183)
1350 self.assertEqual(result, 'z%01/%20'*253_183)
1351
1352 def test_unquote_to_bytes(self):
1353 result = urllib.parse.unquote_to_bytes('abc%20def')
1354 self.assertEqual(result, b'abc def')
1355 result = urllib.parse.unquote_to_bytes('')
1356 self.assertEqual(result, b'')
1357
1358 def test_quote_errors(self):
1359 self.assertRaises(TypeError, urllib.parse.quote, b'foo',
1360 encoding='utf-8')
1361 self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict')
1362
1363 def test_issue14072(self):
1364 p1 = urllib.parse.urlsplit('tel:+31-641044153')
1365 self.assertEqual(p1.scheme, 'tel')
1366 self.assertEqual(p1.path, '+31-641044153')
1367 p2 = urllib.parse.urlsplit('tel:+31641044153')
1368 self.assertEqual(p2.scheme, 'tel')
1369 self.assertEqual(p2.path, '+31641044153')
1370 # assert the behavior for urlparse
1371 p1 = urllib.parse.urlparse('tel:+31-641044153')
1372 self.assertEqual(p1.scheme, 'tel')
1373 self.assertEqual(p1.path, '+31-641044153')
1374 p2 = urllib.parse.urlparse('tel:+31641044153')
1375 self.assertEqual(p2.scheme, 'tel')
1376 self.assertEqual(p2.path, '+31641044153')
1377
1378 def test_invalid_bracketed_hosts(self):
1379 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[192.0.2.146]/Path?Query')
1380 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[important.com:8000]/Path?Query')
1381 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[v123r.IP]/Path?Query')
1382 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[v12ae]/Path?Query')
1383 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[v.IP]/Path?Query')
1384 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[v123.]/Path?Query')
1385 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[v]/Path?Query')
1386 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[0439:23af::2309::fae7:1234]/Path?Query')
1387 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[0439:23af:2309::fae7:1234:2342:438e:192.0.2.146]/Path?Query')
1388 self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@]v6a.ip[/Path')
1389 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]')
1390 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix')
1391 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]/')
1392 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix/')
1393 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]?')
1394 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix?')
1395 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]')
1396 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix')
1397 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]/')
1398 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix/')
1399 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]?')
1400 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix?')
1401 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]:a')
1402 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix:a')
1403 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]:a1')
1404 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix:a1')
1405 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]:1a')
1406 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix:1a')
1407 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]:')
1408 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix:/')
1409 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]:?')
1410 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://user@prefix.[v6a.ip]')
1411 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://user@[v6a.ip].suffix')
1412 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip')
1413 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://v6a.ip]')
1414 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://]v6a.ip[')
1415 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://]v6a.ip')
1416 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://v6a.ip[')
1417 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip')
1418 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://v6a.ip].suffix')
1419 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix]v6a.ip[suffix')
1420 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix]v6a.ip')
1421 self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://v6a.ip[suffix')
1422
1423 def test_splitting_bracketed_hosts(self):
1424 p1 = urllib.parse.urlsplit('scheme://user@[v6a.ip]:1234/path?query')
1425 self.assertEqual(p1.hostname, 'v6a.ip')
1426 self.assertEqual(p1.username, 'user')
1427 self.assertEqual(p1.path, '/path')
1428 self.assertEqual(p1.port, 1234)
1429 p2 = urllib.parse.urlsplit('scheme://user@[0439:23af:2309::fae7%test]/path?query')
1430 self.assertEqual(p2.hostname, '0439:23af:2309::fae7%test')
1431 self.assertEqual(p2.username, 'user')
1432 self.assertEqual(p2.path, '/path')
1433 self.assertIs(p2.port, None)
1434 p3 = urllib.parse.urlsplit('scheme://user@[0439:23af:2309::fae7:1234:192.0.2.146%test]/path?query')
1435 self.assertEqual(p3.hostname, '0439:23af:2309::fae7:1234:192.0.2.146%test')
1436 self.assertEqual(p3.username, 'user')
1437 self.assertEqual(p3.path, '/path')
1438
1439 def test_port_casting_failure_message(self):
1440 message = "Port could not be cast to integer value as 'oracle'"
1441 p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle')
1442 with self.assertRaisesRegex(ValueError, message):
1443 p1.port
1444
1445 p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle')
1446 with self.assertRaisesRegex(ValueError, message):
1447 p2.port
1448
1449 def test_telurl_params(self):
1450 p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516')
1451 self.assertEqual(p1.scheme, 'tel')
1452 self.assertEqual(p1.path, '123-4')
1453 self.assertEqual(p1.params, 'phone-context=+1-650-516')
1454
1455 p1 = urllib.parse.urlparse('tel:+1-201-555-0123')
1456 self.assertEqual(p1.scheme, 'tel')
1457 self.assertEqual(p1.path, '+1-201-555-0123')
1458 self.assertEqual(p1.params, '')
1459
1460 p1 = urllib.parse.urlparse('tel:7042;phone-context=example.com')
1461 self.assertEqual(p1.scheme, 'tel')
1462 self.assertEqual(p1.path, '7042')
1463 self.assertEqual(p1.params, 'phone-context=example.com')
1464
1465 p1 = urllib.parse.urlparse('tel:863-1234;phone-context=+1-914-555')
1466 self.assertEqual(p1.scheme, 'tel')
1467 self.assertEqual(p1.path, '863-1234')
1468 self.assertEqual(p1.params, 'phone-context=+1-914-555')
1469
1470 def test_Quoter_repr(self):
1471 quoter = urllib.parse._Quoter(urllib.parse._ALWAYS_SAFE)
1472 self.assertIn('Quoter', repr(quoter))
1473
1474 def test_clear_cache_for_code_coverage(self):
1475 urllib.parse.clear_cache()
1476
1477 def test_urllib_parse_getattr_failure(self):
1478 """Test that urllib.parse.__getattr__() fails correctly."""
1479 with self.assertRaises(AttributeError):
1480 unused = urllib.parse.this_does_not_exist
1481
1482 def test_all(self):
1483 expected = []
1484 undocumented = {
1485 'splitattr', 'splithost', 'splitnport', 'splitpasswd',
1486 'splitport', 'splitquery', 'splittag', 'splittype', 'splituser',
1487 'splitvalue',
1488 'ResultBase', 'clear_cache', 'to_bytes', 'unwrap',
1489 }
1490 for name in dir(urllib.parse):
1491 if name.startswith('_') or name in undocumented:
1492 continue
1493 object = getattr(urllib.parse, name)
1494 if getattr(object, '__module__', None) == 'urllib.parse':
1495 expected.append(name)
1496 self.assertCountEqual(urllib.parse.__all__, expected)
1497
1498 def test_urlsplit_normalization(self):
1499 # Certain characters should never occur in the netloc,
1500 # including under normalization.
1501 # Ensure that ALL of them are detected and cause an error
1502 illegal_chars = '/:#?@'
1503 hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
1504 denorm_chars = [
1505 c for c in map(chr, range(128, sys.maxunicode))
1506 if unicodedata.decomposition(c)
1507 and (hex_chars & set(unicodedata.decomposition(c).split()))
1508 and c not in illegal_chars
1509 ]
1510 # Sanity check that we found at least one such character
1511 self.assertIn('\u2100', denorm_chars)
1512 self.assertIn('\uFF03', denorm_chars)
1513
1514 # bpo-36742: Verify port separators are ignored when they
1515 # existed prior to decomposition
1516 urllib.parse.urlsplit('http://\u30d5\u309a:80')
1517 with self.assertRaises(ValueError):
1518 urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
1519
1520 for scheme in ["http", "https", "ftp"]:
1521 for netloc in ["netloc{}false.netloc", "n{}user@netloc"]:
1522 for c in denorm_chars:
1523 url = "{}://{}/path".format(scheme, netloc.format(c))
1524 with self.subTest(url=url, char='{:04X}'.format(ord(c))):
1525 with self.assertRaises(ValueError):
1526 urllib.parse.urlsplit(url)
1527
1528 class Utility_Tests(unittest.TestCase):
1529 """Testcase to test the various utility functions in the urllib."""
1530 # In Python 2 this test class was in test_urllib.
1531
1532 def test_splittype(self):
1533 splittype = urllib.parse._splittype
1534 self.assertEqual(splittype('type:opaquestring'), ('type', 'opaquestring'))
1535 self.assertEqual(splittype('opaquestring'), (None, 'opaquestring'))
1536 self.assertEqual(splittype(':opaquestring'), (None, ':opaquestring'))
1537 self.assertEqual(splittype('type:'), ('type', ''))
1538 self.assertEqual(splittype('type:opaque:string'), ('type', 'opaque:string'))
1539
1540 def test_splithost(self):
1541 splithost = urllib.parse._splithost
1542 self.assertEqual(splithost('//www.example.org:80/foo/bar/baz.html'),
1543 ('www.example.org:80', '/foo/bar/baz.html'))
1544 self.assertEqual(splithost('//www.example.org:80'),
1545 ('www.example.org:80', ''))
1546 self.assertEqual(splithost('/foo/bar/baz.html'),
1547 (None, '/foo/bar/baz.html'))
1548
1549 # bpo-30500: # starts a fragment.
1550 self.assertEqual(splithost('//127.0.0.1#@host.com'),
1551 ('127.0.0.1', '/#@host.com'))
1552 self.assertEqual(splithost('//127.0.0.1#@host.com:80'),
1553 ('127.0.0.1', '/#@host.com:80'))
1554 self.assertEqual(splithost('//127.0.0.1:80#@host.com'),
1555 ('127.0.0.1:80', '/#@host.com'))
1556
1557 # Empty host is returned as empty string.
1558 self.assertEqual(splithost("///file"),
1559 ('', '/file'))
1560
1561 # Trailing semicolon, question mark and hash symbol are kept.
1562 self.assertEqual(splithost("//example.net/file;"),
1563 ('example.net', '/file;'))
1564 self.assertEqual(splithost("//example.net/file?"),
1565 ('example.net', '/file?'))
1566 self.assertEqual(splithost("//example.net/file#"),
1567 ('example.net', '/file#'))
1568
1569 def test_splituser(self):
1570 splituser = urllib.parse._splituser
1571 self.assertEqual(splituser('User:[email protected]:080'),
1572 ('User:Pass', 'www.python.org:080'))
1573 self.assertEqual(splituser('@www.python.org:080'),
1574 ('', 'www.python.org:080'))
1575 self.assertEqual(splituser('www.python.org:080'),
1576 (None, 'www.python.org:080'))
1577 self.assertEqual(splituser('User:Pass@'),
1578 ('User:Pass', ''))
1579 self.assertEqual(splituser('[email protected]:[email protected]:080'),
1580 ('[email protected]:Pass', 'www.python.org:080'))
1581
1582 def test_splitpasswd(self):
1583 # Some of the password examples are not sensible, but it is added to
1584 # confirming to RFC2617 and addressing issue4675.
1585 splitpasswd = urllib.parse._splitpasswd
1586 self.assertEqual(splitpasswd('user:ab'), ('user', 'ab'))
1587 self.assertEqual(splitpasswd('user:a\nb'), ('user', 'a\nb'))
1588 self.assertEqual(splitpasswd('user:a\tb'), ('user', 'a\tb'))
1589 self.assertEqual(splitpasswd('user:a\rb'), ('user', 'a\rb'))
1590 self.assertEqual(splitpasswd('user:a\fb'), ('user', 'a\fb'))
1591 self.assertEqual(splitpasswd('user:a\vb'), ('user', 'a\vb'))
1592 self.assertEqual(splitpasswd('user:a:b'), ('user', 'a:b'))
1593 self.assertEqual(splitpasswd('user:a b'), ('user', 'a b'))
1594 self.assertEqual(splitpasswd('user 2:ab'), ('user 2', 'ab'))
1595 self.assertEqual(splitpasswd('user+1:a+b'), ('user+1', 'a+b'))
1596 self.assertEqual(splitpasswd('user:'), ('user', ''))
1597 self.assertEqual(splitpasswd('user'), ('user', None))
1598 self.assertEqual(splitpasswd(':ab'), ('', 'ab'))
1599
1600 def test_splitport(self):
1601 splitport = urllib.parse._splitport
1602 self.assertEqual(splitport('parrot:88'), ('parrot', '88'))
1603 self.assertEqual(splitport('parrot'), ('parrot', None))
1604 self.assertEqual(splitport('parrot:'), ('parrot', None))
1605 self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None))
1606 self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None))
1607 self.assertEqual(splitport('[::1]:88'), ('[::1]', '88'))
1608 self.assertEqual(splitport('[::1]'), ('[::1]', None))
1609 self.assertEqual(splitport(':88'), ('', '88'))
1610
1611 def test_splitnport(self):
1612 splitnport = urllib.parse._splitnport
1613 self.assertEqual(splitnport('parrot:88'), ('parrot', 88))
1614 self.assertEqual(splitnport('parrot'), ('parrot', -1))
1615 self.assertEqual(splitnport('parrot', 55), ('parrot', 55))
1616 self.assertEqual(splitnport('parrot:'), ('parrot', -1))
1617 self.assertEqual(splitnport('parrot:', 55), ('parrot', 55))
1618 self.assertEqual(splitnport('127.0.0.1'), ('127.0.0.1', -1))
1619 self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55))
1620 self.assertEqual(splitnport('parrot:cheese'), ('parrot', None))
1621 self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None))
1622 self.assertEqual(splitnport('parrot: +1_0 '), ('parrot', None))
1623
1624 def test_splitquery(self):
1625 # Normal cases are exercised by other tests; ensure that we also
1626 # catch cases with no port specified (testcase ensuring coverage)
1627 splitquery = urllib.parse._splitquery
1628 self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
1629 ('http://python.org/fake', 'foo=bar'))
1630 self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
1631 ('http://python.org/fake?foo=bar', ''))
1632 self.assertEqual(splitquery('http://python.org/fake'),
1633 ('http://python.org/fake', None))
1634 self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar'))
1635
1636 def test_splittag(self):
1637 splittag = urllib.parse._splittag
1638 self.assertEqual(splittag('http://example.com?foo=bar#baz'),
1639 ('http://example.com?foo=bar', 'baz'))
1640 self.assertEqual(splittag('http://example.com?foo=bar#'),
1641 ('http://example.com?foo=bar', ''))
1642 self.assertEqual(splittag('#baz'), ('', 'baz'))
1643 self.assertEqual(splittag('http://example.com?foo=bar'),
1644 ('http://example.com?foo=bar', None))
1645 self.assertEqual(splittag('http://example.com?foo=bar#baz#boo'),
1646 ('http://example.com?foo=bar#baz', 'boo'))
1647
1648 def test_splitattr(self):
1649 splitattr = urllib.parse._splitattr
1650 self.assertEqual(splitattr('/path;attr1=value1;attr2=value2'),
1651 ('/path', ['attr1=value1', 'attr2=value2']))
1652 self.assertEqual(splitattr('/path;'), ('/path', ['']))
1653 self.assertEqual(splitattr(';attr1=value1;attr2=value2'),
1654 ('', ['attr1=value1', 'attr2=value2']))
1655 self.assertEqual(splitattr('/path'), ('/path', []))
1656
1657 def test_splitvalue(self):
1658 # Normal cases are exercised by other tests; test pathological cases
1659 # with no key/value pairs. (testcase ensuring coverage)
1660 splitvalue = urllib.parse._splitvalue
1661 self.assertEqual(splitvalue('foo=bar'), ('foo', 'bar'))
1662 self.assertEqual(splitvalue('foo='), ('foo', ''))
1663 self.assertEqual(splitvalue('=bar'), ('', 'bar'))
1664 self.assertEqual(splitvalue('foobar'), ('foobar', None))
1665 self.assertEqual(splitvalue('foo=bar=baz'), ('foo', 'bar=baz'))
1666
1667 def test_to_bytes(self):
1668 result = urllib.parse._to_bytes('http://www.python.org')
1669 self.assertEqual(result, 'http://www.python.org')
1670 self.assertRaises(UnicodeError, urllib.parse._to_bytes,
1671 'http://www.python.org/medi\u00e6val')
1672
1673 @support.subTests('wrapped_url',
1674 ('<URL:scheme://host/path>', '<scheme://host/path>',
1675 'URL:scheme://host/path', 'scheme://host/path'))
1676 def test_unwrap(self, wrapped_url):
1677 url = urllib.parse.unwrap(wrapped_url)
1678 self.assertEqual(url, 'scheme://host/path')
1679
1680
1681 class DeprecationTest(unittest.TestCase):
1682 def test_splittype_deprecation(self):
1683 with self.assertWarns(DeprecationWarning) as cm:
1684 urllib.parse.splittype('')
1685 self.assertEqual(str(cm.warning),
1686 'urllib.parse.splittype() is deprecated as of 3.8, '
1687 'use urllib.parse.urlparse() instead')
1688
1689 def test_splithost_deprecation(self):
1690 with self.assertWarns(DeprecationWarning) as cm:
1691 urllib.parse.splithost('')
1692 self.assertEqual(str(cm.warning),
1693 'urllib.parse.splithost() is deprecated as of 3.8, '
1694 'use urllib.parse.urlparse() instead')
1695
1696 def test_splituser_deprecation(self):
1697 with self.assertWarns(DeprecationWarning) as cm:
1698 urllib.parse.splituser('')
1699 self.assertEqual(str(cm.warning),
1700 'urllib.parse.splituser() is deprecated as of 3.8, '
1701 'use urllib.parse.urlparse() instead')
1702
1703 def test_splitpasswd_deprecation(self):
1704 with self.assertWarns(DeprecationWarning) as cm:
1705 urllib.parse.splitpasswd('')
1706 self.assertEqual(str(cm.warning),
1707 'urllib.parse.splitpasswd() is deprecated as of 3.8, '
1708 'use urllib.parse.urlparse() instead')
1709
1710 def test_splitport_deprecation(self):
1711 with self.assertWarns(DeprecationWarning) as cm:
1712 urllib.parse.splitport('')
1713 self.assertEqual(str(cm.warning),
1714 'urllib.parse.splitport() is deprecated as of 3.8, '
1715 'use urllib.parse.urlparse() instead')
1716
1717 def test_splitnport_deprecation(self):
1718 with self.assertWarns(DeprecationWarning) as cm:
1719 urllib.parse.splitnport('')
1720 self.assertEqual(str(cm.warning),
1721 'urllib.parse.splitnport() is deprecated as of 3.8, '
1722 'use urllib.parse.urlparse() instead')
1723
1724 def test_splitquery_deprecation(self):
1725 with self.assertWarns(DeprecationWarning) as cm:
1726 urllib.parse.splitquery('')
1727 self.assertEqual(str(cm.warning),
1728 'urllib.parse.splitquery() is deprecated as of 3.8, '
1729 'use urllib.parse.urlparse() instead')
1730
1731 def test_splittag_deprecation(self):
1732 with self.assertWarns(DeprecationWarning) as cm:
1733 urllib.parse.splittag('')
1734 self.assertEqual(str(cm.warning),
1735 'urllib.parse.splittag() is deprecated as of 3.8, '
1736 'use urllib.parse.urlparse() instead')
1737
1738 def test_splitattr_deprecation(self):
1739 with self.assertWarns(DeprecationWarning) as cm:
1740 urllib.parse.splitattr('')
1741 self.assertEqual(str(cm.warning),
1742 'urllib.parse.splitattr() is deprecated as of 3.8, '
1743 'use urllib.parse.urlparse() instead')
1744
1745 def test_splitvalue_deprecation(self):
1746 with self.assertWarns(DeprecationWarning) as cm:
1747 urllib.parse.splitvalue('')
1748 self.assertEqual(str(cm.warning),
1749 'urllib.parse.splitvalue() is deprecated as of 3.8, '
1750 'use urllib.parse.parse_qsl() instead')
1751
1752 def test_to_bytes_deprecation(self):
1753 with self.assertWarns(DeprecationWarning) as cm:
1754 urllib.parse.to_bytes('')
1755 self.assertEqual(str(cm.warning),
1756 'urllib.parse.to_bytes() is deprecated as of 3.8')
1757
1758
1759 def str_encode(s):
1760 return s.encode('ascii')
1761
1762 def tuple_encode(t):
1763 return tuple(str_encode(x) for x in t)
1764
1765 if __name__ == "__main__":
1766 unittest.main()