blob: 4d389d72e4ebf8bb8a7b07b2a1463657f10fe665 [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
Georg Brandl24420152008-05-26 16:32:26 +00005import http.client
Barry Warsaw820c1202008-06-12 04:06:45 +00006import email.message
Jeremy Hylton66dc8c52007-08-04 03:42:26 +00007import io
Brett Cannon74bfd702003-04-25 09:39:47 +00008import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Brett Cannon74bfd702003-04-25 09:39:47 +000010import os
Georg Brandl5a650a22005-08-26 08:51:34 +000011import tempfile
Florent Xicluna99e472e2010-08-14 23:12:27 +000012import warnings
Jeremy Hylton6102e292000-08-31 15:48:10 +000013
Brett Cannon74bfd702003-04-25 09:39:47 +000014def hexescape(char):
15 """Escape char as RFC 2396 specifies"""
16 hex_repr = hex(ord(char))[2:].upper()
17 if len(hex_repr) == 1:
18 hex_repr = "0%s" % hex_repr
19 return "%" + hex_repr
Jeremy Hylton6102e292000-08-31 15:48:10 +000020
Jeremy Hylton1afc1692008-06-18 20:49:58 +000021# Shortcut for testing FancyURLopener
22_urlopener = None
23def urlopen(url, data=None, proxies=None):
24 """urlopen(url [, data]) -> open file-like object"""
25 global _urlopener
26 if proxies is not None:
27 opener = urllib.request.FancyURLopener(proxies=proxies)
28 elif not _urlopener:
29 opener = urllib.request.FancyURLopener()
30 _urlopener = opener
31 else:
32 opener = _urlopener
33 if data is None:
34 return opener.open(url)
35 else:
36 return opener.open(url, data)
37
Brett Cannon74bfd702003-04-25 09:39:47 +000038class urlopen_FileTests(unittest.TestCase):
39 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000040
Brett Cannon74bfd702003-04-25 09:39:47 +000041 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000042 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000043
Brett Cannon74bfd702003-04-25 09:39:47 +000044 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000045
Brett Cannon74bfd702003-04-25 09:39:47 +000046 def setUp(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000047 # Create a temp file to use for testing
48 self.text = bytes("test_urllib: %s\n" % self.__class__.__name__,
49 "ascii")
50 f = open(support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000051 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000052 f.write(self.text)
Brett Cannon74bfd702003-04-25 09:39:47 +000053 finally:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000054 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000055 self.pathname = support.TESTFN
Jeremy Hylton1afc1692008-06-18 20:49:58 +000056 self.returned_obj = urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000057
Brett Cannon74bfd702003-04-25 09:39:47 +000058 def tearDown(self):
59 """Shut down the open object"""
60 self.returned_obj.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000061 os.remove(support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000062
Brett Cannon74bfd702003-04-25 09:39:47 +000063 def test_interface(self):
64 # Make sure object returned by urlopen() has the specified methods
65 for attr in ("read", "readline", "readlines", "fileno",
Christian Heimes9bd667a2008-01-20 15:14:11 +000066 "close", "info", "geturl", "getcode", "__iter__"):
Georg Brandlab91fde2009-08-13 08:51:18 +000067 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +000068 "object returned by urlopen() lacks %s attribute" %
69 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000070
Brett Cannon74bfd702003-04-25 09:39:47 +000071 def test_read(self):
72 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +000073
Brett Cannon74bfd702003-04-25 09:39:47 +000074 def test_readline(self):
75 self.assertEqual(self.text, self.returned_obj.readline())
Guido van Rossuma0982942007-07-10 08:30:03 +000076 self.assertEqual(b'', self.returned_obj.readline(),
Brett Cannon74bfd702003-04-25 09:39:47 +000077 "calling readline() after exhausting the file did not"
78 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +000079
Brett Cannon74bfd702003-04-25 09:39:47 +000080 def test_readlines(self):
81 lines_list = self.returned_obj.readlines()
82 self.assertEqual(len(lines_list), 1,
83 "readlines() returned the wrong number of lines")
84 self.assertEqual(lines_list[0], self.text,
85 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +000086
Brett Cannon74bfd702003-04-25 09:39:47 +000087 def test_fileno(self):
88 file_num = self.returned_obj.fileno()
Georg Brandlab91fde2009-08-13 08:51:18 +000089 self.assertTrue(isinstance(file_num, int),
Brett Cannon74bfd702003-04-25 09:39:47 +000090 "fileno() did not return an int")
91 self.assertEqual(os.read(file_num, len(self.text)), self.text,
92 "Reading on the file descriptor returned by fileno() "
93 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +000094
Brett Cannon74bfd702003-04-25 09:39:47 +000095 def test_close(self):
96 # Test close() by calling it hear and then having it be called again
97 # by the tearDown() method for the test
98 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +000099
Brett Cannon74bfd702003-04-25 09:39:47 +0000100 def test_info(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000101 self.assertTrue(isinstance(self.returned_obj.info(), email.message.Message))
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000102
Brett Cannon74bfd702003-04-25 09:39:47 +0000103 def test_geturl(self):
104 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +0000105
Christian Heimes9bd667a2008-01-20 15:14:11 +0000106 def test_getcode(self):
Florent Xiclunab4efb3d2010-08-14 18:24:40 +0000107 self.assertIsNone(self.returned_obj.getcode())
Christian Heimes9bd667a2008-01-20 15:14:11 +0000108
Brett Cannon74bfd702003-04-25 09:39:47 +0000109 def test_iter(self):
110 # Test iterator
111 # Don't need to count number of iterations since test would fail the
112 # instant it returned anything beyond the first line from the
113 # comparison
114 for line in self.returned_obj.__iter__():
115 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +0000116
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000117class ProxyTests(unittest.TestCase):
118
119 def setUp(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000120 # Records changes to env vars
121 self.env = support.EnvironmentVarGuard()
Benjamin Petersonffeda292010-01-09 18:48:46 +0000122 # Delete all proxy related env vars
123 for k in os.environ.keys():
124 if 'proxy' in k.lower():
125 self.env.unset(k)
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000126
127 def tearDown(self):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000128 # Restore all proxy related env vars
Walter Dörwaldb525e182009-04-26 21:39:21 +0000129 self.env.__exit__()
130 del self.env
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000131
132 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000133 self.env.set('NO_PROXY', 'localhost')
134 proxies = urllib.request.getproxies_environment()
135 # getproxies_environment use lowered case truncated (no '_proxy') keys
Florent Xiclunab4efb3d2010-08-14 18:24:40 +0000136 self.assertEqual('localhost', proxies['no'])
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000137
138
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000139class urlopen_HttpTests(unittest.TestCase):
140 """Test urlopen() opening a fake http connection."""
141
142 def fakehttp(self, fakedata):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000143 class FakeSocket(io.BytesIO):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000144 def sendall(self, str): pass
Nick Coghlan598c3a82009-02-08 04:01:00 +0000145 def makefile(self, *args, **kwds):
146 return self
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000147 def read(self, amt=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000148 if self.closed: return b""
149 return io.BytesIO.read(self, amt)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000150 def readline(self, length=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000151 if self.closed: return b""
152 return io.BytesIO.readline(self, length)
Georg Brandl24420152008-05-26 16:32:26 +0000153 class FakeHTTPConnection(http.client.HTTPConnection):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000154 def connect(self):
155 self.sock = FakeSocket(fakedata)
Georg Brandl24420152008-05-26 16:32:26 +0000156 self._connection_class = http.client.HTTPConnection
157 http.client.HTTPConnection = FakeHTTPConnection
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000158
159 def unfakehttp(self):
Georg Brandl24420152008-05-26 16:32:26 +0000160 http.client.HTTPConnection = self._connection_class
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000161
162 def test_read(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000163 self.fakehttp(b"Hello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000164 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000165 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000166 self.assertEqual(fp.readline(), b"Hello!")
167 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000168 self.assertEqual(fp.geturl(), 'http://python.org/')
169 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000170 finally:
171 self.unfakehttp()
172
Christian Heimes57dddfb2008-01-02 18:30:52 +0000173 def test_read_bogus(self):
174 # urlopen() should raise IOError for many error codes.
175 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
176Date: Wed, 02 Jan 2008 03:03:54 GMT
177Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
178Connection: close
179Content-Type: text/html; charset=iso-8859-1
180''')
181 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000182 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000183 finally:
184 self.unfakehttp()
185
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000187 # urlopen() raises IOError if the underlying socket does not send any
188 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000189 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000190 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000191 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000192 finally:
193 self.unfakehttp()
194
Senthil Kumaranafef78f2010-08-01 17:55:50 +0000195 def test_userpass_inurl(self):
196 self.fakehttp(b"Hello!")
197 try:
198 fp = urlopen("http://user:pass@python.org/")
199 self.assertEqual(fp.readline(), b"Hello!")
200 self.assertEqual(fp.readline(), b"")
201 self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
202 self.assertEqual(fp.getcode(), 200)
203 finally:
204 self.unfakehttp()
205
Brett Cannon19691362003-04-29 05:08:06 +0000206class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000207 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000208
Brett Cannon19691362003-04-29 05:08:06 +0000209 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000210 # Create a list of temporary files. Each item in the list is a file
211 # name (absolute path or relative to the current working directory).
212 # All files in this list will be deleted in the tearDown method. Note,
213 # this only helps to makes sure temporary files get deleted, but it
214 # does nothing about trying to close files that may still be open. It
215 # is the responsibility of the developer to properly close files even
216 # when exceptional conditions occur.
217 self.tempFiles = []
218
Brett Cannon19691362003-04-29 05:08:06 +0000219 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000220 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000221 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000222 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000223 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000224 FILE.write(self.text)
225 FILE.close()
226 finally:
227 try: FILE.close()
228 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000229
230 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000231 # Delete the temporary files.
232 for each in self.tempFiles:
233 try: os.remove(each)
234 except: pass
235
236 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000237 return "file://%s" % urllib.request.pathname2url(
238 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000239
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000240 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000241 """Creates a new temporary file containing the specified data,
242 registers the file for deletion during the test fixture tear down, and
243 returns the absolute path of the file."""
244
245 newFd, newFilePath = tempfile.mkstemp()
246 try:
247 self.registerFileForCleanUp(newFilePath)
248 newFile = os.fdopen(newFd, "wb")
249 newFile.write(data)
250 newFile.close()
251 finally:
252 try: newFile.close()
253 except: pass
254 return newFilePath
255
256 def registerFileForCleanUp(self, fileName):
257 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000258
259 def test_basic(self):
260 # Make sure that a local file just gets its own location returned and
261 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000262 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000263 self.assertEqual(result[0], support.TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000264 self.assertTrue(isinstance(result[1], email.message.Message),
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000265 "did not get a email.message.Message instance "
266 "as second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000267
268 def test_copy(self):
269 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000270 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000271 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000272 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000273 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000274 self.assertEqual(second_temp, result[0])
Georg Brandlab91fde2009-08-13 08:51:18 +0000275 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000276 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000277 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000278 try:
279 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000280 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000281 finally:
282 try: FILE.close()
283 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000284 self.assertEqual(self.text, text)
285
286 def test_reporthook(self):
287 # Make sure that the reporthook works.
288 def hooktester(count, block_size, total_size, count_holder=[0]):
Georg Brandlab91fde2009-08-13 08:51:18 +0000289 self.assertTrue(isinstance(count, int))
290 self.assertTrue(isinstance(block_size, int))
291 self.assertTrue(isinstance(total_size, int))
Brett Cannon19691362003-04-29 05:08:06 +0000292 self.assertEqual(count, count_holder[0])
293 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000294 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000295 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000296 urllib.request.urlretrieve(
297 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000298 second_temp, hooktester)
299
300 def test_reporthook_0_bytes(self):
301 # Test on zero length file. Should call reporthook only 1 time.
302 report = []
303 def hooktester(count, block_size, total_size, _report=report):
304 _report.append((count, block_size, total_size))
305 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000306 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000307 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000308 self.assertEqual(len(report), 1)
309 self.assertEqual(report[0][2], 0)
310
311 def test_reporthook_5_bytes(self):
312 # Test on 5 byte file. Should call reporthook only 2 times (once when
313 # the "network connection" is established and once when the block is
314 # read). Since the block size is 8192 bytes, only one block read is
315 # required to read the entire file.
316 report = []
317 def hooktester(count, block_size, total_size, _report=report):
318 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000319 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000320 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000321 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000322 self.assertEqual(len(report), 2)
323 self.assertEqual(report[0][1], 8192)
324 self.assertEqual(report[0][2], 5)
325
326 def test_reporthook_8193_bytes(self):
327 # Test on 8193 byte file. Should call reporthook only 3 times (once
328 # when the "network connection" is established, once for the next 8192
329 # bytes, and once for the last byte).
330 report = []
331 def hooktester(count, block_size, total_size, _report=report):
332 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000333 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000334 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000335 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000336 self.assertEqual(len(report), 3)
337 self.assertEqual(report[0][1], 8192)
338 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000339
Brett Cannon74bfd702003-04-25 09:39:47 +0000340class QuotingTests(unittest.TestCase):
341 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000342
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000343 According to RFC 2396 (Uniform Resource Identifiers), to escape a
344 character you write it as '%' + <2 character US-ASCII hex value>.
345 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
346 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000347
348 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000349
Brett Cannon74bfd702003-04-25 09:39:47 +0000350 Reserved characters : ";/?:@&=+$,"
351 Have special meaning in URIs and must be escaped if not being used for
352 their special meaning
353 Data characters : letters, digits, and "-_.!~*'()"
354 Unreserved and do not need to be escaped; can be, though, if desired
355 Control characters : 0x00 - 0x1F, 0x7F
356 Have no use in URIs so must be escaped
357 space : 0x20
358 Must be escaped
359 Delimiters : '<>#%"'
360 Must be escaped
361 Unwise : "{}|\^[]`"
362 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000363
Brett Cannon74bfd702003-04-25 09:39:47 +0000364 """
365
366 def test_never_quote(self):
367 # Make sure quote() does not quote letters, digits, and "_,.-"
368 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
369 "abcdefghijklmnopqrstuvwxyz",
370 "0123456789",
371 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000372 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000373 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000374 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000375 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000376 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000377 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000378
379 def test_default_safe(self):
380 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000381 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000382
383 def test_safe(self):
384 # Test setting 'safe' parameter does what it should do
385 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000386 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000387 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000388 "using quote(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000389 result = urllib.parse.quote_plus(quote_by_default,
390 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000391 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000392 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000393 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000394 # Safe expressed as bytes rather than str
395 result = urllib.parse.quote(quote_by_default, safe=b"<>")
396 self.assertEqual(quote_by_default, result,
397 "using quote(): %r != %r" % (quote_by_default, result))
398 # "Safe" non-ASCII characters should have no effect
399 # (Since URIs are not allowed to have non-ASCII characters)
400 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
401 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
402 self.assertEqual(expect, result,
403 "using quote(): %r != %r" %
404 (expect, result))
405 # Same as above, but using a bytes rather than str
406 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
407 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
408 self.assertEqual(expect, result,
409 "using quote(): %r != %r" %
410 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000411
412 def test_default_quoting(self):
413 # Make sure all characters that should be quoted are by default sans
414 # space (separate test for that).
415 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
416 should_quote.append('<>#%"{}|\^[]`')
417 should_quote.append(chr(127)) # For 0x7F
418 should_quote = ''.join(should_quote)
419 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000420 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000421 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000422 "using quote(): "
423 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000424 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000425 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000426 self.assertEqual(hexescape(char), result,
427 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000428 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000429 (char, hexescape(char), result))
430 del should_quote
431 partial_quote = "ab[]cd"
432 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000433 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000434 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000435 "using quote(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000436 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000437 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000438
439 def test_quoting_space(self):
440 # Make sure quote() and quote_plus() handle spaces as specified in
441 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000442 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000443 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000444 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000445 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000446 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000447 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000448 given = "a b cd e f"
449 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000450 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000451 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000452 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000453 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000454 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000455 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000456 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000457
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000458 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000459 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000460 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000461 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000462 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000463 # Test with bytes
464 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
465 'alpha%2Bbeta+gamma')
466 # Test with safe bytes
467 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
468 'alpha+beta+gamma')
469
470 def test_quote_bytes(self):
471 # Bytes should quote directly to percent-encoded values
472 given = b"\xa2\xd8ab\xff"
473 expect = "%A2%D8ab%FF"
474 result = urllib.parse.quote(given)
475 self.assertEqual(expect, result,
476 "using quote(): %r != %r" % (expect, result))
477 # Encoding argument should raise type error on bytes input
478 self.assertRaises(TypeError, urllib.parse.quote, given,
479 encoding="latin-1")
480 # quote_from_bytes should work the same
481 result = urllib.parse.quote_from_bytes(given)
482 self.assertEqual(expect, result,
483 "using quote_from_bytes(): %r != %r"
484 % (expect, result))
485
486 def test_quote_with_unicode(self):
487 # Characters in Latin-1 range, encoded by default in UTF-8
488 given = "\xa2\xd8ab\xff"
489 expect = "%C2%A2%C3%98ab%C3%BF"
490 result = urllib.parse.quote(given)
491 self.assertEqual(expect, result,
492 "using quote(): %r != %r" % (expect, result))
493 # Characters in Latin-1 range, encoded by with None (default)
494 result = urllib.parse.quote(given, encoding=None, errors=None)
495 self.assertEqual(expect, result,
496 "using quote(): %r != %r" % (expect, result))
497 # Characters in Latin-1 range, encoded with Latin-1
498 given = "\xa2\xd8ab\xff"
499 expect = "%A2%D8ab%FF"
500 result = urllib.parse.quote(given, encoding="latin-1")
501 self.assertEqual(expect, result,
502 "using quote(): %r != %r" % (expect, result))
503 # Characters in BMP, encoded by default in UTF-8
504 given = "\u6f22\u5b57" # "Kanji"
505 expect = "%E6%BC%A2%E5%AD%97"
506 result = urllib.parse.quote(given)
507 self.assertEqual(expect, result,
508 "using quote(): %r != %r" % (expect, result))
509 # Characters in BMP, encoded with Latin-1
510 given = "\u6f22\u5b57"
511 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
512 encoding="latin-1")
513 # Characters in BMP, encoded with Latin-1, with replace error handling
514 given = "\u6f22\u5b57"
515 expect = "%3F%3F" # "??"
516 result = urllib.parse.quote(given, encoding="latin-1",
517 errors="replace")
518 self.assertEqual(expect, result,
519 "using quote(): %r != %r" % (expect, result))
520 # Characters in BMP, Latin-1, with xmlcharref error handling
521 given = "\u6f22\u5b57"
522 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
523 result = urllib.parse.quote(given, encoding="latin-1",
524 errors="xmlcharrefreplace")
525 self.assertEqual(expect, result,
526 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000527
Georg Brandlfaf41492009-05-26 18:31:11 +0000528 def test_quote_plus_with_unicode(self):
529 # Encoding (latin-1) test for quote_plus
530 given = "\xa2\xd8 \xff"
531 expect = "%A2%D8+%FF"
532 result = urllib.parse.quote_plus(given, encoding="latin-1")
533 self.assertEqual(expect, result,
534 "using quote_plus(): %r != %r" % (expect, result))
535 # Errors test for quote_plus
536 given = "ab\u6f22\u5b57 cd"
537 expect = "ab%3F%3F+cd"
538 result = urllib.parse.quote_plus(given, encoding="latin-1",
539 errors="replace")
540 self.assertEqual(expect, result,
541 "using quote_plus(): %r != %r" % (expect, result))
542
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000543
Brett Cannon74bfd702003-04-25 09:39:47 +0000544class UnquotingTests(unittest.TestCase):
545 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000546
Brett Cannon74bfd702003-04-25 09:39:47 +0000547 See the doc string for quoting_Tests for details on quoting and such.
548
549 """
550
551 def test_unquoting(self):
552 # Make sure unquoting of all ASCII values works
553 escape_list = []
554 for num in range(128):
555 given = hexescape(chr(num))
556 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000557 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000558 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000559 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000560 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000561 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000562 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000563 (expect, result))
564 escape_list.append(given)
565 escape_string = ''.join(escape_list)
566 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000567 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000568 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000569 "using unquote(): not all characters escaped: "
570 "%s" % result)
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000571 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
572 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
Florent Xicluna99e472e2010-08-14 23:12:27 +0000573 with warnings.catch_warnings():
574 warnings.simplefilter('ignore', BytesWarning)
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000575 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
Brett Cannon74bfd702003-04-25 09:39:47 +0000576
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000577 def test_unquoting_badpercent(self):
578 # Test unquoting on bad percent-escapes
579 given = '%xab'
580 expect = given
581 result = urllib.parse.unquote(given)
582 self.assertEqual(expect, result, "using unquote(): %r != %r"
583 % (expect, result))
584 given = '%x'
585 expect = given
586 result = urllib.parse.unquote(given)
587 self.assertEqual(expect, result, "using unquote(): %r != %r"
588 % (expect, result))
589 given = '%'
590 expect = given
591 result = urllib.parse.unquote(given)
592 self.assertEqual(expect, result, "using unquote(): %r != %r"
593 % (expect, result))
594 # unquote_to_bytes
595 given = '%xab'
596 expect = bytes(given, 'ascii')
597 result = urllib.parse.unquote_to_bytes(given)
598 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
599 % (expect, result))
600 given = '%x'
601 expect = bytes(given, 'ascii')
602 result = urllib.parse.unquote_to_bytes(given)
603 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
604 % (expect, result))
605 given = '%'
606 expect = bytes(given, 'ascii')
607 result = urllib.parse.unquote_to_bytes(given)
608 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
609 % (expect, result))
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000610 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)
611 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000612
613 def test_unquoting_mixed_case(self):
614 # Test unquoting on mixed-case hex digits in the percent-escapes
615 given = '%Ab%eA'
616 expect = b'\xab\xea'
617 result = urllib.parse.unquote_to_bytes(given)
618 self.assertEqual(expect, result,
619 "using unquote_to_bytes(): %r != %r"
620 % (expect, result))
621
Brett Cannon74bfd702003-04-25 09:39:47 +0000622 def test_unquoting_parts(self):
623 # Make sure unquoting works when have non-quoted characters
624 # interspersed
625 given = 'ab%sd' % hexescape('c')
626 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000627 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000628 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000629 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000630 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000631 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000632 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000633
Brett Cannon74bfd702003-04-25 09:39:47 +0000634 def test_unquoting_plus(self):
635 # Test difference between unquote() and unquote_plus()
636 given = "are+there+spaces..."
637 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000638 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000639 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000640 "using unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000641 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000642 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000643 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000644 "using unquote_plus(): %r != %r" % (expect, result))
645
646 def test_unquote_to_bytes(self):
647 given = 'br%C3%BCckner_sapporo_20050930.doc'
648 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
649 result = urllib.parse.unquote_to_bytes(given)
650 self.assertEqual(expect, result,
651 "using unquote_to_bytes(): %r != %r"
652 % (expect, result))
653 # Test on a string with unescaped non-ASCII characters
654 # (Technically an invalid URI; expect those characters to be UTF-8
655 # encoded).
656 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
657 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
658 self.assertEqual(expect, result,
659 "using unquote_to_bytes(): %r != %r"
660 % (expect, result))
661 # Test with a bytes as input
662 given = b'%A2%D8ab%FF'
663 expect = b'\xa2\xd8ab\xff'
664 result = urllib.parse.unquote_to_bytes(given)
665 self.assertEqual(expect, result,
666 "using unquote_to_bytes(): %r != %r"
667 % (expect, result))
668 # Test with a bytes as input, with unescaped non-ASCII bytes
669 # (Technically an invalid URI; expect those bytes to be preserved)
670 given = b'%A2\xd8ab%FF'
671 expect = b'\xa2\xd8ab\xff'
672 result = urllib.parse.unquote_to_bytes(given)
673 self.assertEqual(expect, result,
674 "using unquote_to_bytes(): %r != %r"
675 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000676
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000677 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000678 # Characters in the Latin-1 range, encoded with UTF-8
679 given = 'br%C3%BCckner_sapporo_20050930.doc'
680 expect = 'br\u00fcckner_sapporo_20050930.doc'
681 result = urllib.parse.unquote(given)
682 self.assertEqual(expect, result,
683 "using unquote(): %r != %r" % (expect, result))
684 # Characters in the Latin-1 range, encoded with None (default)
685 result = urllib.parse.unquote(given, encoding=None, errors=None)
686 self.assertEqual(expect, result,
687 "using unquote(): %r != %r" % (expect, result))
688
689 # Characters in the Latin-1 range, encoded with Latin-1
690 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
691 encoding="latin-1")
692 expect = 'br\u00fcckner_sapporo_20050930.doc'
693 self.assertEqual(expect, result,
694 "using unquote(): %r != %r" % (expect, result))
695
696 # Characters in BMP, encoded with UTF-8
697 given = "%E6%BC%A2%E5%AD%97"
698 expect = "\u6f22\u5b57" # "Kanji"
699 result = urllib.parse.unquote(given)
700 self.assertEqual(expect, result,
701 "using unquote(): %r != %r" % (expect, result))
702
703 # Decode with UTF-8, invalid sequence
704 given = "%F3%B1"
705 expect = "\ufffd" # Replacement character
706 result = urllib.parse.unquote(given)
707 self.assertEqual(expect, result,
708 "using unquote(): %r != %r" % (expect, result))
709
710 # Decode with UTF-8, invalid sequence, replace errors
711 result = urllib.parse.unquote(given, errors="replace")
712 self.assertEqual(expect, result,
713 "using unquote(): %r != %r" % (expect, result))
714
715 # Decode with UTF-8, invalid sequence, ignoring errors
716 given = "%F3%B1"
717 expect = ""
718 result = urllib.parse.unquote(given, errors="ignore")
719 self.assertEqual(expect, result,
720 "using unquote(): %r != %r" % (expect, result))
721
722 # A mix of non-ASCII and percent-encoded characters, UTF-8
723 result = urllib.parse.unquote("\u6f22%C3%BC")
724 expect = '\u6f22\u00fc'
725 self.assertEqual(expect, result,
726 "using unquote(): %r != %r" % (expect, result))
727
728 # A mix of non-ASCII and percent-encoded characters, Latin-1
729 # (Note, the string contains non-Latin-1-representable characters)
730 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
731 expect = '\u6f22\u00fc'
732 self.assertEqual(expect, result,
733 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000734
Brett Cannon74bfd702003-04-25 09:39:47 +0000735class urlencode_Tests(unittest.TestCase):
736 """Tests for urlencode()"""
737
738 def help_inputtype(self, given, test_type):
739 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000740
Brett Cannon74bfd702003-04-25 09:39:47 +0000741 'given' must lead to only the pairs:
742 * 1st, 1
743 * 2nd, 2
744 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000745
Brett Cannon74bfd702003-04-25 09:39:47 +0000746 Test cannot assume anything about order. Docs make no guarantee and
747 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000748
Brett Cannon74bfd702003-04-25 09:39:47 +0000749 """
750 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000751 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000752 for expected in expect_somewhere:
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000753 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000754 "testing %s: %s not found in %s" %
755 (test_type, expected, result))
756 self.assertEqual(result.count('&'), 2,
757 "testing %s: expected 2 '&'s; got %s" %
758 (test_type, result.count('&')))
759 amp_location = result.index('&')
760 on_amp_left = result[amp_location - 1]
761 on_amp_right = result[amp_location + 1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000762 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000763 "testing %s: '&' not located in proper place in %s" %
764 (test_type, result))
765 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
766 "testing %s: "
767 "unexpected number of characters: %s != %s" %
768 (test_type, len(result), (5 * 3) + 2))
769
770 def test_using_mapping(self):
771 # Test passing in a mapping object as an argument.
772 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
773 "using dict as input type")
774
775 def test_using_sequence(self):
776 # Test passing in a sequence of two-item sequences as an argument.
777 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
778 "using sequence of two-item tuples as input")
779
780 def test_quoting(self):
781 # Make sure keys and values are quoted using quote_plus()
782 given = {"&":"="}
783 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000784 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000785 self.assertEqual(expect, result)
786 given = {"key name":"A bunch of pluses"}
787 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000788 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000789 self.assertEqual(expect, result)
790
791 def test_doseq(self):
792 # Test that passing True for 'doseq' parameter works correctly
793 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000794 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
795 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000796 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000797 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000798 for value in given["sequence"]:
799 expect = "sequence=%s" % value
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000800 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000801 self.assertEqual(result.count('&'), 2,
802 "Expected 2 '&'s, got %s" % result.count('&'))
803
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000804 def test_empty_sequence(self):
805 self.assertEqual("", urllib.parse.urlencode({}))
806 self.assertEqual("", urllib.parse.urlencode([]))
807
808 def test_nonstring_values(self):
809 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
810 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
811
812 def test_nonstring_seq_values(self):
813 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
814 self.assertEqual("a=None&a=a",
815 urllib.parse.urlencode({"a": [None, "a"]}, True))
816 self.assertEqual("a=a&a=b",
817 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
818
Senthil Kumaranfe1ad152010-07-03 17:55:41 +0000819 def test_urlencode_encoding(self):
820 # ASCII encoding. Expect %3F with errors="replace'
821 given = (('\u00a0', '\u00c1'),)
822 expect = '%3F=%3F'
823 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
824 self.assertEqual(expect, result)
825
826 # Default is UTF-8 encoding.
827 given = (('\u00a0', '\u00c1'),)
828 expect = '%C2%A0=%C3%81'
829 result = urllib.parse.urlencode(given)
830 self.assertEqual(expect, result)
831
832 # Latin-1 encoding.
833 given = (('\u00a0', '\u00c1'),)
834 expect = '%A0=%C1'
835 result = urllib.parse.urlencode(given, encoding="latin-1")
836 self.assertEqual(expect, result)
837
838 def test_urlencode_encoding_doseq(self):
839 # ASCII Encoding. Expect %3F with errors="replace'
840 given = (('\u00a0', '\u00c1'),)
841 expect = '%3F=%3F'
842 result = urllib.parse.urlencode(given, doseq=True,
843 encoding="ASCII", errors="replace")
844 self.assertEqual(expect, result)
845
846 # ASCII Encoding. On a sequence of values.
847 given = (("\u00a0", (1, "\u00c1")),)
848 expect = '%3F=1&%3F=%3F'
849 result = urllib.parse.urlencode(given, True,
850 encoding="ASCII", errors="replace")
851 self.assertEqual(expect, result)
852
853 # Utf-8
854 given = (("\u00a0", "\u00c1"),)
855 expect = '%C2%A0=%C3%81'
856 result = urllib.parse.urlencode(given, True)
857 self.assertEqual(expect, result)
858
859 given = (("\u00a0", (42, "\u00c1")),)
860 expect = '%C2%A0=42&%C2%A0=%C3%81'
861 result = urllib.parse.urlencode(given, True)
862 self.assertEqual(expect, result)
863
864 # latin-1
865 given = (("\u00a0", "\u00c1"),)
866 expect = '%A0=%C1'
867 result = urllib.parse.urlencode(given, True, encoding="latin-1")
868 self.assertEqual(expect, result)
869
870 given = (("\u00a0", (42, "\u00c1")),)
871 expect = '%A0=42&%A0=%C1'
872 result = urllib.parse.urlencode(given, True, encoding="latin-1")
873 self.assertEqual(expect, result)
874
875 def test_urlencode_bytes(self):
876 given = ((b'\xa0\x24', b'\xc1\x24'),)
877 expect = '%A0%24=%C1%24'
878 result = urllib.parse.urlencode(given)
879 self.assertEqual(expect, result)
880 result = urllib.parse.urlencode(given, True)
881 self.assertEqual(expect, result)
882
883 # Sequence of values
884 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
885 expect = '%A0%24=42&%A0%24=%C1%24'
886 result = urllib.parse.urlencode(given, True)
887 self.assertEqual(expect, result)
888
889 def test_urlencode_encoding_safe_parameter(self):
890
891 # Send '$' (\x24) as safe character
892 # Default utf-8 encoding
893
894 given = ((b'\xa0\x24', b'\xc1\x24'),)
895 result = urllib.parse.urlencode(given, safe=":$")
896 expect = '%A0$=%C1$'
897 self.assertEqual(expect, result)
898
899 given = ((b'\xa0\x24', b'\xc1\x24'),)
900 result = urllib.parse.urlencode(given, doseq=True, safe=":$")
901 expect = '%A0$=%C1$'
902 self.assertEqual(expect, result)
903
904 # Safe parameter in sequence
905 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
906 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
907 result = urllib.parse.urlencode(given, True, safe=":$")
908 self.assertEqual(expect, result)
909
910 # Test all above in latin-1 encoding
911
912 given = ((b'\xa0\x24', b'\xc1\x24'),)
913 result = urllib.parse.urlencode(given, safe=":$",
914 encoding="latin-1")
915 expect = '%A0$=%C1$'
916 self.assertEqual(expect, result)
917
918 given = ((b'\xa0\x24', b'\xc1\x24'),)
919 expect = '%A0$=%C1$'
920 result = urllib.parse.urlencode(given, doseq=True, safe=":$",
921 encoding="latin-1")
922
923 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
924 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
925 result = urllib.parse.urlencode(given, True, safe=":$",
926 encoding="latin-1")
927 self.assertEqual(expect, result)
928
Brett Cannon74bfd702003-04-25 09:39:47 +0000929class Pathname_Tests(unittest.TestCase):
930 """Test pathname2url() and url2pathname()"""
931
932 def test_basic(self):
933 # Make sure simple tests pass
934 expected_path = os.path.join("parts", "of", "a", "path")
935 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000936 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000937 self.assertEqual(expected_url, result,
938 "pathname2url() failed; %s != %s" %
939 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000940 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000941 self.assertEqual(expected_path, result,
942 "url2pathame() failed; %s != %s" %
943 (result, expected_path))
944
945 def test_quoting(self):
946 # Test automatic quoting and unquoting works for pathnam2url() and
947 # url2pathname() respectively
948 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000949 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
950 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000951 self.assertEqual(expect, result,
952 "pathname2url() failed; %s != %s" %
953 (expect, result))
954 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000955 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000956 self.assertEqual(expect, result,
957 "url2pathname() failed; %s != %s" %
958 (expect, result))
959 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000960 expect = "%s/using_quote" % urllib.parse.quote("make sure")
961 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000962 self.assertEqual(expect, result,
963 "pathname2url() failed; %s != %s" %
964 (expect, result))
965 given = "make+sure/using_unquote"
966 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000967 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000968 self.assertEqual(expect, result,
969 "url2pathname() failed; %s != %s" %
970 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000971
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000972class Utility_Tests(unittest.TestCase):
973 """Testcase to test the various utility functions in the urllib."""
974
975 def test_splitpasswd(self):
976 """Some of password examples are not sensible, but it is added to
977 confirming to RFC2617 and addressing issue4675.
978 """
979 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
980 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
981 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
982 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
983 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
984 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
985 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
986
Senthil Kumaran690ce9b2009-05-05 18:41:13 +0000987
988class URLopener_Tests(unittest.TestCase):
989 """Testcase to test the open method of URLopener class."""
990
991 def test_quoted_open(self):
992 class DummyURLopener(urllib.request.URLopener):
993 def open_spam(self, url):
994 return url
995
996 self.assertEqual(DummyURLopener().open(
997 'spam://example/ /'),'//example/%20/')
998
Senthil Kumaran0e7e9ae2010-02-20 22:30:21 +0000999 # test the safe characters are not quoted by urlopen
1000 self.assertEqual(DummyURLopener().open(
1001 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
1002 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
1003
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004# Just commented them out.
1005# Can't really tell why keep failing in windows and sparc.
1006# Everywhere else they work ok, but on those machines, someteimes
1007# fail in one of the tests, sometimes in other. I have a linux, and
1008# the tests go ok.
1009# If anybody has one of the problematic enviroments, please help!
1010# . Facundo
1011#
1012# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001013# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +00001014# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1015# serv.settimeout(3)
1016# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1017# serv.bind(("", 9093))
1018# serv.listen(5)
1019# try:
1020# conn, addr = serv.accept()
1021# conn.send("1 Hola mundo\n")
1022# cantdata = 0
1023# while cantdata < 13:
1024# data = conn.recv(13-cantdata)
1025# cantdata += len(data)
1026# time.sleep(.3)
1027# conn.send("2 No more lines\n")
1028# conn.close()
1029# except socket.timeout:
1030# pass
1031# finally:
1032# serv.close()
1033# evt.set()
1034#
1035# class FTPWrapperTests(unittest.TestCase):
1036#
1037# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001038# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +00001039# ftplib.FTP.port = 9093
1040# self.evt = threading.Event()
1041# threading.Thread(target=server, args=(self.evt,)).start()
1042# time.sleep(.1)
1043#
1044# def tearDown(self):
1045# self.evt.wait()
1046#
1047# def testBasic(self):
1048# # connects
1049# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +00001050# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001051#
1052# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001053# # global default timeout is ignored
1054# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001055# self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001056# socket.setdefaulttimeout(30)
1057# try:
1058# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1059# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +00001060# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001061# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001062# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001063#
Georg Brandlf78e02b2008-06-10 17:40:04 +00001064# def testTimeoutDefault(self):
1065# # global default timeout is used
1066# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001067# self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001068# socket.setdefaulttimeout(30)
1069# try:
1070# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1071# finally:
1072# socket.setdefaulttimeout(None)
1073# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1074# ftp.close()
1075#
1076# def testTimeoutValue(self):
1077# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1078# timeout=30)
1079# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1080# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001081
Skip Montanaro080c9972001-01-28 21:12:22 +00001082
1083
Brett Cannon74bfd702003-04-25 09:39:47 +00001084def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001085 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +00001086 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001087 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001088 urlretrieve_FileTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00001089 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001090 QuotingTests,
1091 UnquotingTests,
1092 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001093 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001094 Utility_Tests,
Senthil Kumaran690ce9b2009-05-05 18:41:13 +00001095 URLopener_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001096 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001097 )
Brett Cannon74bfd702003-04-25 09:39:47 +00001098
1099
1100
1101if __name__ == '__main__':
1102 test_main()