blob: d8c3512fa850aa1f0e32a448aedb3064e135fad5 [file] [log] [blame]
Brett Cannon74bfd702003-04-25 09:39:47 +00001"""Regresssion tests for urllib"""
2
Jeremy Hylton1afc1692008-06-18 20:49:58 +00003import urllib.parse
4import urllib.request
guido@google.coma119df92011-03-29 11:41:02 -07005import urllib.error
Georg Brandl24420152008-05-26 16:32:26 +00006import http.client
Barry Warsaw820c1202008-06-12 04:06:45 +00007import email.message
Jeremy Hylton66dc8c52007-08-04 03:42:26 +00008import io
Brett Cannon74bfd702003-04-25 09:39:47 +00009import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
Brett Cannon74bfd702003-04-25 09:39:47 +000011import os
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +080012import sys
Georg Brandl5a650a22005-08-26 08:51:34 +000013import tempfile
Jeremy Hylton6102e292000-08-31 15:48:10 +000014
Senthil Kumaranc5c5a142012-01-14 19:09:04 +080015from base64 import b64encode
Georg Brandl2daf6ae2012-02-20 19:54:16 +010016import collections
Senthil Kumaranc5c5a142012-01-14 19:09:04 +080017
Brett Cannon74bfd702003-04-25 09:39:47 +000018def hexescape(char):
19 """Escape char as RFC 2396 specifies"""
20 hex_repr = hex(ord(char))[2:].upper()
21 if len(hex_repr) == 1:
22 hex_repr = "0%s" % hex_repr
23 return "%" + hex_repr
Jeremy Hylton6102e292000-08-31 15:48:10 +000024
Jeremy Hylton1afc1692008-06-18 20:49:58 +000025# Shortcut for testing FancyURLopener
26_urlopener = None
27def urlopen(url, data=None, proxies=None):
28 """urlopen(url [, data]) -> open file-like object"""
29 global _urlopener
30 if proxies is not None:
31 opener = urllib.request.FancyURLopener(proxies=proxies)
32 elif not _urlopener:
33 opener = urllib.request.FancyURLopener()
34 _urlopener = opener
35 else:
36 opener = _urlopener
37 if data is None:
38 return opener.open(url)
39 else:
40 return opener.open(url, data)
41
Senthil Kumarance260142011-11-01 01:35:17 +080042
43class FakeHTTPMixin(object):
44 def fakehttp(self, fakedata):
45 class FakeSocket(io.BytesIO):
46 io_refs = 1
47
Senthil Kumaranc5c5a142012-01-14 19:09:04 +080048 def sendall(self, data):
49 FakeHTTPConnection.buf = data
Senthil Kumarance260142011-11-01 01:35:17 +080050
51 def makefile(self, *args, **kwds):
52 self.io_refs += 1
53 return self
54
55 def read(self, amt=None):
56 if self.closed:
57 return b""
58 return io.BytesIO.read(self, amt)
59
60 def readline(self, length=None):
61 if self.closed:
62 return b""
63 return io.BytesIO.readline(self, length)
64
65 def close(self):
66 self.io_refs -= 1
67 if self.io_refs == 0:
68 io.BytesIO.close(self)
69
70 class FakeHTTPConnection(http.client.HTTPConnection):
Senthil Kumaranc5c5a142012-01-14 19:09:04 +080071
72 # buffer to store data for verification in urlopen tests.
73 buf = None
74
Senthil Kumarance260142011-11-01 01:35:17 +080075 def connect(self):
76 self.sock = FakeSocket(fakedata)
Senthil Kumaranc5c5a142012-01-14 19:09:04 +080077
Senthil Kumarance260142011-11-01 01:35:17 +080078 self._connection_class = http.client.HTTPConnection
79 http.client.HTTPConnection = FakeHTTPConnection
80
81 def unfakehttp(self):
82 http.client.HTTPConnection = self._connection_class
83
84
Brett Cannon74bfd702003-04-25 09:39:47 +000085class urlopen_FileTests(unittest.TestCase):
86 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000087
Brett Cannon74bfd702003-04-25 09:39:47 +000088 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000089 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000090
Brett Cannon74bfd702003-04-25 09:39:47 +000091 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000092
Brett Cannon74bfd702003-04-25 09:39:47 +000093 def setUp(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000094 # Create a temp file to use for testing
95 self.text = bytes("test_urllib: %s\n" % self.__class__.__name__,
96 "ascii")
97 f = open(support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000098 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000099 f.write(self.text)
Brett Cannon74bfd702003-04-25 09:39:47 +0000100 finally:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000101 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000102 self.pathname = support.TESTFN
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000103 self.returned_obj = urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +0000104
Brett Cannon74bfd702003-04-25 09:39:47 +0000105 def tearDown(self):
106 """Shut down the open object"""
107 self.returned_obj.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000108 os.remove(support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +0000109
Brett Cannon74bfd702003-04-25 09:39:47 +0000110 def test_interface(self):
111 # Make sure object returned by urlopen() has the specified methods
112 for attr in ("read", "readline", "readlines", "fileno",
Christian Heimes9bd667a2008-01-20 15:14:11 +0000113 "close", "info", "geturl", "getcode", "__iter__"):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000114 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +0000115 "object returned by urlopen() lacks %s attribute" %
116 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000117
Brett Cannon74bfd702003-04-25 09:39:47 +0000118 def test_read(self):
119 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +0000120
Brett Cannon74bfd702003-04-25 09:39:47 +0000121 def test_readline(self):
122 self.assertEqual(self.text, self.returned_obj.readline())
Guido van Rossuma0982942007-07-10 08:30:03 +0000123 self.assertEqual(b'', self.returned_obj.readline(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000124 "calling readline() after exhausting the file did not"
125 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +0000126
Brett Cannon74bfd702003-04-25 09:39:47 +0000127 def test_readlines(self):
128 lines_list = self.returned_obj.readlines()
129 self.assertEqual(len(lines_list), 1,
130 "readlines() returned the wrong number of lines")
131 self.assertEqual(lines_list[0], self.text,
132 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +0000133
Brett Cannon74bfd702003-04-25 09:39:47 +0000134 def test_fileno(self):
135 file_num = self.returned_obj.fileno()
Ezio Melottie9615932010-01-24 19:26:24 +0000136 self.assertIsInstance(file_num, int, "fileno() did not return an int")
Brett Cannon74bfd702003-04-25 09:39:47 +0000137 self.assertEqual(os.read(file_num, len(self.text)), self.text,
138 "Reading on the file descriptor returned by fileno() "
139 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000140
Brett Cannon74bfd702003-04-25 09:39:47 +0000141 def test_close(self):
Senthil Kumarand91ffca2011-03-19 17:25:27 +0800142 # Test close() by calling it here and then having it be called again
Brett Cannon74bfd702003-04-25 09:39:47 +0000143 # by the tearDown() method for the test
144 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +0000145
Brett Cannon74bfd702003-04-25 09:39:47 +0000146 def test_info(self):
Ezio Melottie9615932010-01-24 19:26:24 +0000147 self.assertIsInstance(self.returned_obj.info(), email.message.Message)
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000148
Brett Cannon74bfd702003-04-25 09:39:47 +0000149 def test_geturl(self):
150 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +0000151
Christian Heimes9bd667a2008-01-20 15:14:11 +0000152 def test_getcode(self):
Florent Xicluna419e3842010-08-08 16:16:07 +0000153 self.assertIsNone(self.returned_obj.getcode())
Christian Heimes9bd667a2008-01-20 15:14:11 +0000154
Brett Cannon74bfd702003-04-25 09:39:47 +0000155 def test_iter(self):
156 # Test iterator
157 # Don't need to count number of iterations since test would fail the
158 # instant it returned anything beyond the first line from the
Raymond Hettinger038018a2011-06-26 14:29:35 +0200159 # comparison.
160 # Use the iterator in the usual implicit way to test for ticket #4608.
161 for line in self.returned_obj:
Brett Cannon74bfd702003-04-25 09:39:47 +0000162 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +0000163
Senthil Kumaran3800ea92012-01-21 11:52:48 +0800164 def test_relativelocalfile(self):
165 self.assertRaises(ValueError,urllib.request.urlopen,'./' + self.pathname)
166
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000167class ProxyTests(unittest.TestCase):
168
169 def setUp(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000170 # Records changes to env vars
171 self.env = support.EnvironmentVarGuard()
Benjamin Peterson46a99002010-01-09 18:45:30 +0000172 # Delete all proxy related env vars
Antoine Pitroub3a88b52010-10-14 18:31:39 +0000173 for k in list(os.environ):
Antoine Pitrou8c8f1ac2010-10-14 18:32:54 +0000174 if 'proxy' in k.lower():
Benjamin Peterson46a99002010-01-09 18:45:30 +0000175 self.env.unset(k)
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000176
177 def tearDown(self):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000178 # Restore all proxy related env vars
Walter Dörwaldb525e182009-04-26 21:39:21 +0000179 self.env.__exit__()
180 del self.env
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000181
182 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000183 self.env.set('NO_PROXY', 'localhost')
184 proxies = urllib.request.getproxies_environment()
185 # getproxies_environment use lowered case truncated (no '_proxy') keys
Florent Xicluna419e3842010-08-08 16:16:07 +0000186 self.assertEqual('localhost', proxies['no'])
Senthil Kumaran89976f12011-08-06 12:27:40 +0800187 # List of no_proxies with space.
188 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
189 self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com'))
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000190
Senthil Kumarance260142011-11-01 01:35:17 +0800191class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000192 """Test urlopen() opening a fake http connection."""
193
Antoine Pitrou988dbd72010-12-17 17:35:56 +0000194 def check_read(self, ver):
195 self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000196 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000197 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000198 self.assertEqual(fp.readline(), b"Hello!")
199 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000200 self.assertEqual(fp.geturl(), 'http://python.org/')
201 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000202 finally:
203 self.unfakehttp()
204
Senthil Kumaran26430412011-04-13 07:01:19 +0800205 def test_url_fragment(self):
206 # Issue #11703: geturl() omits fragments in the original URL.
207 url = 'http://docs.python.org/library/urllib.html#OK'
Senthil Kumaranb17abb12011-04-13 07:22:29 +0800208 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
Senthil Kumaran26430412011-04-13 07:01:19 +0800209 try:
210 fp = urllib.request.urlopen(url)
211 self.assertEqual(fp.geturl(), url)
212 finally:
213 self.unfakehttp()
214
Senthil Kumarand91ffca2011-03-19 17:25:27 +0800215 def test_willclose(self):
216 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
Senthil Kumaranacbaa922011-03-20 05:30:16 +0800217 try:
218 resp = urlopen("http://www.python.org")
219 self.assertTrue(resp.fp.will_close)
220 finally:
221 self.unfakehttp()
Senthil Kumarand91ffca2011-03-19 17:25:27 +0800222
Antoine Pitrou988dbd72010-12-17 17:35:56 +0000223 def test_read_0_9(self):
224 # "0.9" response accepted (but not "simple responses" without
225 # a status line)
226 self.check_read(b"0.9")
227
228 def test_read_1_0(self):
229 self.check_read(b"1.0")
230
231 def test_read_1_1(self):
232 self.check_read(b"1.1")
233
Christian Heimes57dddfb2008-01-02 18:30:52 +0000234 def test_read_bogus(self):
235 # urlopen() should raise IOError for many error codes.
236 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
237Date: Wed, 02 Jan 2008 03:03:54 GMT
238Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
239Connection: close
240Content-Type: text/html; charset=iso-8859-1
241''')
242 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000243 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000244 finally:
245 self.unfakehttp()
246
guido@google.coma119df92011-03-29 11:41:02 -0700247 def test_invalid_redirect(self):
248 # urlopen() should raise IOError for many error codes.
249 self.fakehttp(b'''HTTP/1.1 302 Found
250Date: Wed, 02 Jan 2008 03:03:54 GMT
251Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
252Location: file://guidocomputer.athome.com:/python/license
253Connection: close
254Content-Type: text/html; charset=iso-8859-1
255''')
256 try:
257 self.assertRaises(urllib.error.HTTPError, urlopen,
258 "http://python.org/")
259 finally:
260 self.unfakehttp()
261
Guido van Rossumd8faa362007-04-27 19:54:29 +0000262 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000263 # urlopen() raises IOError if the underlying socket does not send any
264 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000265 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000266 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000267 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000268 finally:
269 self.unfakehttp()
270
Senthil Kumaranf5776862012-10-21 13:30:02 -0700271 def test_missing_localfile(self):
272 # Test for #10836
Senthil Kumaran3ebef362012-10-21 18:31:25 -0700273 with self.assertRaises(urllib.error.URLError) as e:
Senthil Kumaranf5776862012-10-21 13:30:02 -0700274 urlopen('file://localhost/a/file/which/doesnot/exists.py')
Senthil Kumaran3ebef362012-10-21 18:31:25 -0700275 self.assertTrue(e.exception.filename)
276 self.assertTrue(e.exception.reason)
277
278 def test_file_notexists(self):
279 fd, tmp_file = tempfile.mkstemp()
Senthil Kumaran3194d7c2012-10-23 09:40:53 -0700280 tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
Senthil Kumaranbf644c52012-10-23 11:07:02 -0700281 try:
282 self.assertTrue(os.path.exists(tmp_file))
283 with urlopen(tmp_fileurl) as fobj:
284 self.assertTrue(fobj)
285 finally:
286 os.close(fd)
287 os.unlink(tmp_file)
Senthil Kumaran3ebef362012-10-21 18:31:25 -0700288 self.assertFalse(os.path.exists(tmp_file))
289 with self.assertRaises(urllib.error.URLError):
290 urlopen(tmp_fileurl)
291
292 def test_ftp_nohost(self):
293 test_ftp_url = 'ftp:///path'
294 with self.assertRaises(urllib.error.URLError) as e:
295 urlopen(test_ftp_url)
296 self.assertFalse(e.exception.filename)
297 self.assertTrue(e.exception.reason)
298
299 def test_ftp_nonexisting(self):
300 with self.assertRaises(urllib.error.URLError) as e:
301 urlopen('ftp://localhost/a/file/which/doesnot/exists.py')
302 self.assertFalse(e.exception.filename)
303 self.assertTrue(e.exception.reason)
304
Senthil Kumaranf5776862012-10-21 13:30:02 -0700305
Senthil Kumarande0eb242010-08-01 17:53:37 +0000306 def test_userpass_inurl(self):
Antoine Pitrou988dbd72010-12-17 17:35:56 +0000307 self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
Senthil Kumarande0eb242010-08-01 17:53:37 +0000308 try:
309 fp = urlopen("http://user:pass@python.org/")
310 self.assertEqual(fp.readline(), b"Hello!")
311 self.assertEqual(fp.readline(), b"")
312 self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
313 self.assertEqual(fp.getcode(), 200)
314 finally:
315 self.unfakehttp()
316
Senthil Kumaranc5c5a142012-01-14 19:09:04 +0800317 def test_userpass_inurl_w_spaces(self):
318 self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
319 try:
320 userpass = "a b:c d"
321 url = "http://{}@python.org/".format(userpass)
322 fakehttp_wrapper = http.client.HTTPConnection
323 authorization = ("Authorization: Basic %s\r\n" %
324 b64encode(userpass.encode("ASCII")).decode("ASCII"))
325 fp = urlopen(url)
326 # The authorization header must be in place
327 self.assertIn(authorization, fakehttp_wrapper.buf.decode("UTF-8"))
328 self.assertEqual(fp.readline(), b"Hello!")
329 self.assertEqual(fp.readline(), b"")
330 # the spaces are quoted in URL so no match
331 self.assertNotEqual(fp.geturl(), url)
332 self.assertEqual(fp.getcode(), 200)
333 finally:
334 self.unfakehttp()
335
Senthil Kumaran38b968b92012-03-14 13:43:53 -0700336 def test_URLopener_deprecation(self):
337 with support.check_warnings(('',DeprecationWarning)):
Senthil Kumaran3ebef362012-10-21 18:31:25 -0700338 urllib.request.URLopener()
Senthil Kumaran38b968b92012-03-14 13:43:53 -0700339
Brett Cannon19691362003-04-29 05:08:06 +0000340class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000341 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000342
Brett Cannon19691362003-04-29 05:08:06 +0000343 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000344 # Create a list of temporary files. Each item in the list is a file
345 # name (absolute path or relative to the current working directory).
346 # All files in this list will be deleted in the tearDown method. Note,
347 # this only helps to makes sure temporary files get deleted, but it
348 # does nothing about trying to close files that may still be open. It
349 # is the responsibility of the developer to properly close files even
350 # when exceptional conditions occur.
351 self.tempFiles = []
352
Brett Cannon19691362003-04-29 05:08:06 +0000353 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000354 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000355 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000356 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000357 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000358 FILE.write(self.text)
359 FILE.close()
360 finally:
361 try: FILE.close()
362 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000363
364 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000365 # Delete the temporary files.
366 for each in self.tempFiles:
367 try: os.remove(each)
368 except: pass
369
370 def constructLocalFileUrl(self, filePath):
Victor Stinner6c6f8512010-08-07 10:09:35 +0000371 filePath = os.path.abspath(filePath)
372 try:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000373 filePath.encode("utf-8")
Victor Stinner6c6f8512010-08-07 10:09:35 +0000374 except UnicodeEncodeError:
375 raise unittest.SkipTest("filePath is not encodable to utf8")
376 return "file://%s" % urllib.request.pathname2url(filePath)
Georg Brandl5a650a22005-08-26 08:51:34 +0000377
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000378 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000379 """Creates a new temporary file containing the specified data,
380 registers the file for deletion during the test fixture tear down, and
381 returns the absolute path of the file."""
382
383 newFd, newFilePath = tempfile.mkstemp()
384 try:
385 self.registerFileForCleanUp(newFilePath)
386 newFile = os.fdopen(newFd, "wb")
387 newFile.write(data)
388 newFile.close()
389 finally:
390 try: newFile.close()
391 except: pass
392 return newFilePath
393
394 def registerFileForCleanUp(self, fileName):
395 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000396
397 def test_basic(self):
398 # Make sure that a local file just gets its own location returned and
399 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000400 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000401 self.assertEqual(result[0], support.TESTFN)
Ezio Melottie9615932010-01-24 19:26:24 +0000402 self.assertIsInstance(result[1], email.message.Message,
403 "did not get a email.message.Message instance "
404 "as second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000405
406 def test_copy(self):
407 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000408 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000409 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000410 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000411 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000412 self.assertEqual(second_temp, result[0])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000413 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000414 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000415 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000416 try:
417 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000418 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000419 finally:
420 try: FILE.close()
421 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000422 self.assertEqual(self.text, text)
423
424 def test_reporthook(self):
425 # Make sure that the reporthook works.
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700426 def hooktester(block_count, block_read_size, file_size, count_holder=[0]):
427 self.assertIsInstance(block_count, int)
428 self.assertIsInstance(block_read_size, int)
429 self.assertIsInstance(file_size, int)
430 self.assertEqual(block_count, count_holder[0])
Brett Cannon19691362003-04-29 05:08:06 +0000431 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000432 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000433 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000434 urllib.request.urlretrieve(
435 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000436 second_temp, hooktester)
437
438 def test_reporthook_0_bytes(self):
439 # Test on zero length file. Should call reporthook only 1 time.
440 report = []
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700441 def hooktester(block_count, block_read_size, file_size, _report=report):
442 _report.append((block_count, block_read_size, file_size))
Georg Brandl5a650a22005-08-26 08:51:34 +0000443 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000444 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000445 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000446 self.assertEqual(len(report), 1)
447 self.assertEqual(report[0][2], 0)
448
449 def test_reporthook_5_bytes(self):
450 # Test on 5 byte file. Should call reporthook only 2 times (once when
451 # the "network connection" is established and once when the block is
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700452 # read).
Georg Brandl5a650a22005-08-26 08:51:34 +0000453 report = []
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700454 def hooktester(block_count, block_read_size, file_size, _report=report):
455 _report.append((block_count, block_read_size, file_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000456 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000457 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000458 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000459 self.assertEqual(len(report), 2)
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700460 self.assertEqual(report[0][1], 0)
461 self.assertEqual(report[1][1], 5)
Georg Brandl5a650a22005-08-26 08:51:34 +0000462
463 def test_reporthook_8193_bytes(self):
464 # Test on 8193 byte file. Should call reporthook only 3 times (once
465 # when the "network connection" is established, once for the next 8192
466 # bytes, and once for the last byte).
467 report = []
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700468 def hooktester(block_count, block_read_size, file_size, _report=report):
469 _report.append((block_count, block_read_size, file_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000470 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000471 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000472 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000473 self.assertEqual(len(report), 3)
Senthil Kumarane24f96a2012-03-13 19:29:33 -0700474 self.assertEqual(report[0][1], 0)
475 self.assertEqual(report[1][1], 8192)
476 self.assertEqual(report[2][1], 1)
Skip Montanaro080c9972001-01-28 21:12:22 +0000477
Senthil Kumarance260142011-11-01 01:35:17 +0800478
479class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
480 """Test urllib.urlretrieve() using fake http connections"""
481
482 def test_short_content_raises_ContentTooShortError(self):
483 self.fakehttp(b'''HTTP/1.1 200 OK
484Date: Wed, 02 Jan 2008 03:03:54 GMT
485Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
486Connection: close
487Content-Length: 100
488Content-Type: text/html; charset=iso-8859-1
489
490FF
491''')
492
493 def _reporthook(par1, par2, par3):
494 pass
495
496 with self.assertRaises(urllib.error.ContentTooShortError):
497 try:
498 urllib.request.urlretrieve('http://example.com/',
499 reporthook=_reporthook)
500 finally:
501 self.unfakehttp()
502
503 def test_short_content_raises_ContentTooShortError_without_reporthook(self):
504 self.fakehttp(b'''HTTP/1.1 200 OK
505Date: Wed, 02 Jan 2008 03:03:54 GMT
506Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
507Connection: close
508Content-Length: 100
509Content-Type: text/html; charset=iso-8859-1
510
511FF
512''')
513 with self.assertRaises(urllib.error.ContentTooShortError):
514 try:
515 urllib.request.urlretrieve('http://example.com/')
516 finally:
517 self.unfakehttp()
518
519
Brett Cannon74bfd702003-04-25 09:39:47 +0000520class QuotingTests(unittest.TestCase):
521 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000522
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000523 According to RFC 2396 (Uniform Resource Identifiers), to escape a
524 character you write it as '%' + <2 character US-ASCII hex value>.
525 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
526 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000527
528 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000529
Brett Cannon74bfd702003-04-25 09:39:47 +0000530 Reserved characters : ";/?:@&=+$,"
531 Have special meaning in URIs and must be escaped if not being used for
532 their special meaning
533 Data characters : letters, digits, and "-_.!~*'()"
534 Unreserved and do not need to be escaped; can be, though, if desired
535 Control characters : 0x00 - 0x1F, 0x7F
536 Have no use in URIs so must be escaped
537 space : 0x20
538 Must be escaped
539 Delimiters : '<>#%"'
540 Must be escaped
541 Unwise : "{}|\^[]`"
542 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000543
Brett Cannon74bfd702003-04-25 09:39:47 +0000544 """
545
546 def test_never_quote(self):
547 # Make sure quote() does not quote letters, digits, and "_,.-"
548 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
549 "abcdefghijklmnopqrstuvwxyz",
550 "0123456789",
551 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000552 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000553 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000554 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000555 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000556 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000557 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000558
559 def test_default_safe(self):
560 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000561 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000562
563 def test_safe(self):
564 # Test setting 'safe' parameter does what it should do
565 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000566 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000567 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000568 "using quote(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000569 result = urllib.parse.quote_plus(quote_by_default,
570 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000571 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000572 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000573 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000574 # Safe expressed as bytes rather than str
575 result = urllib.parse.quote(quote_by_default, safe=b"<>")
576 self.assertEqual(quote_by_default, result,
577 "using quote(): %r != %r" % (quote_by_default, result))
578 # "Safe" non-ASCII characters should have no effect
579 # (Since URIs are not allowed to have non-ASCII characters)
580 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
581 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
582 self.assertEqual(expect, result,
583 "using quote(): %r != %r" %
584 (expect, result))
585 # Same as above, but using a bytes rather than str
586 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
587 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
588 self.assertEqual(expect, result,
589 "using quote(): %r != %r" %
590 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000591
592 def test_default_quoting(self):
593 # Make sure all characters that should be quoted are by default sans
594 # space (separate test for that).
595 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
596 should_quote.append('<>#%"{}|\^[]`')
597 should_quote.append(chr(127)) # For 0x7F
598 should_quote = ''.join(should_quote)
599 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000600 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000601 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000602 "using quote(): "
603 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000604 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000605 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000606 self.assertEqual(hexescape(char), result,
607 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000608 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000609 (char, hexescape(char), result))
610 del should_quote
611 partial_quote = "ab[]cd"
612 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000613 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000614 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000615 "using quote(): %r != %r" % (expected, result))
Senthil Kumaran305a68e2011-09-13 06:40:27 +0800616 result = urllib.parse.quote_plus(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000617 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000618 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000619
620 def test_quoting_space(self):
621 # Make sure quote() and quote_plus() handle spaces as specified in
622 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000623 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000624 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000625 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000626 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000627 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000628 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000629 given = "a b cd e f"
630 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000631 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000632 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000633 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000634 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000635 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000636 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000637 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000638
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000639 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000640 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000641 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000642 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000643 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000644 # Test with bytes
645 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
646 'alpha%2Bbeta+gamma')
647 # Test with safe bytes
648 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
649 'alpha+beta+gamma')
650
651 def test_quote_bytes(self):
652 # Bytes should quote directly to percent-encoded values
653 given = b"\xa2\xd8ab\xff"
654 expect = "%A2%D8ab%FF"
655 result = urllib.parse.quote(given)
656 self.assertEqual(expect, result,
657 "using quote(): %r != %r" % (expect, result))
658 # Encoding argument should raise type error on bytes input
659 self.assertRaises(TypeError, urllib.parse.quote, given,
660 encoding="latin-1")
661 # quote_from_bytes should work the same
662 result = urllib.parse.quote_from_bytes(given)
663 self.assertEqual(expect, result,
664 "using quote_from_bytes(): %r != %r"
665 % (expect, result))
666
667 def test_quote_with_unicode(self):
668 # Characters in Latin-1 range, encoded by default in UTF-8
669 given = "\xa2\xd8ab\xff"
670 expect = "%C2%A2%C3%98ab%C3%BF"
671 result = urllib.parse.quote(given)
672 self.assertEqual(expect, result,
673 "using quote(): %r != %r" % (expect, result))
674 # Characters in Latin-1 range, encoded by with None (default)
675 result = urllib.parse.quote(given, encoding=None, errors=None)
676 self.assertEqual(expect, result,
677 "using quote(): %r != %r" % (expect, result))
678 # Characters in Latin-1 range, encoded with Latin-1
679 given = "\xa2\xd8ab\xff"
680 expect = "%A2%D8ab%FF"
681 result = urllib.parse.quote(given, encoding="latin-1")
682 self.assertEqual(expect, result,
683 "using quote(): %r != %r" % (expect, result))
684 # Characters in BMP, encoded by default in UTF-8
685 given = "\u6f22\u5b57" # "Kanji"
686 expect = "%E6%BC%A2%E5%AD%97"
687 result = urllib.parse.quote(given)
688 self.assertEqual(expect, result,
689 "using quote(): %r != %r" % (expect, result))
690 # Characters in BMP, encoded with Latin-1
691 given = "\u6f22\u5b57"
692 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
693 encoding="latin-1")
694 # Characters in BMP, encoded with Latin-1, with replace error handling
695 given = "\u6f22\u5b57"
696 expect = "%3F%3F" # "??"
697 result = urllib.parse.quote(given, encoding="latin-1",
698 errors="replace")
699 self.assertEqual(expect, result,
700 "using quote(): %r != %r" % (expect, result))
701 # Characters in BMP, Latin-1, with xmlcharref error handling
702 given = "\u6f22\u5b57"
703 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
704 result = urllib.parse.quote(given, encoding="latin-1",
705 errors="xmlcharrefreplace")
706 self.assertEqual(expect, result,
707 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000708
Georg Brandlfaf41492009-05-26 18:31:11 +0000709 def test_quote_plus_with_unicode(self):
710 # Encoding (latin-1) test for quote_plus
711 given = "\xa2\xd8 \xff"
712 expect = "%A2%D8+%FF"
713 result = urllib.parse.quote_plus(given, encoding="latin-1")
714 self.assertEqual(expect, result,
715 "using quote_plus(): %r != %r" % (expect, result))
716 # Errors test for quote_plus
717 given = "ab\u6f22\u5b57 cd"
718 expect = "ab%3F%3F+cd"
719 result = urllib.parse.quote_plus(given, encoding="latin-1",
720 errors="replace")
721 self.assertEqual(expect, result,
722 "using quote_plus(): %r != %r" % (expect, result))
723
Senthil Kumarand496c4c2010-07-30 19:34:36 +0000724
Brett Cannon74bfd702003-04-25 09:39:47 +0000725class UnquotingTests(unittest.TestCase):
726 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000727
Brett Cannon74bfd702003-04-25 09:39:47 +0000728 See the doc string for quoting_Tests for details on quoting and such.
729
730 """
731
732 def test_unquoting(self):
733 # Make sure unquoting of all ASCII values works
734 escape_list = []
735 for num in range(128):
736 given = hexescape(chr(num))
737 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000738 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000739 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000740 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000741 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000742 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000743 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000744 (expect, result))
745 escape_list.append(given)
746 escape_string = ''.join(escape_list)
747 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000748 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000749 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000750 "using unquote(): not all characters escaped: "
751 "%s" % result)
Georg Brandl604ef372010-07-31 08:20:02 +0000752 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
753 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
Florent Xicluna62829dc2010-08-14 20:51:58 +0000754 with support.check_warnings(('', BytesWarning), quiet=True):
755 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
Brett Cannon74bfd702003-04-25 09:39:47 +0000756
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000757 def test_unquoting_badpercent(self):
758 # Test unquoting on bad percent-escapes
759 given = '%xab'
760 expect = given
761 result = urllib.parse.unquote(given)
762 self.assertEqual(expect, result, "using unquote(): %r != %r"
763 % (expect, result))
764 given = '%x'
765 expect = given
766 result = urllib.parse.unquote(given)
767 self.assertEqual(expect, result, "using unquote(): %r != %r"
768 % (expect, result))
769 given = '%'
770 expect = given
771 result = urllib.parse.unquote(given)
772 self.assertEqual(expect, result, "using unquote(): %r != %r"
773 % (expect, result))
774 # unquote_to_bytes
775 given = '%xab'
776 expect = bytes(given, 'ascii')
777 result = urllib.parse.unquote_to_bytes(given)
778 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
779 % (expect, result))
780 given = '%x'
781 expect = bytes(given, 'ascii')
782 result = urllib.parse.unquote_to_bytes(given)
783 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
784 % (expect, result))
785 given = '%'
786 expect = bytes(given, 'ascii')
787 result = urllib.parse.unquote_to_bytes(given)
788 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
789 % (expect, result))
Georg Brandl604ef372010-07-31 08:20:02 +0000790 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)
791 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())
Senthil Kumaran79e17f62010-07-19 18:17:19 +0000792
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000793 def test_unquoting_mixed_case(self):
794 # Test unquoting on mixed-case hex digits in the percent-escapes
795 given = '%Ab%eA'
796 expect = b'\xab\xea'
797 result = urllib.parse.unquote_to_bytes(given)
798 self.assertEqual(expect, result,
799 "using unquote_to_bytes(): %r != %r"
800 % (expect, result))
801
Brett Cannon74bfd702003-04-25 09:39:47 +0000802 def test_unquoting_parts(self):
803 # Make sure unquoting works when have non-quoted characters
804 # interspersed
805 given = 'ab%sd' % hexescape('c')
806 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000807 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000808 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000809 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000810 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000811 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000812 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000813
Brett Cannon74bfd702003-04-25 09:39:47 +0000814 def test_unquoting_plus(self):
815 # Test difference between unquote() and unquote_plus()
816 given = "are+there+spaces..."
817 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000818 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000819 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000820 "using unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000821 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000822 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000823 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000824 "using unquote_plus(): %r != %r" % (expect, result))
825
826 def test_unquote_to_bytes(self):
827 given = 'br%C3%BCckner_sapporo_20050930.doc'
828 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
829 result = urllib.parse.unquote_to_bytes(given)
830 self.assertEqual(expect, result,
831 "using unquote_to_bytes(): %r != %r"
832 % (expect, result))
833 # Test on a string with unescaped non-ASCII characters
834 # (Technically an invalid URI; expect those characters to be UTF-8
835 # encoded).
836 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
837 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
838 self.assertEqual(expect, result,
839 "using unquote_to_bytes(): %r != %r"
840 % (expect, result))
841 # Test with a bytes as input
842 given = b'%A2%D8ab%FF'
843 expect = b'\xa2\xd8ab\xff'
844 result = urllib.parse.unquote_to_bytes(given)
845 self.assertEqual(expect, result,
846 "using unquote_to_bytes(): %r != %r"
847 % (expect, result))
848 # Test with a bytes as input, with unescaped non-ASCII bytes
849 # (Technically an invalid URI; expect those bytes to be preserved)
850 given = b'%A2\xd8ab%FF'
851 expect = b'\xa2\xd8ab\xff'
852 result = urllib.parse.unquote_to_bytes(given)
853 self.assertEqual(expect, result,
854 "using unquote_to_bytes(): %r != %r"
855 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000856
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000857 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000858 # Characters in the Latin-1 range, encoded with UTF-8
859 given = 'br%C3%BCckner_sapporo_20050930.doc'
860 expect = 'br\u00fcckner_sapporo_20050930.doc'
861 result = urllib.parse.unquote(given)
862 self.assertEqual(expect, result,
863 "using unquote(): %r != %r" % (expect, result))
864 # Characters in the Latin-1 range, encoded with None (default)
865 result = urllib.parse.unquote(given, encoding=None, errors=None)
866 self.assertEqual(expect, result,
867 "using unquote(): %r != %r" % (expect, result))
868
869 # Characters in the Latin-1 range, encoded with Latin-1
870 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
871 encoding="latin-1")
872 expect = 'br\u00fcckner_sapporo_20050930.doc'
873 self.assertEqual(expect, result,
874 "using unquote(): %r != %r" % (expect, result))
875
876 # Characters in BMP, encoded with UTF-8
877 given = "%E6%BC%A2%E5%AD%97"
878 expect = "\u6f22\u5b57" # "Kanji"
879 result = urllib.parse.unquote(given)
880 self.assertEqual(expect, result,
881 "using unquote(): %r != %r" % (expect, result))
882
883 # Decode with UTF-8, invalid sequence
884 given = "%F3%B1"
885 expect = "\ufffd" # Replacement character
886 result = urllib.parse.unquote(given)
887 self.assertEqual(expect, result,
888 "using unquote(): %r != %r" % (expect, result))
889
890 # Decode with UTF-8, invalid sequence, replace errors
891 result = urllib.parse.unquote(given, errors="replace")
892 self.assertEqual(expect, result,
893 "using unquote(): %r != %r" % (expect, result))
894
895 # Decode with UTF-8, invalid sequence, ignoring errors
896 given = "%F3%B1"
897 expect = ""
898 result = urllib.parse.unquote(given, errors="ignore")
899 self.assertEqual(expect, result,
900 "using unquote(): %r != %r" % (expect, result))
901
902 # A mix of non-ASCII and percent-encoded characters, UTF-8
903 result = urllib.parse.unquote("\u6f22%C3%BC")
904 expect = '\u6f22\u00fc'
905 self.assertEqual(expect, result,
906 "using unquote(): %r != %r" % (expect, result))
907
908 # A mix of non-ASCII and percent-encoded characters, Latin-1
909 # (Note, the string contains non-Latin-1-representable characters)
910 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
911 expect = '\u6f22\u00fc'
912 self.assertEqual(expect, result,
913 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000914
Brett Cannon74bfd702003-04-25 09:39:47 +0000915class urlencode_Tests(unittest.TestCase):
916 """Tests for urlencode()"""
917
918 def help_inputtype(self, given, test_type):
919 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000920
Brett Cannon74bfd702003-04-25 09:39:47 +0000921 'given' must lead to only the pairs:
922 * 1st, 1
923 * 2nd, 2
924 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000925
Brett Cannon74bfd702003-04-25 09:39:47 +0000926 Test cannot assume anything about order. Docs make no guarantee and
927 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000928
Brett Cannon74bfd702003-04-25 09:39:47 +0000929 """
930 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000931 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000932 for expected in expect_somewhere:
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000933 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000934 "testing %s: %s not found in %s" %
935 (test_type, expected, result))
936 self.assertEqual(result.count('&'), 2,
937 "testing %s: expected 2 '&'s; got %s" %
938 (test_type, result.count('&')))
939 amp_location = result.index('&')
940 on_amp_left = result[amp_location - 1]
941 on_amp_right = result[amp_location + 1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000942 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000943 "testing %s: '&' not located in proper place in %s" %
944 (test_type, result))
945 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
946 "testing %s: "
947 "unexpected number of characters: %s != %s" %
948 (test_type, len(result), (5 * 3) + 2))
949
950 def test_using_mapping(self):
951 # Test passing in a mapping object as an argument.
952 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
953 "using dict as input type")
954
955 def test_using_sequence(self):
956 # Test passing in a sequence of two-item sequences as an argument.
957 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
958 "using sequence of two-item tuples as input")
959
960 def test_quoting(self):
961 # Make sure keys and values are quoted using quote_plus()
962 given = {"&":"="}
963 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000964 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000965 self.assertEqual(expect, result)
966 given = {"key name":"A bunch of pluses"}
967 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000968 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000969 self.assertEqual(expect, result)
970
971 def test_doseq(self):
972 # Test that passing True for 'doseq' parameter works correctly
973 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000974 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
975 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000976 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000977 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000978 for value in given["sequence"]:
979 expect = "sequence=%s" % value
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000980 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000981 self.assertEqual(result.count('&'), 2,
982 "Expected 2 '&'s, got %s" % result.count('&'))
983
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000984 def test_empty_sequence(self):
985 self.assertEqual("", urllib.parse.urlencode({}))
986 self.assertEqual("", urllib.parse.urlencode([]))
987
988 def test_nonstring_values(self):
989 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
990 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
991
992 def test_nonstring_seq_values(self):
993 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
994 self.assertEqual("a=None&a=a",
995 urllib.parse.urlencode({"a": [None, "a"]}, True))
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100996 data = collections.OrderedDict([("a", 1), ("b", 1)])
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000997 self.assertEqual("a=a&a=b",
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100998 urllib.parse.urlencode({"a": data}, True))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000999
Senthil Kumarandf022da2010-07-03 17:48:22 +00001000 def test_urlencode_encoding(self):
1001 # ASCII encoding. Expect %3F with errors="replace'
1002 given = (('\u00a0', '\u00c1'),)
1003 expect = '%3F=%3F'
1004 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
1005 self.assertEqual(expect, result)
1006
1007 # Default is UTF-8 encoding.
1008 given = (('\u00a0', '\u00c1'),)
1009 expect = '%C2%A0=%C3%81'
1010 result = urllib.parse.urlencode(given)
1011 self.assertEqual(expect, result)
1012
1013 # Latin-1 encoding.
1014 given = (('\u00a0', '\u00c1'),)
1015 expect = '%A0=%C1'
1016 result = urllib.parse.urlencode(given, encoding="latin-1")
1017 self.assertEqual(expect, result)
1018
1019 def test_urlencode_encoding_doseq(self):
1020 # ASCII Encoding. Expect %3F with errors="replace'
1021 given = (('\u00a0', '\u00c1'),)
1022 expect = '%3F=%3F'
1023 result = urllib.parse.urlencode(given, doseq=True,
1024 encoding="ASCII", errors="replace")
1025 self.assertEqual(expect, result)
1026
1027 # ASCII Encoding. On a sequence of values.
1028 given = (("\u00a0", (1, "\u00c1")),)
1029 expect = '%3F=1&%3F=%3F'
1030 result = urllib.parse.urlencode(given, True,
1031 encoding="ASCII", errors="replace")
1032 self.assertEqual(expect, result)
1033
1034 # Utf-8
1035 given = (("\u00a0", "\u00c1"),)
1036 expect = '%C2%A0=%C3%81'
1037 result = urllib.parse.urlencode(given, True)
1038 self.assertEqual(expect, result)
1039
1040 given = (("\u00a0", (42, "\u00c1")),)
1041 expect = '%C2%A0=42&%C2%A0=%C3%81'
1042 result = urllib.parse.urlencode(given, True)
1043 self.assertEqual(expect, result)
1044
1045 # latin-1
1046 given = (("\u00a0", "\u00c1"),)
1047 expect = '%A0=%C1'
1048 result = urllib.parse.urlencode(given, True, encoding="latin-1")
1049 self.assertEqual(expect, result)
1050
1051 given = (("\u00a0", (42, "\u00c1")),)
1052 expect = '%A0=42&%A0=%C1'
1053 result = urllib.parse.urlencode(given, True, encoding="latin-1")
1054 self.assertEqual(expect, result)
1055
1056 def test_urlencode_bytes(self):
1057 given = ((b'\xa0\x24', b'\xc1\x24'),)
1058 expect = '%A0%24=%C1%24'
1059 result = urllib.parse.urlencode(given)
1060 self.assertEqual(expect, result)
1061 result = urllib.parse.urlencode(given, True)
1062 self.assertEqual(expect, result)
1063
1064 # Sequence of values
1065 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
1066 expect = '%A0%24=42&%A0%24=%C1%24'
1067 result = urllib.parse.urlencode(given, True)
1068 self.assertEqual(expect, result)
1069
1070 def test_urlencode_encoding_safe_parameter(self):
1071
1072 # Send '$' (\x24) as safe character
1073 # Default utf-8 encoding
1074
1075 given = ((b'\xa0\x24', b'\xc1\x24'),)
1076 result = urllib.parse.urlencode(given, safe=":$")
1077 expect = '%A0$=%C1$'
1078 self.assertEqual(expect, result)
1079
1080 given = ((b'\xa0\x24', b'\xc1\x24'),)
1081 result = urllib.parse.urlencode(given, doseq=True, safe=":$")
1082 expect = '%A0$=%C1$'
1083 self.assertEqual(expect, result)
1084
1085 # Safe parameter in sequence
1086 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
1087 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
1088 result = urllib.parse.urlencode(given, True, safe=":$")
1089 self.assertEqual(expect, result)
1090
1091 # Test all above in latin-1 encoding
1092
1093 given = ((b'\xa0\x24', b'\xc1\x24'),)
1094 result = urllib.parse.urlencode(given, safe=":$",
1095 encoding="latin-1")
1096 expect = '%A0$=%C1$'
1097 self.assertEqual(expect, result)
1098
1099 given = ((b'\xa0\x24', b'\xc1\x24'),)
1100 expect = '%A0$=%C1$'
1101 result = urllib.parse.urlencode(given, doseq=True, safe=":$",
1102 encoding="latin-1")
1103
1104 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
1105 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
1106 result = urllib.parse.urlencode(given, True, safe=":$",
1107 encoding="latin-1")
1108 self.assertEqual(expect, result)
1109
Brett Cannon74bfd702003-04-25 09:39:47 +00001110class Pathname_Tests(unittest.TestCase):
1111 """Test pathname2url() and url2pathname()"""
1112
1113 def test_basic(self):
1114 # Make sure simple tests pass
1115 expected_path = os.path.join("parts", "of", "a", "path")
1116 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001117 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +00001118 self.assertEqual(expected_url, result,
1119 "pathname2url() failed; %s != %s" %
1120 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001121 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +00001122 self.assertEqual(expected_path, result,
1123 "url2pathame() failed; %s != %s" %
1124 (result, expected_path))
1125
1126 def test_quoting(self):
1127 # Test automatic quoting and unquoting works for pathnam2url() and
1128 # url2pathname() respectively
1129 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001130 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
1131 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +00001132 self.assertEqual(expect, result,
1133 "pathname2url() failed; %s != %s" %
1134 (expect, result))
1135 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001136 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +00001137 self.assertEqual(expect, result,
1138 "url2pathname() failed; %s != %s" %
1139 (expect, result))
1140 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001141 expect = "%s/using_quote" % urllib.parse.quote("make sure")
1142 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +00001143 self.assertEqual(expect, result,
1144 "pathname2url() failed; %s != %s" %
1145 (expect, result))
1146 given = "make+sure/using_unquote"
1147 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001148 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +00001149 self.assertEqual(expect, result,
1150 "url2pathname() failed; %s != %s" %
1151 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +00001152
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +08001153 @unittest.skipUnless(sys.platform == 'win32',
1154 'test specific to the urllib.url2path function.')
1155 def test_ntpath(self):
1156 given = ('/C:/', '///C:/', '/C|//')
1157 expect = 'C:\\'
1158 for url in given:
1159 result = urllib.request.url2pathname(url)
1160 self.assertEqual(expect, result,
1161 'urllib.request..url2pathname() failed; %s != %s' %
1162 (expect, result))
1163 given = '///C|/path'
1164 expect = 'C:\\path'
1165 result = urllib.request.url2pathname(given)
1166 self.assertEqual(expect, result,
1167 'urllib.request.url2pathname() failed; %s != %s' %
1168 (expect, result))
1169
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001170class Utility_Tests(unittest.TestCase):
1171 """Testcase to test the various utility functions in the urllib."""
1172
1173 def test_splitpasswd(self):
1174 """Some of password examples are not sensible, but it is added to
1175 confirming to RFC2617 and addressing issue4675.
1176 """
1177 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
1178 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
1179 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
1180 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
1181 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
1182 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
1183 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
Senthil Kumaranc5c5a142012-01-14 19:09:04 +08001184 self.assertEqual(('user', 'a b'),urllib.parse.splitpasswd('user:a b'))
1185 self.assertEqual(('user 2', 'ab'),urllib.parse.splitpasswd('user 2:ab'))
1186 self.assertEqual(('user+1', 'a+b'),urllib.parse.splitpasswd('user+1:a+b'))
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001187
Senthil Kumaran1b7da512011-10-06 00:32:02 +08001188 def test_thishost(self):
1189 """Test the urllib.request.thishost utility function returns a tuple"""
1190 self.assertIsInstance(urllib.request.thishost(), tuple)
1191
Senthil Kumaran690ce9b2009-05-05 18:41:13 +00001192
1193class URLopener_Tests(unittest.TestCase):
1194 """Testcase to test the open method of URLopener class."""
1195
1196 def test_quoted_open(self):
1197 class DummyURLopener(urllib.request.URLopener):
1198 def open_spam(self, url):
1199 return url
1200
1201 self.assertEqual(DummyURLopener().open(
1202 'spam://example/ /'),'//example/%20/')
1203
Senthil Kumaran734f0592010-02-20 22:19:04 +00001204 # test the safe characters are not quoted by urlopen
1205 self.assertEqual(DummyURLopener().open(
1206 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
1207 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
1208
Guido van Rossume7ba4952007-06-06 23:52:48 +00001209# Just commented them out.
1210# Can't really tell why keep failing in windows and sparc.
Ezio Melotti13925002011-03-16 11:05:33 +02001211# Everywhere else they work ok, but on those machines, sometimes
Guido van Rossume7ba4952007-06-06 23:52:48 +00001212# fail in one of the tests, sometimes in other. I have a linux, and
1213# the tests go ok.
1214# If anybody has one of the problematic enviroments, please help!
1215# . Facundo
1216#
1217# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001218# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +00001219# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1220# serv.settimeout(3)
1221# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1222# serv.bind(("", 9093))
1223# serv.listen(5)
1224# try:
1225# conn, addr = serv.accept()
1226# conn.send("1 Hola mundo\n")
1227# cantdata = 0
1228# while cantdata < 13:
1229# data = conn.recv(13-cantdata)
1230# cantdata += len(data)
1231# time.sleep(.3)
1232# conn.send("2 No more lines\n")
1233# conn.close()
1234# except socket.timeout:
1235# pass
1236# finally:
1237# serv.close()
1238# evt.set()
1239#
1240# class FTPWrapperTests(unittest.TestCase):
1241#
1242# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001243# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +00001244# ftplib.FTP.port = 9093
1245# self.evt = threading.Event()
1246# threading.Thread(target=server, args=(self.evt,)).start()
1247# time.sleep(.1)
1248#
1249# def tearDown(self):
1250# self.evt.wait()
1251#
1252# def testBasic(self):
1253# # connects
1254# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +00001255# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001256#
1257# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001258# # global default timeout is ignored
1259# import socket
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001260# self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001261# socket.setdefaulttimeout(30)
1262# try:
1263# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1264# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +00001265# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001266# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001267# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001268#
Georg Brandlf78e02b2008-06-10 17:40:04 +00001269# def testTimeoutDefault(self):
1270# # global default timeout is used
1271# import socket
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001272# self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001273# socket.setdefaulttimeout(30)
1274# try:
1275# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1276# finally:
1277# socket.setdefaulttimeout(None)
1278# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1279# ftp.close()
1280#
1281# def testTimeoutValue(self):
1282# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1283# timeout=30)
1284# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1285# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001286
Senthil Kumarande49d642011-10-16 23:54:44 +08001287class RequestTests(unittest.TestCase):
1288 """Unit tests for urllib.request.Request."""
1289
1290 def test_default_values(self):
1291 Request = urllib.request.Request
1292 request = Request("http://www.python.org")
1293 self.assertEqual(request.get_method(), 'GET')
1294 request = Request("http://www.python.org", {})
1295 self.assertEqual(request.get_method(), 'POST')
1296
1297 def test_with_method_arg(self):
1298 Request = urllib.request.Request
1299 request = Request("http://www.python.org", method='HEAD')
1300 self.assertEqual(request.method, 'HEAD')
1301 self.assertEqual(request.get_method(), 'HEAD')
1302 request = Request("http://www.python.org", {}, method='HEAD')
1303 self.assertEqual(request.method, 'HEAD')
1304 self.assertEqual(request.get_method(), 'HEAD')
1305 request = Request("http://www.python.org", method='GET')
1306 self.assertEqual(request.get_method(), 'GET')
1307 request.method = 'HEAD'
1308 self.assertEqual(request.get_method(), 'HEAD')
Skip Montanaro080c9972001-01-28 21:12:22 +00001309
1310
Brett Cannon74bfd702003-04-25 09:39:47 +00001311def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001312 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +00001313 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001314 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001315 urlretrieve_FileTests,
Senthil Kumarance260142011-11-01 01:35:17 +08001316 urlretrieve_HttpTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00001317 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001318 QuotingTests,
1319 UnquotingTests,
1320 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001321 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001322 Utility_Tests,
Senthil Kumaran690ce9b2009-05-05 18:41:13 +00001323 URLopener_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001324 #FTPWrapperTests,
Senthil Kumarande49d642011-10-16 23:54:44 +08001325 RequestTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001326 )
Brett Cannon74bfd702003-04-25 09:39:47 +00001327
1328
1329
1330if __name__ == '__main__':
1331 test_main()