Debian Codegraph @

Lib/test/test_urllib.py

raw
1 """Regression tests for what was in Python 2's "urllib" module"""
2
3 import urllib.parse
4 import urllib.request
5 import urllib.error
6 import http.client
7 import email.message
8 import io
9 import unittest
10 from test import support
11 from test.support import os_helper
12 from test.support import socket_helper
13 from test.support import control_characters_c0
14 import os
15 import socket
16 try:
17 import ssl
18 except ImportError:
19 ssl = None
20 import sys
21 import tempfile
22
23 import collections
24
25
26 if not socket_helper.has_gethostname:
27 raise unittest.SkipTest("test requires gethostname()")
28
29
30 def hexescape(char):
31 """Escape char as RFC 2396 specifies"""
32 hex_repr = hex(ord(char))[2:].upper()
33 if len(hex_repr) == 1:
34 hex_repr = "0%s" % hex_repr
35 return "%" + hex_repr
36
37
38 def fakehttp(fakedata, mock_close=False):
39 class FakeSocket(io.BytesIO):
40 io_refs = 1
41
42 def sendall(self, data):
43 FakeHTTPConnection.buf = data
44
45 def makefile(self, *args, **kwds):
46 self.io_refs += 1
47 return self
48
49 def read(self, amt=None):
50 if self.closed:
51 return b""
52 return io.BytesIO.read(self, amt)
53
54 def readline(self, length=None):
55 if self.closed:
56 return b""
57 return io.BytesIO.readline(self, length)
58
59 def close(self):
60 self.io_refs -= 1
61 if self.io_refs == 0:
62 io.BytesIO.close(self)
63
64 class FakeHTTPConnection(http.client.HTTPConnection):
65
66 # buffer to store data for verification in urlopen tests.
67 buf = None
68
69 def connect(self):
70 self.sock = FakeSocket(self.fakedata)
71 type(self).fakesock = self.sock
72
73 if mock_close:
74 # bpo-36918: HTTPConnection destructor calls close() which calls
75 # flush(). Problem: flush() calls self.fp.flush() which raises
76 # "ValueError: I/O operation on closed file" which is logged as an
77 # "Exception ignored in". Override close() to silence this error.
78 def close(self):
79 pass
80 FakeHTTPConnection.fakedata = fakedata
81
82 return FakeHTTPConnection
83
84
85 class FakeHTTPMixin(object):
86 def fakehttp(self, fakedata, mock_close=False):
87 fake_http_class = fakehttp(fakedata, mock_close=mock_close)
88 self._connection_class = http.client.HTTPConnection
89 http.client.HTTPConnection = fake_http_class
90
91 def unfakehttp(self):
92 http.client.HTTPConnection = self._connection_class
93
94
95 class urlopen_FileTests(unittest.TestCase):
96 """Test urlopen() opening a temporary file.
97
98 Try to test as much functionality as possible so as to cut down on reliance
99 on connecting to the Net for testing.
100
101 """
102
103 def setUp(self):
104 # Create a temp file to use for testing
105 self.text = bytes("test_urllib: %s\n" % self.__class__.__name__,
106 "ascii")
107 f = open(os_helper.TESTFN, 'wb')
108 try:
109 f.write(self.text)
110 finally:
111 f.close()
112 self.pathname = os_helper.TESTFN
113 self.quoted_pathname = urllib.parse.quote(os.fsencode(self.pathname))
114 self.returned_obj = urllib.request.urlopen("file:%s" % self.quoted_pathname)
115
116 def tearDown(self):
117 """Shut down the open object"""
118 self.returned_obj.close()
119 os.remove(os_helper.TESTFN)
120
121 def test_interface(self):
122 # Make sure object returned by urlopen() has the specified methods
123 for attr in ("read", "readline", "readlines", "fileno",
124 "close", "info", "geturl", "getcode", "__iter__"):
125 self.assertHasAttr(self.returned_obj, attr)
126
127 def test_read(self):
128 self.assertEqual(self.text, self.returned_obj.read())
129
130 def test_readline(self):
131 self.assertEqual(self.text, self.returned_obj.readline())
132 self.assertEqual(b'', self.returned_obj.readline(),
133 "calling readline() after exhausting the file did not"
134 " return an empty string")
135
136 def test_readlines(self):
137 lines_list = self.returned_obj.readlines()
138 self.assertEqual(len(lines_list), 1,
139 "readlines() returned the wrong number of lines")
140 self.assertEqual(lines_list[0], self.text,
141 "readlines() returned improper text")
142
143 def test_fileno(self):
144 file_num = self.returned_obj.fileno()
145 self.assertIsInstance(file_num, int, "fileno() did not return an int")
146 self.assertEqual(os.read(file_num, len(self.text)), self.text,
147 "Reading on the file descriptor returned by fileno() "
148 "did not return the expected text")
149
150 def test_close(self):
151 # Test close() by calling it here and then having it be called again
152 # by the tearDown() method for the test
153 self.returned_obj.close()
154
155 def test_headers(self):
156 self.assertIsInstance(self.returned_obj.headers, email.message.Message)
157
158 def test_url(self):
159 self.assertEqual(self.returned_obj.url, "file:" + self.quoted_pathname)
160
161 def test_status(self):
162 self.assertIsNone(self.returned_obj.status)
163
164 def test_info(self):
165 self.assertIsInstance(self.returned_obj.info(), email.message.Message)
166
167 def test_geturl(self):
168 self.assertEqual(self.returned_obj.geturl(), "file:" + self.quoted_pathname)
169
170 def test_getcode(self):
171 self.assertIsNone(self.returned_obj.getcode())
172
173 def test_iter(self):
174 # Test iterator
175 # Don't need to count number of iterations since test would fail the
176 # instant it returned anything beyond the first line from the
177 # comparison.
178 # Use the iterator in the usual implicit way to test for ticket #4608.
179 for line in self.returned_obj:
180 self.assertEqual(line, self.text)
181
182 def test_relativelocalfile(self):
183 self.assertRaises(ValueError,urllib.request.urlopen,'./' + self.pathname)
184
185 def test_remote_authority(self):
186 # Test for GH-90812.
187 url = 'file://pythontest.net/foo/bar'
188 with self.assertRaises(urllib.error.URLError) as e:
189 urllib.request.urlopen(url)
190 if os.name == 'nt':
191 self.assertEqual(e.exception.filename, r'\\pythontest.net\foo\bar')
192 else:
193 self.assertEqual(e.exception.reason, 'file:// scheme is supported only on localhost')
194
195
196 class ProxyTests(unittest.TestCase):
197
198 def setUp(self):
199 # Records changes to env vars
200 self.env = self.enterContext(os_helper.EnvironmentVarGuard())
201 # Delete all proxy related env vars
202 for k in list(os.environ):
203 if 'proxy' in k.lower():
204 self.env.unset(k)
205
206 def test_getproxies_environment_keep_no_proxies(self):
207 self.env.set('NO_PROXY', 'localhost')
208 proxies = urllib.request.getproxies_environment()
209 # getproxies_environment use lowered case truncated (no '_proxy') keys
210 self.assertEqual('localhost', proxies['no'])
211 # List of no_proxies with space.
212 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com:1234')
213 self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com'))
214 self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com:8888'))
215 self.assertTrue(urllib.request.proxy_bypass_environment('newdomain.com:1234'))
216
217 def test_proxy_cgi_ignore(self):
218 try:
219 self.env.set('HTTP_PROXY', 'http://somewhere:3128')
220 proxies = urllib.request.getproxies_environment()
221 self.assertEqual('http://somewhere:3128', proxies['http'])
222 self.env.set('REQUEST_METHOD', 'GET')
223 proxies = urllib.request.getproxies_environment()
224 self.assertNotIn('http', proxies)
225 finally:
226 self.env.unset('REQUEST_METHOD')
227 self.env.unset('HTTP_PROXY')
228
229 def test_proxy_bypass_environment_host_match(self):
230 bypass = urllib.request.proxy_bypass_environment
231 self.env.set('NO_PROXY',
232 'localhost, anotherdomain.com, newdomain.com:1234, .d.o.t')
233 self.assertTrue(bypass('localhost'))
234 self.assertTrue(bypass('LocalHost')) # MixedCase
235 self.assertTrue(bypass('LOCALHOST')) # UPPERCASE
236 self.assertTrue(bypass('.localhost'))
237 self.assertTrue(bypass('newdomain.com:1234'))
238 self.assertTrue(bypass('.newdomain.com:1234'))
239 self.assertTrue(bypass('foo.d.o.t')) # issue 29142
240 self.assertTrue(bypass('d.o.t'))
241 self.assertTrue(bypass('anotherdomain.com:8888'))
242 self.assertTrue(bypass('.anotherdomain.com:8888'))
243 self.assertTrue(bypass('www.newdomain.com:1234'))
244 self.assertFalse(bypass('prelocalhost'))
245 self.assertFalse(bypass('newdomain.com')) # no port
246 self.assertFalse(bypass('newdomain.com:1235')) # wrong port
247
248 def test_proxy_bypass_environment_always_match(self):
249 bypass = urllib.request.proxy_bypass_environment
250 self.env.set('NO_PROXY', '*')
251 self.assertTrue(bypass('newdomain.com'))
252 self.assertTrue(bypass('newdomain.com:1234'))
253 self.env.set('NO_PROXY', '*, anotherdomain.com')
254 self.assertTrue(bypass('anotherdomain.com'))
255 self.assertFalse(bypass('newdomain.com'))
256 self.assertFalse(bypass('newdomain.com:1234'))
257
258 def test_proxy_bypass_environment_newline(self):
259 bypass = urllib.request.proxy_bypass_environment
260 self.env.set('NO_PROXY',
261 'localhost, anotherdomain.com, newdomain.com:1234')
262 self.assertFalse(bypass('localhost\n'))
263 self.assertFalse(bypass('anotherdomain.com:8888\n'))
264 self.assertFalse(bypass('newdomain.com:1234\n'))
265
266
267 class ProxyTests_withOrderedEnv(unittest.TestCase):
268
269 def setUp(self):
270 # We need to test conditions, where variable order _is_ significant
271 self._saved_env = os.environ
272 # Monkey patch os.environ, start with empty fake environment
273 os.environ = collections.OrderedDict()
274
275 def tearDown(self):
276 os.environ = self._saved_env
277
278 def test_getproxies_environment_prefer_lowercase(self):
279 # Test lowercase preference with removal
280 os.environ['no_proxy'] = ''
281 os.environ['No_Proxy'] = 'localhost'
282 self.assertFalse(urllib.request.proxy_bypass_environment('localhost'))
283 self.assertFalse(urllib.request.proxy_bypass_environment('arbitrary'))
284 os.environ['http_proxy'] = ''
285 os.environ['HTTP_PROXY'] = 'http://somewhere:3128'
286 proxies = urllib.request.getproxies_environment()
287 self.assertEqual({}, proxies)
288 # Test lowercase preference of proxy bypass and correct matching including ports
289 os.environ['no_proxy'] = 'localhost, noproxy.com, my.proxy:1234'
290 os.environ['No_Proxy'] = 'xyz.com'
291 self.assertTrue(urllib.request.proxy_bypass_environment('localhost'))
292 self.assertTrue(urllib.request.proxy_bypass_environment('noproxy.com:5678'))
293 self.assertTrue(urllib.request.proxy_bypass_environment('my.proxy:1234'))
294 self.assertFalse(urllib.request.proxy_bypass_environment('my.proxy'))
295 self.assertFalse(urllib.request.proxy_bypass_environment('arbitrary'))
296 # Test lowercase preference with replacement
297 os.environ['http_proxy'] = 'http://somewhere:3128'
298 os.environ['Http_Proxy'] = 'http://somewhereelse:3128'
299 proxies = urllib.request.getproxies_environment()
300 self.assertEqual('http://somewhere:3128', proxies['http'])
301
302
303 class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
304 """Test urlopen() opening a fake http connection."""
305
306 def check_read(self, ver):
307 self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!")
308 try:
309 fp = urllib.request.urlopen("http://python.org/")
310 self.assertEqual(fp.readline(), b"Hello!")
311 self.assertEqual(fp.readline(), b"")
312 self.assertEqual(fp.geturl(), 'http://python.org/')
313 self.assertEqual(fp.getcode(), 200)
314 finally:
315 self.unfakehttp()
316
317 def test_url_fragment(self):
318 # Issue #11703: geturl() omits fragments in the original URL.
319 url = 'http://docs.python.org/library/urllib.html#OK'
320 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
321 try:
322 fp = urllib.request.urlopen(url)
323 self.assertEqual(fp.geturl(), url)
324 finally:
325 self.unfakehttp()
326
327 def test_willclose(self):
328 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
329 try:
330 resp = urllib.request.urlopen("http://www.python.org")
331 self.assertTrue(resp.will_close)
332 finally:
333 self.unfakehttp()
334
335 @unittest.skipUnless(ssl, "ssl module required")
336 def test_url_path_with_control_char_rejected(self):
337 for char_no in list(range(0, 0x21)) + [0x7f]:
338 char = chr(char_no)
339 schemeless_url = f"//localhost:7777/test{char}/"
340 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
341 try:
342 # We explicitly test urllib.request.urlopen() instead of the top
343 # level 'def urlopen()' function defined in this... (quite ugly)
344 # test suite. They use different url opening codepaths. Plain
345 # urlopen uses FancyURLOpener which goes via a codepath that
346 # calls urllib.parse.quote() on the URL which makes all of the
347 # above attempts at injection within the url _path_ safe.
348 escaped_char_repr = repr(char).replace('\\', r'\\')
349 InvalidURL = http.client.InvalidURL
350 with self.assertRaisesRegex(
351 InvalidURL, f"contain control.*{escaped_char_repr}"):
352 urllib.request.urlopen(f"http:{schemeless_url}")
353 with self.assertRaisesRegex(
354 InvalidURL, f"contain control.*{escaped_char_repr}"):
355 urllib.request.urlopen(f"https:{schemeless_url}")
356 finally:
357 self.unfakehttp()
358
359 @unittest.skipUnless(ssl, "ssl module required")
360 def test_url_path_with_newline_header_injection_rejected(self):
361 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
362 host = "localhost:7777?a=1 HTTP/1.1\r\nX-injected: header\r\nTEST: 123"
363 schemeless_url = "//" + host + ":8080/test/?test=a"
364 try:
365 # We explicitly test urllib.request.urlopen() instead of the top
366 # level 'def urlopen()' function defined in this... (quite ugly)
367 # test suite. They use different url opening codepaths. Plain
368 # urlopen uses FancyURLOpener which goes via a codepath that
369 # calls urllib.parse.quote() on the URL which makes all of the
370 # above attempts at injection within the url _path_ safe.
371 InvalidURL = http.client.InvalidURL
372 with self.assertRaisesRegex(
373 InvalidURL, r"contain control.*\\r.*(found at least . .)"):
374 urllib.request.urlopen(f"http:{schemeless_url}")
375 with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
376 urllib.request.urlopen(f"https:{schemeless_url}")
377 finally:
378 self.unfakehttp()
379
380 @unittest.skipUnless(ssl, "ssl module required")
381 def test_url_host_with_control_char_rejected(self):
382 for char_no in list(range(0, 0x21)) + [0x7f]:
383 char = chr(char_no)
384 schemeless_url = f"//localhost{char}/test/"
385 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
386 try:
387 escaped_char_repr = repr(char).replace('\\', r'\\')
388 InvalidURL = http.client.InvalidURL
389 with self.assertRaisesRegex(
390 InvalidURL, f"contain control.*{escaped_char_repr}"):
391 urllib.request.urlopen(f"http:{schemeless_url}")
392 with self.assertRaisesRegex(InvalidURL, f"contain control.*{escaped_char_repr}"):
393 urllib.request.urlopen(f"https:{schemeless_url}")
394 finally:
395 self.unfakehttp()
396
397 @unittest.skipUnless(ssl, "ssl module required")
398 def test_url_host_with_newline_header_injection_rejected(self):
399 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
400 host = "localhost\r\nX-injected: header\r\n"
401 schemeless_url = "//" + host + ":8080/test/?test=a"
402 try:
403 InvalidURL = http.client.InvalidURL
404 with self.assertRaisesRegex(
405 InvalidURL, r"contain control.*\\r"):
406 urllib.request.urlopen(f"http:{schemeless_url}")
407 with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
408 urllib.request.urlopen(f"https:{schemeless_url}")
409 finally:
410 self.unfakehttp()
411
412 def test_read_0_9(self):
413 # "0.9" response accepted (but not "simple responses" without
414 # a status line)
415 self.check_read(b"0.9")
416
417 def test_read_1_0(self):
418 self.check_read(b"1.0")
419
420 def test_read_1_1(self):
421 self.check_read(b"1.1")
422
423 def test_read_bogus(self):
424 # urlopen() should raise OSError for many error codes.
425 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
426 Date: Wed, 02 Jan 2008 03:03:54 GMT
427 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
428 Connection: close
429 Content-Type: text/html; charset=iso-8859-1
430 ''', mock_close=True)
431 try:
432 with self.assertRaises(urllib.error.HTTPError) as cm:
433 urllib.request.urlopen("http://python.org/")
434 cm.exception.close()
435 finally:
436 self.unfakehttp()
437
438 def test_invalid_redirect(self):
439 # urlopen() should raise OSError for many error codes.
440 self.fakehttp(b'''HTTP/1.1 302 Found
441 Date: Wed, 02 Jan 2008 03:03:54 GMT
442 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
443 Location: file://guidocomputer.athome.com:/python/license
444 Connection: close
445 Content-Type: text/html; charset=iso-8859-1
446 ''', mock_close=True)
447 try:
448 msg = "Redirection to url 'file:"
449 with self.assertRaisesRegex(urllib.error.HTTPError, msg) as cm:
450 urllib.request.urlopen("http://python.org/")
451 cm.exception.close()
452 finally:
453 self.unfakehttp()
454
455 def test_redirect_limit_independent(self):
456 # Ticket #12923: make sure independent requests each use their
457 # own retry limit.
458 for i in range(urllib.request.HTTPRedirectHandler.max_redirections):
459 self.fakehttp(b'''HTTP/1.1 302 Found
460 Location: file://guidocomputer.athome.com:/python/license
461 Connection: close
462 ''', mock_close=True)
463 try:
464 with self.assertRaises(urllib.error.HTTPError) as cm:
465 urllib.request.urlopen("http://something")
466 cm.exception.close()
467 finally:
468 self.unfakehttp()
469
470 def test_empty_socket(self):
471 # urlopen() raises OSError if the underlying socket does not send any
472 # data. (#1680230)
473 self.fakehttp(b'')
474 try:
475 self.assertRaises(OSError, urllib.request.urlopen, "http://something")
476 finally:
477 self.unfakehttp()
478
479 def test_missing_localfile(self):
480 # Test for #10836
481 with self.assertRaises(urllib.error.URLError) as e:
482 urllib.request.urlopen('file://localhost/a/file/which/doesnot/exists.py')
483 self.assertTrue(e.exception.filename)
484 self.assertTrue(e.exception.reason)
485
486 def test_file_notexists(self):
487 fd, tmp_file = tempfile.mkstemp()
488 tmp_file_canon_url = urllib.request.pathname2url(tmp_file, add_scheme=True)
489 parsed = urllib.parse.urlsplit(tmp_file_canon_url)
490 tmp_fileurl = parsed._replace(netloc='localhost').geturl()
491 try:
492 self.assertTrue(os.path.exists(tmp_file))
493 with urllib.request.urlopen(tmp_fileurl) as fobj:
494 self.assertTrue(fobj)
495 self.assertEqual(fobj.url, tmp_file_canon_url)
496 finally:
497 os.close(fd)
498 os.unlink(tmp_file)
499 self.assertFalse(os.path.exists(tmp_file))
500 with self.assertRaises(urllib.error.URLError):
501 urllib.request.urlopen(tmp_fileurl)
502
503 def test_ftp_nohost(self):
504 test_ftp_url = 'ftp:///path'
505 with self.assertRaises(urllib.error.URLError) as e:
506 urllib.request.urlopen(test_ftp_url)
507 self.assertFalse(e.exception.filename)
508 self.assertTrue(e.exception.reason)
509
510 def test_ftp_nonexisting(self):
511 with self.assertRaises(urllib.error.URLError) as e:
512 urllib.request.urlopen('ftp://localhost/a/file/which/doesnot/exists.py')
513 self.assertFalse(e.exception.filename)
514 self.assertTrue(e.exception.reason)
515
516
517 class urlopen_DataTests(unittest.TestCase):
518 """Test urlopen() opening a data URL."""
519
520 def setUp(self):
521 # clear _opener global variable
522 self.addCleanup(urllib.request.urlcleanup)
523
524 # text containing URL special- and unicode-characters
525 self.text = "test data URLs :;,%=& \u00f6 \u00c4 "
526 # 2x1 pixel RGB PNG image with one black and one white pixel
527 self.image = (
528 b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00'
529 b'\x01\x08\x02\x00\x00\x00{@\xe8\xdd\x00\x00\x00\x01sRGB\x00\xae'
530 b'\xce\x1c\xe9\x00\x00\x00\x0fIDAT\x08\xd7c```\xf8\xff\xff?\x00'
531 b'\x06\x01\x02\xfe\no/\x1e\x00\x00\x00\x00IEND\xaeB`\x82')
532
533 self.text_url = (
534 "data:text/plain;charset=UTF-8,test%20data%20URLs%20%3A%3B%2C%25%3"
535 "D%26%20%C3%B6%20%C3%84%20")
536 self.text_url_base64 = (
537 "data:text/plain;charset=ISO-8859-1;base64,dGVzdCBkYXRhIFVSTHMgOjs"
538 "sJT0mIPYgxCA%3D")
539 # base64 encoded data URL that contains ignorable spaces,
540 # such as "\n", " ", "%0A", and "%20".
541 self.image_url = (
542 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7\n"
543 "QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 "
544 "vHgAAAABJRU5ErkJggg%3D%3D%0A%20")
545
546 self.text_url_resp = self.enterContext(
547 urllib.request.urlopen(self.text_url))
548 self.text_url_base64_resp = self.enterContext(
549 urllib.request.urlopen(self.text_url_base64))
550 self.image_url_resp = self.enterContext(urllib.request.urlopen(self.image_url))
551
552 def test_interface(self):
553 # Make sure object returned by urlopen() has the specified methods
554 for attr in ("read", "readline", "readlines",
555 "close", "info", "geturl", "getcode", "__iter__"):
556 self.assertHasAttr(self.text_url_resp, attr)
557
558 def test_info(self):
559 self.assertIsInstance(self.text_url_resp.info(), email.message.Message)
560 self.assertEqual(self.text_url_base64_resp.info().get_params(),
561 [('text/plain', ''), ('charset', 'ISO-8859-1')])
562 self.assertEqual(self.image_url_resp.info()['content-length'],
563 str(len(self.image)))
564 r = urllib.request.urlopen("data:,")
565 self.assertEqual(r.info().get_params(),
566 [('text/plain', ''), ('charset', 'US-ASCII')])
567 r.close()
568
569 def test_geturl(self):
570 self.assertEqual(self.text_url_resp.geturl(), self.text_url)
571 self.assertEqual(self.text_url_base64_resp.geturl(),
572 self.text_url_base64)
573 self.assertEqual(self.image_url_resp.geturl(), self.image_url)
574
575 def test_read_text(self):
576 self.assertEqual(self.text_url_resp.read().decode(
577 dict(self.text_url_resp.info().get_params())['charset']), self.text)
578
579 def test_read_text_base64(self):
580 self.assertEqual(self.text_url_base64_resp.read().decode(
581 dict(self.text_url_base64_resp.info().get_params())['charset']),
582 self.text)
583
584 def test_read_image(self):
585 self.assertEqual(self.image_url_resp.read(), self.image)
586
587 def test_missing_comma(self):
588 self.assertRaises(ValueError,urllib.request.urlopen,'data:text/plain')
589
590 def test_invalid_base64_data(self):
591 # missing padding character
592 self.assertRaises(ValueError,urllib.request.urlopen,'data:;base64,Cg=')
593
594 def test_invalid_mediatype(self):
595 for c0 in control_characters_c0():
596 self.assertRaises(ValueError,urllib.request.urlopen,
597 f'data:text/html;{c0},data')
598 for c0 in control_characters_c0():
599 self.assertRaises(ValueError,urllib.request.urlopen,
600 f'data:text/html{c0};base64,ZGF0YQ==')
601
602 class urlretrieve_FileTests(unittest.TestCase):
603 """Test urllib.urlretrieve() on local files"""
604
605 def setUp(self):
606 # clear _opener global variable
607 self.addCleanup(urllib.request.urlcleanup)
608
609 # Create a list of temporary files. Each item in the list is a file
610 # name (absolute path or relative to the current working directory).
611 # All files in this list will be deleted in the tearDown method. Note,
612 # this only helps to makes sure temporary files get deleted, but it
613 # does nothing about trying to close files that may still be open. It
614 # is the responsibility of the developer to properly close files even
615 # when exceptional conditions occur.
616 self.tempFiles = []
617
618 # Create a temporary file.
619 self.registerFileForCleanUp(os_helper.TESTFN)
620 self.text = b'testing urllib.urlretrieve'
621 try:
622 FILE = open(os_helper.TESTFN, 'wb')
623 FILE.write(self.text)
624 FILE.close()
625 finally:
626 try: FILE.close()
627 except: pass
628
629 def tearDown(self):
630 # Delete the temporary files.
631 for each in self.tempFiles:
632 try: os.remove(each)
633 except: pass
634
635 def constructLocalFileUrl(self, filePath):
636 filePath = os.path.abspath(filePath)
637 return urllib.request.pathname2url(filePath, add_scheme=True)
638
639 def createNewTempFile(self, data=b""):
640 """Creates a new temporary file containing the specified data,
641 registers the file for deletion during the test fixture tear down, and
642 returns the absolute path of the file."""
643
644 newFd, newFilePath = tempfile.mkstemp()
645 try:
646 self.registerFileForCleanUp(newFilePath)
647 newFile = os.fdopen(newFd, "wb")
648 newFile.write(data)
649 newFile.close()
650 finally:
651 try: newFile.close()
652 except: pass
653 return newFilePath
654
655 def registerFileForCleanUp(self, fileName):
656 self.tempFiles.append(fileName)
657
658 def test_basic(self):
659 # Make sure that a local file just gets its own location returned and
660 # a headers value is returned.
661 result = urllib.request.urlretrieve("file:%s" % os_helper.TESTFN)
662 self.assertEqual(result[0], os_helper.TESTFN)
663 self.assertIsInstance(result[1], email.message.Message,
664 "did not get an email.message.Message instance "
665 "as second returned value")
666
667 def test_copy(self):
668 # Test that setting the filename argument works.
669 second_temp = "%s.2" % os_helper.TESTFN
670 self.registerFileForCleanUp(second_temp)
671 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
672 os_helper.TESTFN), second_temp)
673 self.assertEqual(second_temp, result[0])
674 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
675 "made")
676 FILE = open(second_temp, 'rb')
677 try:
678 text = FILE.read()
679 FILE.close()
680 finally:
681 try: FILE.close()
682 except: pass
683 self.assertEqual(self.text, text)
684
685 def test_reporthook(self):
686 # Make sure that the reporthook works.
687 def hooktester(block_count, block_read_size, file_size, count_holder=[0]):
688 self.assertIsInstance(block_count, int)
689 self.assertIsInstance(block_read_size, int)
690 self.assertIsInstance(file_size, int)
691 self.assertEqual(block_count, count_holder[0])
692 count_holder[0] = count_holder[0] + 1
693 second_temp = "%s.2" % os_helper.TESTFN
694 self.registerFileForCleanUp(second_temp)
695 urllib.request.urlretrieve(
696 self.constructLocalFileUrl(os_helper.TESTFN),
697 second_temp, hooktester)
698
699 def test_reporthook_0_bytes(self):
700 # Test on zero length file. Should call reporthook only 1 time.
701 report = []
702 def hooktester(block_count, block_read_size, file_size, _report=report):
703 _report.append((block_count, block_read_size, file_size))
704 srcFileName = self.createNewTempFile()
705 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
706 os_helper.TESTFN, hooktester)
707 self.assertEqual(len(report), 1)
708 self.assertEqual(report[0][2], 0)
709
710 def test_reporthook_5_bytes(self):
711 # Test on 5 byte file. Should call reporthook only 2 times (once when
712 # the "network connection" is established and once when the block is
713 # read).
714 report = []
715 def hooktester(block_count, block_read_size, file_size, _report=report):
716 _report.append((block_count, block_read_size, file_size))
717 srcFileName = self.createNewTempFile(b"x" * 5)
718 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
719 os_helper.TESTFN, hooktester)
720 self.assertEqual(len(report), 2)
721 self.assertEqual(report[0][2], 5)
722 self.assertEqual(report[1][2], 5)
723
724 def test_reporthook_8193_bytes(self):
725 # Test on 8193 byte file. Should call reporthook only 3 times (once
726 # when the "network connection" is established, once for the next 8192
727 # bytes, and once for the last byte).
728 report = []
729 def hooktester(block_count, block_read_size, file_size, _report=report):
730 _report.append((block_count, block_read_size, file_size))
731 srcFileName = self.createNewTempFile(b"x" * 8193)
732 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
733 os_helper.TESTFN, hooktester)
734 self.assertEqual(len(report), 3)
735 self.assertEqual(report[0][2], 8193)
736 self.assertEqual(report[0][1], 8192)
737 self.assertEqual(report[1][1], 8192)
738 self.assertEqual(report[2][1], 8192)
739
740
741 class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
742 """Test urllib.urlretrieve() using fake http connections"""
743
744 def test_short_content_raises_ContentTooShortError(self):
745 self.addCleanup(urllib.request.urlcleanup)
746
747 self.fakehttp(b'''HTTP/1.1 200 OK
748 Date: Wed, 02 Jan 2008 03:03:54 GMT
749 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
750 Connection: close
751 Content-Length: 100
752 Content-Type: text/html; charset=iso-8859-1
753
754 FF
755 ''')
756
757 def _reporthook(par1, par2, par3):
758 pass
759
760 with self.assertRaises(urllib.error.ContentTooShortError):
761 try:
762 urllib.request.urlretrieve(support.TEST_HTTP_URL,
763 reporthook=_reporthook)
764 finally:
765 self.unfakehttp()
766
767 def test_short_content_raises_ContentTooShortError_without_reporthook(self):
768 self.addCleanup(urllib.request.urlcleanup)
769
770 self.fakehttp(b'''HTTP/1.1 200 OK
771 Date: Wed, 02 Jan 2008 03:03:54 GMT
772 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
773 Connection: close
774 Content-Length: 100
775 Content-Type: text/html; charset=iso-8859-1
776
777 FF
778 ''')
779 with self.assertRaises(urllib.error.ContentTooShortError):
780 try:
781 urllib.request.urlretrieve(support.TEST_HTTP_URL)
782 finally:
783 self.unfakehttp()
784
785
786 class QuotingTests(unittest.TestCase):
787 r"""Tests for urllib.quote() and urllib.quote_plus()
788
789 According to RFC 3986 (Uniform Resource Identifiers), to escape a
790 character you write it as '%' + <2 character US-ASCII hex value>.
791 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
792 character properly. Case does not matter on the hex letters.
793
794 The various character sets specified are:
795
796 Reserved characters : ";/?:@&=+$,"
797 Have special meaning in URIs and must be escaped if not being used for
798 their special meaning
799 Data characters : letters, digits, and "-_.!~*'()"
800 Unreserved and do not need to be escaped; can be, though, if desired
801 Control characters : 0x00 - 0x1F, 0x7F
802 Have no use in URIs so must be escaped
803 space : 0x20
804 Must be escaped
805 Delimiters : '<>#%"'
806 Must be escaped
807 Unwise : "{}|\^[]`"
808 Must be escaped
809
810 """
811
812 def test_never_quote(self):
813 # Make sure quote() does not quote letters, digits, and "_,.-"
814 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
815 "abcdefghijklmnopqrstuvwxyz",
816 "0123456789",
817 "_.-~"])
818 result = urllib.parse.quote(do_not_quote)
819 self.assertEqual(do_not_quote, result,
820 "using quote(): %r != %r" % (do_not_quote, result))
821 result = urllib.parse.quote_plus(do_not_quote)
822 self.assertEqual(do_not_quote, result,
823 "using quote_plus(): %r != %r" % (do_not_quote, result))
824
825 def test_default_safe(self):
826 # Test '/' is default value for 'safe' parameter
827 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
828
829 def test_safe(self):
830 # Test setting 'safe' parameter does what it should do
831 quote_by_default = "<>"
832 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
833 self.assertEqual(quote_by_default, result,
834 "using quote(): %r != %r" % (quote_by_default, result))
835 result = urllib.parse.quote_plus(quote_by_default,
836 safe=quote_by_default)
837 self.assertEqual(quote_by_default, result,
838 "using quote_plus(): %r != %r" %
839 (quote_by_default, result))
840 # Safe expressed as bytes rather than str
841 result = urllib.parse.quote(quote_by_default, safe=b"<>")
842 self.assertEqual(quote_by_default, result,
843 "using quote(): %r != %r" % (quote_by_default, result))
844 # "Safe" non-ASCII characters should have no effect
845 # (Since URIs are not allowed to have non-ASCII characters)
846 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
847 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
848 self.assertEqual(expect, result,
849 "using quote(): %r != %r" %
850 (expect, result))
851 # Same as above, but using a bytes rather than str
852 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
853 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
854 self.assertEqual(expect, result,
855 "using quote(): %r != %r" %
856 (expect, result))
857
858 def test_default_quoting(self):
859 # Make sure all characters that should be quoted are by default sans
860 # space (separate test for that).
861 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
862 should_quote.append(r'<>#%"{}|\^[]`')
863 should_quote.append(chr(127)) # For 0x7F
864 should_quote = ''.join(should_quote)
865 for char in should_quote:
866 result = urllib.parse.quote(char)
867 self.assertEqual(hexescape(char), result,
868 "using quote(): "
869 "%s should be escaped to %s, not %s" %
870 (char, hexescape(char), result))
871 result = urllib.parse.quote_plus(char)
872 self.assertEqual(hexescape(char), result,
873 "using quote_plus(): "
874 "%s should be escapes to %s, not %s" %
875 (char, hexescape(char), result))
876 del should_quote
877 partial_quote = "ab[]cd"
878 expected = "ab%5B%5Dcd"
879 result = urllib.parse.quote(partial_quote)
880 self.assertEqual(expected, result,
881 "using quote(): %r != %r" % (expected, result))
882 result = urllib.parse.quote_plus(partial_quote)
883 self.assertEqual(expected, result,
884 "using quote_plus(): %r != %r" % (expected, result))
885
886 def test_quoting_space(self):
887 # Make sure quote() and quote_plus() handle spaces as specified in
888 # their unique way
889 result = urllib.parse.quote(' ')
890 self.assertEqual(result, hexescape(' '),
891 "using quote(): %r != %r" % (result, hexescape(' ')))
892 result = urllib.parse.quote_plus(' ')
893 self.assertEqual(result, '+',
894 "using quote_plus(): %r != +" % result)
895 given = "a b cd e f"
896 expect = given.replace(' ', hexescape(' '))
897 result = urllib.parse.quote(given)
898 self.assertEqual(expect, result,
899 "using quote(): %r != %r" % (expect, result))
900 expect = given.replace(' ', '+')
901 result = urllib.parse.quote_plus(given)
902 self.assertEqual(expect, result,
903 "using quote_plus(): %r != %r" % (expect, result))
904
905 def test_quoting_plus(self):
906 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
907 'alpha%2Bbeta+gamma')
908 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
909 'alpha+beta+gamma')
910 # Test with bytes
911 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
912 'alpha%2Bbeta+gamma')
913 # Test with safe bytes
914 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
915 'alpha+beta+gamma')
916
917 def test_quote_bytes(self):
918 # Bytes should quote directly to percent-encoded values
919 given = b"\xa2\xd8ab\xff"
920 expect = "%A2%D8ab%FF"
921 result = urllib.parse.quote(given)
922 self.assertEqual(expect, result,
923 "using quote(): %r != %r" % (expect, result))
924 # Encoding argument should raise type error on bytes input
925 self.assertRaises(TypeError, urllib.parse.quote, given,
926 encoding="latin-1")
927 # quote_from_bytes should work the same
928 result = urllib.parse.quote_from_bytes(given)
929 self.assertEqual(expect, result,
930 "using quote_from_bytes(): %r != %r"
931 % (expect, result))
932
933 def test_quote_with_unicode(self):
934 # Characters in Latin-1 range, encoded by default in UTF-8
935 given = "\xa2\xd8ab\xff"
936 expect = "%C2%A2%C3%98ab%C3%BF"
937 result = urllib.parse.quote(given)
938 self.assertEqual(expect, result,
939 "using quote(): %r != %r" % (expect, result))
940 # Characters in Latin-1 range, encoded by with None (default)
941 result = urllib.parse.quote(given, encoding=None, errors=None)
942 self.assertEqual(expect, result,
943 "using quote(): %r != %r" % (expect, result))
944 # Characters in Latin-1 range, encoded with Latin-1
945 given = "\xa2\xd8ab\xff"
946 expect = "%A2%D8ab%FF"
947 result = urllib.parse.quote(given, encoding="latin-1")
948 self.assertEqual(expect, result,
949 "using quote(): %r != %r" % (expect, result))
950 # Characters in BMP, encoded by default in UTF-8
951 given = "\u6f22\u5b57" # "Kanji"
952 expect = "%E6%BC%A2%E5%AD%97"
953 result = urllib.parse.quote(given)
954 self.assertEqual(expect, result,
955 "using quote(): %r != %r" % (expect, result))
956 # Characters in BMP, encoded with Latin-1
957 given = "\u6f22\u5b57"
958 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
959 encoding="latin-1")
960 # Characters in BMP, encoded with Latin-1, with replace error handling
961 given = "\u6f22\u5b57"
962 expect = "%3F%3F" # "??"
963 result = urllib.parse.quote(given, encoding="latin-1",
964 errors="replace")
965 self.assertEqual(expect, result,
966 "using quote(): %r != %r" % (expect, result))
967 # Characters in BMP, Latin-1, with xmlcharref error handling
968 given = "\u6f22\u5b57"
969 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
970 result = urllib.parse.quote(given, encoding="latin-1",
971 errors="xmlcharrefreplace")
972 self.assertEqual(expect, result,
973 "using quote(): %r != %r" % (expect, result))
974
975 def test_quote_plus_with_unicode(self):
976 # Encoding (latin-1) test for quote_plus
977 given = "\xa2\xd8 \xff"
978 expect = "%A2%D8+%FF"
979 result = urllib.parse.quote_plus(given, encoding="latin-1")
980 self.assertEqual(expect, result,
981 "using quote_plus(): %r != %r" % (expect, result))
982 # Errors test for quote_plus
983 given = "ab\u6f22\u5b57 cd"
984 expect = "ab%3F%3F+cd"
985 result = urllib.parse.quote_plus(given, encoding="latin-1",
986 errors="replace")
987 self.assertEqual(expect, result,
988 "using quote_plus(): %r != %r" % (expect, result))
989
990
991 class UnquotingTests(unittest.TestCase):
992 """Tests for unquote() and unquote_plus()
993
994 See the doc string for quoting_Tests for details on quoting and such.
995
996 """
997
998 def test_unquoting(self):
999 # Make sure unquoting of all ASCII values works
1000 escape_list = []
1001 for num in range(128):
1002 given = hexescape(chr(num))
1003 expect = chr(num)
1004 result = urllib.parse.unquote(given)
1005 self.assertEqual(expect, result,
1006 "using unquote(): %r != %r" % (expect, result))
1007 result = urllib.parse.unquote_plus(given)
1008 self.assertEqual(expect, result,
1009 "using unquote_plus(): %r != %r" %
1010 (expect, result))
1011 escape_list.append(given)
1012 escape_string = ''.join(escape_list)
1013 del escape_list
1014 result = urllib.parse.unquote(escape_string)
1015 self.assertEqual(result.count('%'), 1,
1016 "using unquote(): not all characters escaped: "
1017 "%s" % result)
1018
1019 def test_unquote_rejects_none_and_tuple(self):
1020 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
1021 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
1022
1023 def test_unquoting_badpercent(self):
1024 # Test unquoting on bad percent-escapes
1025 given = '%xab'
1026 expect = given
1027 result = urllib.parse.unquote(given)
1028 self.assertEqual(expect, result, "using unquote(): %r != %r"
1029 % (expect, result))
1030 given = '%x'
1031 expect = given
1032 result = urllib.parse.unquote(given)
1033 self.assertEqual(expect, result, "using unquote(): %r != %r"
1034 % (expect, result))
1035 given = '%'
1036 expect = given
1037 result = urllib.parse.unquote(given)
1038 self.assertEqual(expect, result, "using unquote(): %r != %r"
1039 % (expect, result))
1040 # unquote_to_bytes
1041 given = '%xab'
1042 expect = bytes(given, 'ascii')
1043 result = urllib.parse.unquote_to_bytes(given)
1044 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
1045 % (expect, result))
1046 given = '%x'
1047 expect = bytes(given, 'ascii')
1048 result = urllib.parse.unquote_to_bytes(given)
1049 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
1050 % (expect, result))
1051 given = '%'
1052 expect = bytes(given, 'ascii')
1053 result = urllib.parse.unquote_to_bytes(given)
1054 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
1055 % (expect, result))
1056 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)
1057 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())
1058
1059 def test_unquoting_mixed_case(self):
1060 # Test unquoting on mixed-case hex digits in the percent-escapes
1061 given = '%Ab%eA'
1062 expect = b'\xab\xea'
1063 result = urllib.parse.unquote_to_bytes(given)
1064 self.assertEqual(expect, result,
1065 "using unquote_to_bytes(): %r != %r"
1066 % (expect, result))
1067
1068 def test_unquoting_parts(self):
1069 # Make sure unquoting works when have non-quoted characters
1070 # interspersed
1071 given = 'ab%sd' % hexescape('c')
1072 expect = "abcd"
1073 result = urllib.parse.unquote(given)
1074 self.assertEqual(expect, result,
1075 "using quote(): %r != %r" % (expect, result))
1076 result = urllib.parse.unquote_plus(given)
1077 self.assertEqual(expect, result,
1078 "using unquote_plus(): %r != %r" % (expect, result))
1079
1080 def test_unquoting_plus(self):
1081 # Test difference between unquote() and unquote_plus()
1082 given = "are+there+spaces..."
1083 expect = given
1084 result = urllib.parse.unquote(given)
1085 self.assertEqual(expect, result,
1086 "using unquote(): %r != %r" % (expect, result))
1087 expect = given.replace('+', ' ')
1088 result = urllib.parse.unquote_plus(given)
1089 self.assertEqual(expect, result,
1090 "using unquote_plus(): %r != %r" % (expect, result))
1091
1092 def test_unquote_to_bytes(self):
1093 given = 'br%C3%BCckner_sapporo_20050930.doc'
1094 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
1095 result = urllib.parse.unquote_to_bytes(given)
1096 self.assertEqual(expect, result,
1097 "using unquote_to_bytes(): %r != %r"
1098 % (expect, result))
1099 # Test on a string with unescaped non-ASCII characters
1100 # (Technically an invalid URI; expect those characters to be UTF-8
1101 # encoded).
1102 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
1103 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
1104 self.assertEqual(expect, result,
1105 "using unquote_to_bytes(): %r != %r"
1106 % (expect, result))
1107 # Test with a bytes as input
1108 given = b'%A2%D8ab%FF'
1109 expect = b'\xa2\xd8ab\xff'
1110 result = urllib.parse.unquote_to_bytes(given)
1111 self.assertEqual(expect, result,
1112 "using unquote_to_bytes(): %r != %r"
1113 % (expect, result))
1114 # Test with a bytes as input, with unescaped non-ASCII bytes
1115 # (Technically an invalid URI; expect those bytes to be preserved)
1116 given = b'%A2\xd8ab%FF'
1117 expect = b'\xa2\xd8ab\xff'
1118 result = urllib.parse.unquote_to_bytes(given)
1119 self.assertEqual(expect, result,
1120 "using unquote_to_bytes(): %r != %r"
1121 % (expect, result))
1122
1123 def test_unquote_with_unicode(self):
1124 # Characters in the Latin-1 range, encoded with UTF-8
1125 given = 'br%C3%BCckner_sapporo_20050930.doc'
1126 expect = 'br\u00fcckner_sapporo_20050930.doc'
1127 result = urllib.parse.unquote(given)
1128 self.assertEqual(expect, result,
1129 "using unquote(): %r != %r" % (expect, result))
1130 # Characters in the Latin-1 range, encoded with None (default)
1131 result = urllib.parse.unquote(given, encoding=None, errors=None)
1132 self.assertEqual(expect, result,
1133 "using unquote(): %r != %r" % (expect, result))
1134
1135 # Characters in the Latin-1 range, encoded with Latin-1
1136 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
1137 encoding="latin-1")
1138 expect = 'br\u00fcckner_sapporo_20050930.doc'
1139 self.assertEqual(expect, result,
1140 "using unquote(): %r != %r" % (expect, result))
1141
1142 # Characters in BMP, encoded with UTF-8
1143 given = "%E6%BC%A2%E5%AD%97"
1144 expect = "\u6f22\u5b57" # "Kanji"
1145 result = urllib.parse.unquote(given)
1146 self.assertEqual(expect, result,
1147 "using unquote(): %r != %r" % (expect, result))
1148
1149 # Decode with UTF-8, invalid sequence
1150 given = "%F3%B1"
1151 expect = "\ufffd" # Replacement character
1152 result = urllib.parse.unquote(given)
1153 self.assertEqual(expect, result,
1154 "using unquote(): %r != %r" % (expect, result))
1155
1156 # Decode with UTF-8, invalid sequence, replace errors
1157 result = urllib.parse.unquote(given, errors="replace")
1158 self.assertEqual(expect, result,
1159 "using unquote(): %r != %r" % (expect, result))
1160
1161 # Decode with UTF-8, invalid sequence, ignoring errors
1162 given = "%F3%B1"
1163 expect = ""
1164 result = urllib.parse.unquote(given, errors="ignore")
1165 self.assertEqual(expect, result,
1166 "using unquote(): %r != %r" % (expect, result))
1167
1168 # A mix of non-ASCII and percent-encoded characters, UTF-8
1169 result = urllib.parse.unquote("\u6f22%C3%BC")
1170 expect = '\u6f22\u00fc'
1171 self.assertEqual(expect, result,
1172 "using unquote(): %r != %r" % (expect, result))
1173
1174 # A mix of non-ASCII and percent-encoded characters, Latin-1
1175 # (Note, the string contains non-Latin-1-representable characters)
1176 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
1177 expect = '\u6f22\u00fc'
1178 self.assertEqual(expect, result,
1179 "using unquote(): %r != %r" % (expect, result))
1180
1181 def test_unquoting_with_bytes_input(self):
1182 # ASCII characters decoded to a string
1183 given = b'blueberryjam'
1184 expect = 'blueberryjam'
1185 result = urllib.parse.unquote(given)
1186 self.assertEqual(expect, result,
1187 "using unquote(): %r != %r" % (expect, result))
1188
1189 # A mix of non-ASCII hex-encoded characters and ASCII characters
1190 given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
1191 expect = 'bl\u00e5b\u00e6rsyltet\u00f8y'
1192 result = urllib.parse.unquote(given)
1193 self.assertEqual(expect, result,
1194 "using unquote(): %r != %r" % (expect, result))
1195
1196 # A mix of non-ASCII percent-encoded characters and ASCII characters
1197 given = b'bl%c3%a5b%c3%a6rsyltet%c3%b8j'
1198 expect = 'bl\u00e5b\u00e6rsyltet\u00f8j'
1199 result = urllib.parse.unquote(given)
1200 self.assertEqual(expect, result,
1201 "using unquote(): %r != %r" % (expect, result))
1202
1203
1204 class urlencode_Tests(unittest.TestCase):
1205 """Tests for urlencode()"""
1206
1207 def help_inputtype(self, given, test_type):
1208 """Helper method for testing different input types.
1209
1210 'given' must lead to only the pairs:
1211 * 1st, 1
1212 * 2nd, 2
1213 * 3rd, 3
1214
1215 Test cannot assume anything about order. Docs make no guarantee and
1216 have possible dictionary input.
1217
1218 """
1219 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
1220 result = urllib.parse.urlencode(given)
1221 for expected in expect_somewhere:
1222 self.assertIn(expected, result,
1223 "testing %s: %s not found in %s" %
1224 (test_type, expected, result))
1225 self.assertEqual(result.count('&'), 2,
1226 "testing %s: expected 2 '&'s; got %s" %
1227 (test_type, result.count('&')))
1228 amp_location = result.index('&')
1229 on_amp_left = result[amp_location - 1]
1230 on_amp_right = result[amp_location + 1]
1231 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
1232 "testing %s: '&' not located in proper place in %s" %
1233 (test_type, result))
1234 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
1235 "testing %s: "
1236 "unexpected number of characters: %s != %s" %
1237 (test_type, len(result), (5 * 3) + 2))
1238
1239 def test_using_mapping(self):
1240 # Test passing in a mapping object as an argument.
1241 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
1242 "using dict as input type")
1243
1244 def test_using_sequence(self):
1245 # Test passing in a sequence of two-item sequences as an argument.
1246 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
1247 "using sequence of two-item tuples as input")
1248
1249 def test_quoting(self):
1250 # Make sure keys and values are quoted using quote_plus()
1251 given = {"&":"="}
1252 expect = "%s=%s" % (hexescape('&'), hexescape('='))
1253 result = urllib.parse.urlencode(given)
1254 self.assertEqual(expect, result)
1255 given = {"key name":"A bunch of pluses"}
1256 expect = "key+name=A+bunch+of+pluses"
1257 result = urllib.parse.urlencode(given)
1258 self.assertEqual(expect, result)
1259
1260 def test_doseq(self):
1261 # Test that passing True for 'doseq' parameter works correctly
1262 given = {'sequence':['1', '2', '3']}
1263 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
1264 result = urllib.parse.urlencode(given)
1265 self.assertEqual(expect, result)
1266 result = urllib.parse.urlencode(given, True)
1267 for value in given["sequence"]:
1268 expect = "sequence=%s" % value
1269 self.assertIn(expect, result)
1270 self.assertEqual(result.count('&'), 2,
1271 "Expected 2 '&'s, got %s" % result.count('&'))
1272
1273 def test_empty_sequence(self):
1274 self.assertEqual("", urllib.parse.urlencode({}))
1275 self.assertEqual("", urllib.parse.urlencode([]))
1276
1277 def test_nonstring_values(self):
1278 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
1279 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
1280
1281 def test_nonstring_seq_values(self):
1282 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
1283 self.assertEqual("a=None&a=a",
1284 urllib.parse.urlencode({"a": [None, "a"]}, True))
1285 data = collections.OrderedDict([("a", 1), ("b", 1)])
1286 self.assertEqual("a=a&a=b",
1287 urllib.parse.urlencode({"a": data}, True))
1288
1289 def test_urlencode_encoding(self):
1290 # ASCII encoding. Expect %3F with errors="replace'
1291 given = (('\u00a0', '\u00c1'),)
1292 expect = '%3F=%3F'
1293 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
1294 self.assertEqual(expect, result)
1295
1296 # Default is UTF-8 encoding.
1297 given = (('\u00a0', '\u00c1'),)
1298 expect = '%C2%A0=%C3%81'
1299 result = urllib.parse.urlencode(given)
1300 self.assertEqual(expect, result)
1301
1302 # Latin-1 encoding.
1303 given = (('\u00a0', '\u00c1'),)
1304 expect = '%A0=%C1'
1305 result = urllib.parse.urlencode(given, encoding="latin-1")
1306 self.assertEqual(expect, result)
1307
1308 def test_urlencode_encoding_doseq(self):
1309 # ASCII Encoding. Expect %3F with errors="replace'
1310 given = (('\u00a0', '\u00c1'),)
1311 expect = '%3F=%3F'
1312 result = urllib.parse.urlencode(given, doseq=True,
1313 encoding="ASCII", errors="replace")
1314 self.assertEqual(expect, result)
1315
1316 # ASCII Encoding. On a sequence of values.
1317 given = (("\u00a0", (1, "\u00c1")),)
1318 expect = '%3F=1&%3F=%3F'
1319 result = urllib.parse.urlencode(given, True,
1320 encoding="ASCII", errors="replace")
1321 self.assertEqual(expect, result)
1322
1323 # Utf-8
1324 given = (("\u00a0", "\u00c1"),)
1325 expect = '%C2%A0=%C3%81'
1326 result = urllib.parse.urlencode(given, True)
1327 self.assertEqual(expect, result)
1328
1329 given = (("\u00a0", (42, "\u00c1")),)
1330 expect = '%C2%A0=42&%C2%A0=%C3%81'
1331 result = urllib.parse.urlencode(given, True)
1332 self.assertEqual(expect, result)
1333
1334 # latin-1
1335 given = (("\u00a0", "\u00c1"),)
1336 expect = '%A0=%C1'
1337 result = urllib.parse.urlencode(given, True, encoding="latin-1")
1338 self.assertEqual(expect, result)
1339
1340 given = (("\u00a0", (42, "\u00c1")),)
1341 expect = '%A0=42&%A0=%C1'
1342 result = urllib.parse.urlencode(given, True, encoding="latin-1")
1343 self.assertEqual(expect, result)
1344
1345 def test_urlencode_bytes(self):
1346 given = ((b'\xa0\x24', b'\xc1\x24'),)
1347 expect = '%A0%24=%C1%24'
1348 result = urllib.parse.urlencode(given)
1349 self.assertEqual(expect, result)
1350 result = urllib.parse.urlencode(given, True)
1351 self.assertEqual(expect, result)
1352
1353 # Sequence of values
1354 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
1355 expect = '%A0%24=42&%A0%24=%C1%24'
1356 result = urllib.parse.urlencode(given, True)
1357 self.assertEqual(expect, result)
1358
1359 def test_urlencode_encoding_safe_parameter(self):
1360
1361 # Send '$' (\x24) as safe character
1362 # Default utf-8 encoding
1363
1364 given = ((b'\xa0\x24', b'\xc1\x24'),)
1365 result = urllib.parse.urlencode(given, safe=":$")
1366 expect = '%A0$=%C1$'
1367 self.assertEqual(expect, result)
1368
1369 given = ((b'\xa0\x24', b'\xc1\x24'),)
1370 result = urllib.parse.urlencode(given, doseq=True, safe=":$")
1371 expect = '%A0$=%C1$'
1372 self.assertEqual(expect, result)
1373
1374 # Safe parameter in sequence
1375 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
1376 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
1377 result = urllib.parse.urlencode(given, True, safe=":$")
1378 self.assertEqual(expect, result)
1379
1380 # Test all above in latin-1 encoding
1381
1382 given = ((b'\xa0\x24', b'\xc1\x24'),)
1383 result = urllib.parse.urlencode(given, safe=":$",
1384 encoding="latin-1")
1385 expect = '%A0$=%C1$'
1386 self.assertEqual(expect, result)
1387
1388 given = ((b'\xa0\x24', b'\xc1\x24'),)
1389 expect = '%A0$=%C1$'
1390 result = urllib.parse.urlencode(given, doseq=True, safe=":$",
1391 encoding="latin-1")
1392
1393 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
1394 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
1395 result = urllib.parse.urlencode(given, True, safe=":$",
1396 encoding="latin-1")
1397 self.assertEqual(expect, result)
1398
1399 class Pathname_Tests(unittest.TestCase):
1400 """Test pathname2url() and url2pathname()"""
1401
1402 def test_basic(self):
1403 # Make sure simple tests pass
1404 expected_path = os.path.join("parts", "of", "a", "path")
1405 expected_url = "parts/of/a/path"
1406 result = urllib.request.pathname2url(expected_path)
1407 self.assertEqual(expected_url, result,
1408 "pathname2url() failed; %s != %s" %
1409 (result, expected_url))
1410 result = urllib.request.url2pathname(expected_url)
1411 self.assertEqual(expected_path, result,
1412 "url2pathame() failed; %s != %s" %
1413 (result, expected_path))
1414
1415 def test_quoting(self):
1416 # Test automatic quoting and unquoting works for pathnam2url() and
1417 # url2pathname() respectively
1418 given = os.path.join("needs", "quot=ing", "here")
1419 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
1420 result = urllib.request.pathname2url(given)
1421 self.assertEqual(expect, result,
1422 "pathname2url() failed; %s != %s" %
1423 (expect, result))
1424 expect = given
1425 result = urllib.request.url2pathname(result)
1426 self.assertEqual(expect, result,
1427 "url2pathname() failed; %s != %s" %
1428 (expect, result))
1429 given = os.path.join("make sure", "using_quote")
1430 expect = "%s/using_quote" % urllib.parse.quote("make sure")
1431 result = urllib.request.pathname2url(given)
1432 self.assertEqual(expect, result,
1433 "pathname2url() failed; %s != %s" %
1434 (expect, result))
1435 given = "make+sure/using_unquote"
1436 expect = os.path.join("make+sure", "using_unquote")
1437 result = urllib.request.url2pathname(given)
1438 self.assertEqual(expect, result,
1439 "url2pathname() failed; %s != %s" %
1440 (expect, result))
1441
1442 def test_pathname2url(self):
1443 # Test cases common to Windows and POSIX.
1444 fn = urllib.request.pathname2url
1445 sep = os.path.sep
1446 self.assertEqual(fn(''), '')
1447 self.assertEqual(fn(sep), '///')
1448 self.assertEqual(fn('a'), 'a')
1449 self.assertEqual(fn(f'a{sep}b.c'), 'a/b.c')
1450 self.assertEqual(fn(f'{sep}a{sep}b.c'), '///a/b.c')
1451 self.assertEqual(fn(f'{sep}a{sep}b%#c'), '///a/b%25%23c')
1452
1453 def test_pathname2url_add_scheme(self):
1454 sep = os.path.sep
1455 subtests = [
1456 ('', 'file:'),
1457 (sep, 'file:///'),
1458 ('a', 'file:a'),
1459 (f'a{sep}b.c', 'file:a/b.c'),
1460 (f'{sep}a{sep}b.c', 'file:///a/b.c'),
1461 (f'{sep}a{sep}b%#c', 'file:///a/b%25%23c'),
1462 ]
1463 for path, expected_url in subtests:
1464 with self.subTest(path=path):
1465 self.assertEqual(
1466 urllib.request.pathname2url(path, add_scheme=True), expected_url)
1467
1468 @unittest.skipUnless(sys.platform == 'win32',
1469 'test specific to Windows pathnames.')
1470 def test_pathname2url_win(self):
1471 # Test special prefixes are correctly handled in pathname2url()
1472 fn = urllib.request.pathname2url
1473 self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir')
1474 self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '//server/share/dir')
1475 self.assertEqual(fn("C:"), '///C:')
1476 self.assertEqual(fn("C:\\"), '///C:/')
1477 self.assertEqual(fn('c:\\a\\b.c'), '///c:/a/b.c')
1478 self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
1479 self.assertEqual(fn('C:\\a\\b.c\\'), '///C:/a/b.c/')
1480 self.assertEqual(fn('C:\\a\\\\b.c'), '///C:/a//b.c')
1481 self.assertEqual(fn('C:\\a\\b%#c'), '///C:/a/b%25%23c')
1482 self.assertEqual(fn('C:\\a\\b\xe9'), '///C:/a/b%C3%A9')
1483 self.assertEqual(fn('C:\\foo\\bar\\spam.foo'), "///C:/foo/bar/spam.foo")
1484 # NTFS alternate data streams
1485 self.assertEqual(fn('C:\\foo:bar'), '///C:/foo%3Abar')
1486 self.assertEqual(fn('foo:bar'), 'foo%3Abar')
1487 # No drive letter
1488 self.assertEqual(fn("\\folder\\test\\"), '///folder/test/')
1489 self.assertEqual(fn("\\\\folder\\test\\"), '//folder/test/')
1490 self.assertEqual(fn("\\\\\\folder\\test\\"), '///folder/test/')
1491 self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
1492 self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
1493 self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
1494 # Alternate path separator
1495 self.assertEqual(fn('C:/a/b.c'), '///C:/a/b.c')
1496 self.assertEqual(fn('//some/share/a/b.c'), '//some/share/a/b.c')
1497 self.assertEqual(fn('//?/C:/dir'), '///C:/dir')
1498 self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir')
1499 # Round-tripping
1500 urls = ['///C:',
1501 '///folder/test/',
1502 '///C:/foo/bar/spam.foo']
1503 for url in urls:
1504 self.assertEqual(fn(urllib.request.url2pathname(url)), url)
1505
1506 @unittest.skipIf(sys.platform == 'win32',
1507 'test specific to POSIX pathnames')
1508 def test_pathname2url_posix(self):
1509 fn = urllib.request.pathname2url
1510 self.assertEqual(fn('//a/b.c'), '////a/b.c')
1511 self.assertEqual(fn('///a/b.c'), '/////a/b.c')
1512 self.assertEqual(fn('////a/b.c'), '//////a/b.c')
1513
1514 @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
1515 def test_pathname2url_nonascii(self):
1516 encoding = sys.getfilesystemencoding()
1517 errors = sys.getfilesystemencodeerrors()
1518 url = urllib.parse.quote(os_helper.FS_NONASCII, encoding=encoding, errors=errors)
1519 self.assertEqual(urllib.request.pathname2url(os_helper.FS_NONASCII), url)
1520
1521 def test_url2pathname(self):
1522 # Test cases common to Windows and POSIX.
1523 fn = urllib.request.url2pathname
1524 sep = os.path.sep
1525 self.assertEqual(fn(''), '')
1526 self.assertEqual(fn('/'), f'{sep}')
1527 self.assertEqual(fn('///'), f'{sep}')
1528 self.assertEqual(fn('////'), f'{sep}{sep}')
1529 self.assertEqual(fn('foo'), 'foo')
1530 self.assertEqual(fn('foo/bar'), f'foo{sep}bar')
1531 self.assertEqual(fn('/foo/bar'), f'{sep}foo{sep}bar')
1532 self.assertEqual(fn('//localhost/foo/bar'), f'{sep}foo{sep}bar')
1533 self.assertEqual(fn('///foo/bar'), f'{sep}foo{sep}bar')
1534 self.assertEqual(fn('////foo/bar'), f'{sep}{sep}foo{sep}bar')
1535 self.assertEqual(fn('data:blah'), 'data:blah')
1536 self.assertEqual(fn('data://blah'), f'data:{sep}{sep}blah')
1537 self.assertEqual(fn('foo?bar'), 'foo')
1538 self.assertEqual(fn('foo#bar'), 'foo')
1539 self.assertEqual(fn('foo?bar=baz'), 'foo')
1540 self.assertEqual(fn('foo?bar#baz'), 'foo')
1541 self.assertEqual(fn('foo%3Fbar'), 'foo?bar')
1542 self.assertEqual(fn('foo%23bar'), 'foo#bar')
1543 self.assertEqual(fn('foo%3Fbar%3Dbaz'), 'foo?bar=baz')
1544 self.assertEqual(fn('foo%3Fbar%23baz'), 'foo?bar#baz')
1545
1546 def test_url2pathname_require_scheme(self):
1547 sep = os.path.sep
1548 subtests = [
1549 ('file:', ''),
1550 ('FILE:', ''),
1551 ('FiLe:', ''),
1552 ('file:/', f'{sep}'),
1553 ('file:///', f'{sep}'),
1554 ('file:////', f'{sep}{sep}'),
1555 ('file:foo', 'foo'),
1556 ('file:foo/bar', f'foo{sep}bar'),
1557 ('file:/foo/bar', f'{sep}foo{sep}bar'),
1558 ('file://localhost/foo/bar', f'{sep}foo{sep}bar'),
1559 ('file:///foo/bar', f'{sep}foo{sep}bar'),
1560 ('file:////foo/bar', f'{sep}{sep}foo{sep}bar'),
1561 ('file:data:blah', 'data:blah'),
1562 ('file:data://blah', f'data:{sep}{sep}blah'),
1563 ]
1564 for url, expected_path in subtests:
1565 with self.subTest(url=url):
1566 self.assertEqual(
1567 urllib.request.url2pathname(url, require_scheme=True),
1568 expected_path)
1569
1570 def test_url2pathname_require_scheme_errors(self):
1571 subtests = [
1572 '',
1573 ':',
1574 'foo',
1575 'http:foo',
1576 'localfile:foo',
1577 'data:foo',
1578 'data:file:foo',
1579 'data:file://foo',
1580 ]
1581 for url in subtests:
1582 with self.subTest(url=url):
1583 self.assertRaises(
1584 urllib.error.URLError,
1585 urllib.request.url2pathname,
1586 url, require_scheme=True)
1587
1588 @unittest.skipIf(support.is_emscripten, "Fixed by https://github.com/emscripten-core/emscripten/pull/24593")
1589 def test_url2pathname_resolve_host(self):
1590 fn = urllib.request.url2pathname
1591 sep = os.path.sep
1592 self.assertEqual(fn('//127.0.0.1/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
1593 self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar')
1594 self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
1595
1596 @unittest.skipUnless(sys.platform == 'win32',
1597 'test specific to Windows pathnames.')
1598 def test_url2pathname_win(self):
1599 fn = urllib.request.url2pathname
1600 self.assertEqual(fn('/C:/'), 'C:\\')
1601 self.assertEqual(fn('//C:'), 'C:')
1602 self.assertEqual(fn('//C:/'), 'C:\\')
1603 self.assertEqual(fn('//C:\\'), 'C:\\')
1604 self.assertEqual(fn('//C:80/'), 'C:80\\')
1605 self.assertEqual(fn("///C|"), 'C:')
1606 self.assertEqual(fn("///C:"), 'C:')
1607 self.assertEqual(fn('///C:/'), 'C:\\')
1608 self.assertEqual(fn('/C|//'), 'C:\\\\')
1609 self.assertEqual(fn('///C|/path'), 'C:\\path')
1610 # No DOS drive
1611 self.assertEqual(fn("///C/test/"), '\\C\\test\\')
1612 self.assertEqual(fn("////C/test/"), '\\\\C\\test\\')
1613 # DOS drive paths
1614 self.assertEqual(fn('c:/path/to/file'), 'c:\\path\\to\\file')
1615 self.assertEqual(fn('C:/path/to/file'), 'C:\\path\\to\\file')
1616 self.assertEqual(fn('C:/path/to/file/'), 'C:\\path\\to\\file\\')
1617 self.assertEqual(fn('C:/path/to//file'), 'C:\\path\\to\\\\file')
1618 self.assertEqual(fn('C|/path/to/file'), 'C:\\path\\to\\file')
1619 self.assertEqual(fn('/C|/path/to/file'), 'C:\\path\\to\\file')
1620 self.assertEqual(fn('///C|/path/to/file'), 'C:\\path\\to\\file')
1621 self.assertEqual(fn("///C|/foo/bar/spam.foo"), 'C:\\foo\\bar\\spam.foo')
1622 # Colons in URI
1623 self.assertEqual(fn('///\u00e8|/'), '\u00e8:\\')
1624 self.assertEqual(fn('//host/share/spam.txt:eggs'), '\\\\host\\share\\spam.txt:eggs')
1625 self.assertEqual(fn('///c:/spam.txt:eggs'), 'c:\\spam.txt:eggs')
1626 # UNC paths
1627 self.assertEqual(fn('//server/path/to/file'), '\\\\server\\path\\to\\file')
1628 self.assertEqual(fn('////server/path/to/file'), '\\\\server\\path\\to\\file')
1629 self.assertEqual(fn('/////server/path/to/file'), '\\\\server\\path\\to\\file')
1630 self.assertEqual(fn('//127.0.0.1/path/to/file'), '\\\\127.0.0.1\\path\\to\\file')
1631 # Localhost paths
1632 self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file')
1633 self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file')
1634 self.assertEqual(fn('//localhost/path/to/file'), '\\path\\to\\file')
1635 self.assertEqual(fn('//localhost//server/path/to/file'), '\\\\server\\path\\to\\file')
1636 # Percent-encoded forward slashes are preserved for backwards compatibility
1637 self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar')
1638 self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar')
1639 # Round-tripping
1640 paths = ['C:',
1641 r'\C\test\\',
1642 r'C:\foo\bar\spam.foo']
1643 for path in paths:
1644 self.assertEqual(fn(urllib.request.pathname2url(path)), path)
1645
1646 @unittest.skipIf(sys.platform == 'win32',
1647 'test specific to POSIX pathnames')
1648 def test_url2pathname_posix(self):
1649 fn = urllib.request.url2pathname
1650 self.assertRaises(urllib.error.URLError, fn, '//foo/bar')
1651 self.assertRaises(urllib.error.URLError, fn, '//localhost:/foo/bar')
1652 self.assertRaises(urllib.error.URLError, fn, '//:80/foo/bar')
1653 self.assertRaises(urllib.error.URLError, fn, '//:/foo/bar')
1654 self.assertRaises(urllib.error.URLError, fn, '//c:80/foo/bar')
1655 self.assertRaises(urllib.error.URLError, fn, '//127.0.0.1/foo/bar')
1656
1657 @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
1658 def test_url2pathname_nonascii(self):
1659 encoding = sys.getfilesystemencoding()
1660 errors = sys.getfilesystemencodeerrors()
1661 url = os_helper.FS_NONASCII
1662 self.assertEqual(urllib.request.url2pathname(url), os_helper.FS_NONASCII)
1663 url = urllib.parse.quote(url, encoding=encoding, errors=errors)
1664 self.assertEqual(urllib.request.url2pathname(url), os_helper.FS_NONASCII)
1665
1666 class Utility_Tests(unittest.TestCase):
1667 """Testcase to test the various utility functions in the urllib."""
1668
1669 def test_thishost(self):
1670 """Test the urllib.request.thishost utility function returns a tuple"""
1671 self.assertIsInstance(urllib.request.thishost(), tuple)
1672
1673
1674 class RequestTests(unittest.TestCase):
1675 """Unit tests for urllib.request.Request."""
1676
1677 def test_default_values(self):
1678 Request = urllib.request.Request
1679 request = Request("http://www.python.org")
1680 self.assertEqual(request.get_method(), 'GET')
1681 request = Request("http://www.python.org", {})
1682 self.assertEqual(request.get_method(), 'POST')
1683
1684 def test_with_method_arg(self):
1685 Request = urllib.request.Request
1686 request = Request("http://www.python.org", method='HEAD')
1687 self.assertEqual(request.method, 'HEAD')
1688 self.assertEqual(request.get_method(), 'HEAD')
1689 request = Request("http://www.python.org", {}, method='HEAD')
1690 self.assertEqual(request.method, 'HEAD')
1691 self.assertEqual(request.get_method(), 'HEAD')
1692 request = Request("http://www.python.org", method='GET')
1693 self.assertEqual(request.get_method(), 'GET')
1694 request.method = 'HEAD'
1695 self.assertEqual(request.get_method(), 'HEAD')
1696
1697
1698 if __name__ == '__main__':
1699 unittest.main()