blob: 775d81099294ac55952f6e0d27f7f11d64b51e73 [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
Jeremy Hylton6102e292000-08-31 15:48:10 +000012
Brett Cannon74bfd702003-04-25 09:39:47 +000013def hexescape(char):
14 """Escape char as RFC 2396 specifies"""
15 hex_repr = hex(ord(char))[2:].upper()
16 if len(hex_repr) == 1:
17 hex_repr = "0%s" % hex_repr
18 return "%" + hex_repr
Jeremy Hylton6102e292000-08-31 15:48:10 +000019
Jeremy Hylton1afc1692008-06-18 20:49:58 +000020# Shortcut for testing FancyURLopener
21_urlopener = None
22def urlopen(url, data=None, proxies=None):
23 """urlopen(url [, data]) -> open file-like object"""
24 global _urlopener
25 if proxies is not None:
26 opener = urllib.request.FancyURLopener(proxies=proxies)
27 elif not _urlopener:
28 opener = urllib.request.FancyURLopener()
29 _urlopener = opener
30 else:
31 opener = _urlopener
32 if data is None:
33 return opener.open(url)
34 else:
35 return opener.open(url, data)
36
Brett Cannon74bfd702003-04-25 09:39:47 +000037class urlopen_FileTests(unittest.TestCase):
38 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000039
Brett Cannon74bfd702003-04-25 09:39:47 +000040 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000041 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000042
Brett Cannon74bfd702003-04-25 09:39:47 +000043 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000044
Brett Cannon74bfd702003-04-25 09:39:47 +000045 def setUp(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000046 # Create a temp file to use for testing
47 self.text = bytes("test_urllib: %s\n" % self.__class__.__name__,
48 "ascii")
49 f = open(support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000050 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000051 f.write(self.text)
Brett Cannon74bfd702003-04-25 09:39:47 +000052 finally:
Jeremy Hylton1afc1692008-06-18 20:49:58 +000053 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000054 self.pathname = support.TESTFN
Jeremy Hylton1afc1692008-06-18 20:49:58 +000055 self.returned_obj = urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000056
Brett Cannon74bfd702003-04-25 09:39:47 +000057 def tearDown(self):
58 """Shut down the open object"""
59 self.returned_obj.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000060 os.remove(support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000061
Brett Cannon74bfd702003-04-25 09:39:47 +000062 def test_interface(self):
63 # Make sure object returned by urlopen() has the specified methods
64 for attr in ("read", "readline", "readlines", "fileno",
Christian Heimes9bd667a2008-01-20 15:14:11 +000065 "close", "info", "geturl", "getcode", "__iter__"):
Georg Brandlab91fde2009-08-13 08:51:18 +000066 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +000067 "object returned by urlopen() lacks %s attribute" %
68 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000069
Brett Cannon74bfd702003-04-25 09:39:47 +000070 def test_read(self):
71 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +000072
Brett Cannon74bfd702003-04-25 09:39:47 +000073 def test_readline(self):
74 self.assertEqual(self.text, self.returned_obj.readline())
Guido van Rossuma0982942007-07-10 08:30:03 +000075 self.assertEqual(b'', self.returned_obj.readline(),
Brett Cannon74bfd702003-04-25 09:39:47 +000076 "calling readline() after exhausting the file did not"
77 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +000078
Brett Cannon74bfd702003-04-25 09:39:47 +000079 def test_readlines(self):
80 lines_list = self.returned_obj.readlines()
81 self.assertEqual(len(lines_list), 1,
82 "readlines() returned the wrong number of lines")
83 self.assertEqual(lines_list[0], self.text,
84 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +000085
Brett Cannon74bfd702003-04-25 09:39:47 +000086 def test_fileno(self):
87 file_num = self.returned_obj.fileno()
Georg Brandlab91fde2009-08-13 08:51:18 +000088 self.assertTrue(isinstance(file_num, int),
Brett Cannon74bfd702003-04-25 09:39:47 +000089 "fileno() did not return an int")
90 self.assertEqual(os.read(file_num, len(self.text)), self.text,
91 "Reading on the file descriptor returned by fileno() "
92 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +000093
Brett Cannon74bfd702003-04-25 09:39:47 +000094 def test_close(self):
95 # Test close() by calling it hear and then having it be called again
96 # by the tearDown() method for the test
97 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +000098
Brett Cannon74bfd702003-04-25 09:39:47 +000099 def test_info(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000100 self.assertTrue(isinstance(self.returned_obj.info(), email.message.Message))
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000101
Brett Cannon74bfd702003-04-25 09:39:47 +0000102 def test_geturl(self):
103 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +0000104
Christian Heimes9bd667a2008-01-20 15:14:11 +0000105 def test_getcode(self):
Florent Xiclunab4efb3d2010-08-14 18:24:40 +0000106 self.assertIsNone(self.returned_obj.getcode())
Christian Heimes9bd667a2008-01-20 15:14:11 +0000107
Brett Cannon74bfd702003-04-25 09:39:47 +0000108 def test_iter(self):
109 # Test iterator
110 # Don't need to count number of iterations since test would fail the
111 # instant it returned anything beyond the first line from the
112 # comparison
113 for line in self.returned_obj.__iter__():
114 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +0000115
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000116class ProxyTests(unittest.TestCase):
117
118 def setUp(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000119 # Records changes to env vars
120 self.env = support.EnvironmentVarGuard()
Benjamin Petersonffeda292010-01-09 18:48:46 +0000121 # Delete all proxy related env vars
122 for k in os.environ.keys():
123 if 'proxy' in k.lower():
124 self.env.unset(k)
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000125
126 def tearDown(self):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000127 # Restore all proxy related env vars
Walter Dörwaldb525e182009-04-26 21:39:21 +0000128 self.env.__exit__()
129 del self.env
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000130
131 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000132 self.env.set('NO_PROXY', 'localhost')
133 proxies = urllib.request.getproxies_environment()
134 # getproxies_environment use lowered case truncated (no '_proxy') keys
Florent Xiclunab4efb3d2010-08-14 18:24:40 +0000135 self.assertEqual('localhost', proxies['no'])
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000136
137
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000138class urlopen_HttpTests(unittest.TestCase):
139 """Test urlopen() opening a fake http connection."""
140
141 def fakehttp(self, fakedata):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000142 class FakeSocket(io.BytesIO):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000143 def sendall(self, str): pass
Nick Coghlan598c3a82009-02-08 04:01:00 +0000144 def makefile(self, *args, **kwds):
145 return self
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000146 def read(self, amt=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000147 if self.closed: return b""
148 return io.BytesIO.read(self, amt)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000149 def readline(self, length=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000150 if self.closed: return b""
151 return io.BytesIO.readline(self, length)
Georg Brandl24420152008-05-26 16:32:26 +0000152 class FakeHTTPConnection(http.client.HTTPConnection):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000153 def connect(self):
154 self.sock = FakeSocket(fakedata)
Georg Brandl24420152008-05-26 16:32:26 +0000155 self._connection_class = http.client.HTTPConnection
156 http.client.HTTPConnection = FakeHTTPConnection
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000157
158 def unfakehttp(self):
Georg Brandl24420152008-05-26 16:32:26 +0000159 http.client.HTTPConnection = self._connection_class
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000160
161 def test_read(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000162 self.fakehttp(b"Hello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000163 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000164 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000165 self.assertEqual(fp.readline(), b"Hello!")
166 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000167 self.assertEqual(fp.geturl(), 'http://python.org/')
168 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000169 finally:
170 self.unfakehttp()
171
Christian Heimes57dddfb2008-01-02 18:30:52 +0000172 def test_read_bogus(self):
173 # urlopen() should raise IOError for many error codes.
174 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
175Date: Wed, 02 Jan 2008 03:03:54 GMT
176Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
177Connection: close
178Content-Type: text/html; charset=iso-8859-1
179''')
180 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000181 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000182 finally:
183 self.unfakehttp()
184
Guido van Rossumd8faa362007-04-27 19:54:29 +0000185 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000186 # urlopen() raises IOError if the underlying socket does not send any
187 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000188 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000190 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000191 finally:
192 self.unfakehttp()
193
Senthil Kumaranafef78f2010-08-01 17:55:50 +0000194 def test_userpass_inurl(self):
195 self.fakehttp(b"Hello!")
196 try:
197 fp = urlopen("http://user:pass@python.org/")
198 self.assertEqual(fp.readline(), b"Hello!")
199 self.assertEqual(fp.readline(), b"")
200 self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
201 self.assertEqual(fp.getcode(), 200)
202 finally:
203 self.unfakehttp()
204
Brett Cannon19691362003-04-29 05:08:06 +0000205class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000206 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000207
Brett Cannon19691362003-04-29 05:08:06 +0000208 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000209 # Create a list of temporary files. Each item in the list is a file
210 # name (absolute path or relative to the current working directory).
211 # All files in this list will be deleted in the tearDown method. Note,
212 # this only helps to makes sure temporary files get deleted, but it
213 # does nothing about trying to close files that may still be open. It
214 # is the responsibility of the developer to properly close files even
215 # when exceptional conditions occur.
216 self.tempFiles = []
217
Brett Cannon19691362003-04-29 05:08:06 +0000218 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000219 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000220 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000221 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000222 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000223 FILE.write(self.text)
224 FILE.close()
225 finally:
226 try: FILE.close()
227 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000228
229 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000230 # Delete the temporary files.
231 for each in self.tempFiles:
232 try: os.remove(each)
233 except: pass
234
235 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000236 return "file://%s" % urllib.request.pathname2url(
237 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000238
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000239 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000240 """Creates a new temporary file containing the specified data,
241 registers the file for deletion during the test fixture tear down, and
242 returns the absolute path of the file."""
243
244 newFd, newFilePath = tempfile.mkstemp()
245 try:
246 self.registerFileForCleanUp(newFilePath)
247 newFile = os.fdopen(newFd, "wb")
248 newFile.write(data)
249 newFile.close()
250 finally:
251 try: newFile.close()
252 except: pass
253 return newFilePath
254
255 def registerFileForCleanUp(self, fileName):
256 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000257
258 def test_basic(self):
259 # Make sure that a local file just gets its own location returned and
260 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000261 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000262 self.assertEqual(result[0], support.TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000263 self.assertTrue(isinstance(result[1], email.message.Message),
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000264 "did not get a email.message.Message instance "
265 "as second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000266
267 def test_copy(self):
268 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000269 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000270 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000271 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000272 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000273 self.assertEqual(second_temp, result[0])
Georg Brandlab91fde2009-08-13 08:51:18 +0000274 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000275 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000276 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000277 try:
278 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000279 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000280 finally:
281 try: FILE.close()
282 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000283 self.assertEqual(self.text, text)
284
285 def test_reporthook(self):
286 # Make sure that the reporthook works.
287 def hooktester(count, block_size, total_size, count_holder=[0]):
Georg Brandlab91fde2009-08-13 08:51:18 +0000288 self.assertTrue(isinstance(count, int))
289 self.assertTrue(isinstance(block_size, int))
290 self.assertTrue(isinstance(total_size, int))
Brett Cannon19691362003-04-29 05:08:06 +0000291 self.assertEqual(count, count_holder[0])
292 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000293 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000294 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000295 urllib.request.urlretrieve(
296 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000297 second_temp, hooktester)
298
299 def test_reporthook_0_bytes(self):
300 # Test on zero length file. Should call reporthook only 1 time.
301 report = []
302 def hooktester(count, block_size, total_size, _report=report):
303 _report.append((count, block_size, total_size))
304 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000305 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000306 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000307 self.assertEqual(len(report), 1)
308 self.assertEqual(report[0][2], 0)
309
310 def test_reporthook_5_bytes(self):
311 # Test on 5 byte file. Should call reporthook only 2 times (once when
312 # the "network connection" is established and once when the block is
313 # read). Since the block size is 8192 bytes, only one block read is
314 # required to read the entire file.
315 report = []
316 def hooktester(count, block_size, total_size, _report=report):
317 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000318 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000319 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000320 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000321 self.assertEqual(len(report), 2)
322 self.assertEqual(report[0][1], 8192)
323 self.assertEqual(report[0][2], 5)
324
325 def test_reporthook_8193_bytes(self):
326 # Test on 8193 byte file. Should call reporthook only 3 times (once
327 # when the "network connection" is established, once for the next 8192
328 # bytes, and once for the last byte).
329 report = []
330 def hooktester(count, block_size, total_size, _report=report):
331 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000332 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000333 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000334 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000335 self.assertEqual(len(report), 3)
336 self.assertEqual(report[0][1], 8192)
337 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000338
Brett Cannon74bfd702003-04-25 09:39:47 +0000339class QuotingTests(unittest.TestCase):
340 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000341
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000342 According to RFC 2396 (Uniform Resource Identifiers), to escape a
343 character you write it as '%' + <2 character US-ASCII hex value>.
344 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
345 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000346
347 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000348
Brett Cannon74bfd702003-04-25 09:39:47 +0000349 Reserved characters : ";/?:@&=+$,"
350 Have special meaning in URIs and must be escaped if not being used for
351 their special meaning
352 Data characters : letters, digits, and "-_.!~*'()"
353 Unreserved and do not need to be escaped; can be, though, if desired
354 Control characters : 0x00 - 0x1F, 0x7F
355 Have no use in URIs so must be escaped
356 space : 0x20
357 Must be escaped
358 Delimiters : '<>#%"'
359 Must be escaped
360 Unwise : "{}|\^[]`"
361 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000362
Brett Cannon74bfd702003-04-25 09:39:47 +0000363 """
364
365 def test_never_quote(self):
366 # Make sure quote() does not quote letters, digits, and "_,.-"
367 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
368 "abcdefghijklmnopqrstuvwxyz",
369 "0123456789",
370 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000371 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000372 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000373 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000374 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000375 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000376 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000377
378 def test_default_safe(self):
379 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000380 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000381
382 def test_safe(self):
383 # Test setting 'safe' parameter does what it should do
384 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000385 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000386 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000387 "using quote(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000388 result = urllib.parse.quote_plus(quote_by_default,
389 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000390 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000391 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000392 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000393 # Safe expressed as bytes rather than str
394 result = urllib.parse.quote(quote_by_default, safe=b"<>")
395 self.assertEqual(quote_by_default, result,
396 "using quote(): %r != %r" % (quote_by_default, result))
397 # "Safe" non-ASCII characters should have no effect
398 # (Since URIs are not allowed to have non-ASCII characters)
399 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
400 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
401 self.assertEqual(expect, result,
402 "using quote(): %r != %r" %
403 (expect, result))
404 # Same as above, but using a bytes rather than str
405 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
406 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
407 self.assertEqual(expect, result,
408 "using quote(): %r != %r" %
409 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000410
411 def test_default_quoting(self):
412 # Make sure all characters that should be quoted are by default sans
413 # space (separate test for that).
414 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
415 should_quote.append('<>#%"{}|\^[]`')
416 should_quote.append(chr(127)) # For 0x7F
417 should_quote = ''.join(should_quote)
418 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000419 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000420 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000421 "using quote(): "
422 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000423 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000424 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000425 self.assertEqual(hexescape(char), result,
426 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000427 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000428 (char, hexescape(char), result))
429 del should_quote
430 partial_quote = "ab[]cd"
431 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000432 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000433 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000434 "using quote(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000435 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000436 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000437
438 def test_quoting_space(self):
439 # Make sure quote() and quote_plus() handle spaces as specified in
440 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000441 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000442 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000443 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000444 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000445 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000446 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000447 given = "a b cd e f"
448 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000449 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000450 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000451 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000452 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000453 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000454 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000455 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000456
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000457 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000458 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000459 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000460 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000461 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000462 # Test with bytes
463 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
464 'alpha%2Bbeta+gamma')
465 # Test with safe bytes
466 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
467 'alpha+beta+gamma')
468
469 def test_quote_bytes(self):
470 # Bytes should quote directly to percent-encoded values
471 given = b"\xa2\xd8ab\xff"
472 expect = "%A2%D8ab%FF"
473 result = urllib.parse.quote(given)
474 self.assertEqual(expect, result,
475 "using quote(): %r != %r" % (expect, result))
476 # Encoding argument should raise type error on bytes input
477 self.assertRaises(TypeError, urllib.parse.quote, given,
478 encoding="latin-1")
479 # quote_from_bytes should work the same
480 result = urllib.parse.quote_from_bytes(given)
481 self.assertEqual(expect, result,
482 "using quote_from_bytes(): %r != %r"
483 % (expect, result))
484
485 def test_quote_with_unicode(self):
486 # Characters in Latin-1 range, encoded by default in UTF-8
487 given = "\xa2\xd8ab\xff"
488 expect = "%C2%A2%C3%98ab%C3%BF"
489 result = urllib.parse.quote(given)
490 self.assertEqual(expect, result,
491 "using quote(): %r != %r" % (expect, result))
492 # Characters in Latin-1 range, encoded by with None (default)
493 result = urllib.parse.quote(given, encoding=None, errors=None)
494 self.assertEqual(expect, result,
495 "using quote(): %r != %r" % (expect, result))
496 # Characters in Latin-1 range, encoded with Latin-1
497 given = "\xa2\xd8ab\xff"
498 expect = "%A2%D8ab%FF"
499 result = urllib.parse.quote(given, encoding="latin-1")
500 self.assertEqual(expect, result,
501 "using quote(): %r != %r" % (expect, result))
502 # Characters in BMP, encoded by default in UTF-8
503 given = "\u6f22\u5b57" # "Kanji"
504 expect = "%E6%BC%A2%E5%AD%97"
505 result = urllib.parse.quote(given)
506 self.assertEqual(expect, result,
507 "using quote(): %r != %r" % (expect, result))
508 # Characters in BMP, encoded with Latin-1
509 given = "\u6f22\u5b57"
510 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
511 encoding="latin-1")
512 # Characters in BMP, encoded with Latin-1, with replace error handling
513 given = "\u6f22\u5b57"
514 expect = "%3F%3F" # "??"
515 result = urllib.parse.quote(given, encoding="latin-1",
516 errors="replace")
517 self.assertEqual(expect, result,
518 "using quote(): %r != %r" % (expect, result))
519 # Characters in BMP, Latin-1, with xmlcharref error handling
520 given = "\u6f22\u5b57"
521 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
522 result = urllib.parse.quote(given, encoding="latin-1",
523 errors="xmlcharrefreplace")
524 self.assertEqual(expect, result,
525 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000526
Georg Brandlfaf41492009-05-26 18:31:11 +0000527 def test_quote_plus_with_unicode(self):
528 # Encoding (latin-1) test for quote_plus
529 given = "\xa2\xd8 \xff"
530 expect = "%A2%D8+%FF"
531 result = urllib.parse.quote_plus(given, encoding="latin-1")
532 self.assertEqual(expect, result,
533 "using quote_plus(): %r != %r" % (expect, result))
534 # Errors test for quote_plus
535 given = "ab\u6f22\u5b57 cd"
536 expect = "ab%3F%3F+cd"
537 result = urllib.parse.quote_plus(given, encoding="latin-1",
538 errors="replace")
539 self.assertEqual(expect, result,
540 "using quote_plus(): %r != %r" % (expect, result))
541
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000542
Brett Cannon74bfd702003-04-25 09:39:47 +0000543class UnquotingTests(unittest.TestCase):
544 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000545
Brett Cannon74bfd702003-04-25 09:39:47 +0000546 See the doc string for quoting_Tests for details on quoting and such.
547
548 """
549
550 def test_unquoting(self):
551 # Make sure unquoting of all ASCII values works
552 escape_list = []
553 for num in range(128):
554 given = hexescape(chr(num))
555 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000556 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000557 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000558 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000559 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000560 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000561 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000562 (expect, result))
563 escape_list.append(given)
564 escape_string = ''.join(escape_list)
565 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000566 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000567 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000568 "using unquote(): not all characters escaped: "
569 "%s" % result)
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000570 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
571 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
572 with support.check_warnings():
573 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
Brett Cannon74bfd702003-04-25 09:39:47 +0000574
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000575 def test_unquoting_badpercent(self):
576 # Test unquoting on bad percent-escapes
577 given = '%xab'
578 expect = given
579 result = urllib.parse.unquote(given)
580 self.assertEqual(expect, result, "using unquote(): %r != %r"
581 % (expect, result))
582 given = '%x'
583 expect = given
584 result = urllib.parse.unquote(given)
585 self.assertEqual(expect, result, "using unquote(): %r != %r"
586 % (expect, result))
587 given = '%'
588 expect = given
589 result = urllib.parse.unquote(given)
590 self.assertEqual(expect, result, "using unquote(): %r != %r"
591 % (expect, result))
592 # unquote_to_bytes
593 given = '%xab'
594 expect = bytes(given, 'ascii')
595 result = urllib.parse.unquote_to_bytes(given)
596 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
597 % (expect, result))
598 given = '%x'
599 expect = bytes(given, 'ascii')
600 result = urllib.parse.unquote_to_bytes(given)
601 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
602 % (expect, result))
603 given = '%'
604 expect = bytes(given, 'ascii')
605 result = urllib.parse.unquote_to_bytes(given)
606 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
607 % (expect, result))
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000608 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)
609 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000610
611 def test_unquoting_mixed_case(self):
612 # Test unquoting on mixed-case hex digits in the percent-escapes
613 given = '%Ab%eA'
614 expect = b'\xab\xea'
615 result = urllib.parse.unquote_to_bytes(given)
616 self.assertEqual(expect, result,
617 "using unquote_to_bytes(): %r != %r"
618 % (expect, result))
619
Brett Cannon74bfd702003-04-25 09:39:47 +0000620 def test_unquoting_parts(self):
621 # Make sure unquoting works when have non-quoted characters
622 # interspersed
623 given = 'ab%sd' % hexescape('c')
624 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000625 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000626 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000627 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000628 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000629 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000630 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000631
Brett Cannon74bfd702003-04-25 09:39:47 +0000632 def test_unquoting_plus(self):
633 # Test difference between unquote() and unquote_plus()
634 given = "are+there+spaces..."
635 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000636 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000637 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000638 "using unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000639 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000640 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000641 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000642 "using unquote_plus(): %r != %r" % (expect, result))
643
644 def test_unquote_to_bytes(self):
645 given = 'br%C3%BCckner_sapporo_20050930.doc'
646 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
647 result = urllib.parse.unquote_to_bytes(given)
648 self.assertEqual(expect, result,
649 "using unquote_to_bytes(): %r != %r"
650 % (expect, result))
651 # Test on a string with unescaped non-ASCII characters
652 # (Technically an invalid URI; expect those characters to be UTF-8
653 # encoded).
654 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
655 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
656 self.assertEqual(expect, result,
657 "using unquote_to_bytes(): %r != %r"
658 % (expect, result))
659 # Test with a bytes as input
660 given = b'%A2%D8ab%FF'
661 expect = b'\xa2\xd8ab\xff'
662 result = urllib.parse.unquote_to_bytes(given)
663 self.assertEqual(expect, result,
664 "using unquote_to_bytes(): %r != %r"
665 % (expect, result))
666 # Test with a bytes as input, with unescaped non-ASCII bytes
667 # (Technically an invalid URI; expect those bytes to be preserved)
668 given = b'%A2\xd8ab%FF'
669 expect = b'\xa2\xd8ab\xff'
670 result = urllib.parse.unquote_to_bytes(given)
671 self.assertEqual(expect, result,
672 "using unquote_to_bytes(): %r != %r"
673 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000674
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000675 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000676 # Characters in the Latin-1 range, encoded with UTF-8
677 given = 'br%C3%BCckner_sapporo_20050930.doc'
678 expect = 'br\u00fcckner_sapporo_20050930.doc'
679 result = urllib.parse.unquote(given)
680 self.assertEqual(expect, result,
681 "using unquote(): %r != %r" % (expect, result))
682 # Characters in the Latin-1 range, encoded with None (default)
683 result = urllib.parse.unquote(given, encoding=None, errors=None)
684 self.assertEqual(expect, result,
685 "using unquote(): %r != %r" % (expect, result))
686
687 # Characters in the Latin-1 range, encoded with Latin-1
688 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
689 encoding="latin-1")
690 expect = 'br\u00fcckner_sapporo_20050930.doc'
691 self.assertEqual(expect, result,
692 "using unquote(): %r != %r" % (expect, result))
693
694 # Characters in BMP, encoded with UTF-8
695 given = "%E6%BC%A2%E5%AD%97"
696 expect = "\u6f22\u5b57" # "Kanji"
697 result = urllib.parse.unquote(given)
698 self.assertEqual(expect, result,
699 "using unquote(): %r != %r" % (expect, result))
700
701 # Decode with UTF-8, invalid sequence
702 given = "%F3%B1"
703 expect = "\ufffd" # Replacement character
704 result = urllib.parse.unquote(given)
705 self.assertEqual(expect, result,
706 "using unquote(): %r != %r" % (expect, result))
707
708 # Decode with UTF-8, invalid sequence, replace errors
709 result = urllib.parse.unquote(given, errors="replace")
710 self.assertEqual(expect, result,
711 "using unquote(): %r != %r" % (expect, result))
712
713 # Decode with UTF-8, invalid sequence, ignoring errors
714 given = "%F3%B1"
715 expect = ""
716 result = urllib.parse.unquote(given, errors="ignore")
717 self.assertEqual(expect, result,
718 "using unquote(): %r != %r" % (expect, result))
719
720 # A mix of non-ASCII and percent-encoded characters, UTF-8
721 result = urllib.parse.unquote("\u6f22%C3%BC")
722 expect = '\u6f22\u00fc'
723 self.assertEqual(expect, result,
724 "using unquote(): %r != %r" % (expect, result))
725
726 # A mix of non-ASCII and percent-encoded characters, Latin-1
727 # (Note, the string contains non-Latin-1-representable characters)
728 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
729 expect = '\u6f22\u00fc'
730 self.assertEqual(expect, result,
731 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000732
Brett Cannon74bfd702003-04-25 09:39:47 +0000733class urlencode_Tests(unittest.TestCase):
734 """Tests for urlencode()"""
735
736 def help_inputtype(self, given, test_type):
737 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000738
Brett Cannon74bfd702003-04-25 09:39:47 +0000739 'given' must lead to only the pairs:
740 * 1st, 1
741 * 2nd, 2
742 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000743
Brett Cannon74bfd702003-04-25 09:39:47 +0000744 Test cannot assume anything about order. Docs make no guarantee and
745 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000746
Brett Cannon74bfd702003-04-25 09:39:47 +0000747 """
748 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000749 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000750 for expected in expect_somewhere:
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000751 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000752 "testing %s: %s not found in %s" %
753 (test_type, expected, result))
754 self.assertEqual(result.count('&'), 2,
755 "testing %s: expected 2 '&'s; got %s" %
756 (test_type, result.count('&')))
757 amp_location = result.index('&')
758 on_amp_left = result[amp_location - 1]
759 on_amp_right = result[amp_location + 1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000760 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000761 "testing %s: '&' not located in proper place in %s" %
762 (test_type, result))
763 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
764 "testing %s: "
765 "unexpected number of characters: %s != %s" %
766 (test_type, len(result), (5 * 3) + 2))
767
768 def test_using_mapping(self):
769 # Test passing in a mapping object as an argument.
770 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
771 "using dict as input type")
772
773 def test_using_sequence(self):
774 # Test passing in a sequence of two-item sequences as an argument.
775 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
776 "using sequence of two-item tuples as input")
777
778 def test_quoting(self):
779 # Make sure keys and values are quoted using quote_plus()
780 given = {"&":"="}
781 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000782 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000783 self.assertEqual(expect, result)
784 given = {"key name":"A bunch of pluses"}
785 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000786 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000787 self.assertEqual(expect, result)
788
789 def test_doseq(self):
790 # Test that passing True for 'doseq' parameter works correctly
791 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000792 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
793 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000794 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000795 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000796 for value in given["sequence"]:
797 expect = "sequence=%s" % value
Florent Xicluna37ddbb82010-08-14 21:06:29 +0000798 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000799 self.assertEqual(result.count('&'), 2,
800 "Expected 2 '&'s, got %s" % result.count('&'))
801
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000802 def test_empty_sequence(self):
803 self.assertEqual("", urllib.parse.urlencode({}))
804 self.assertEqual("", urllib.parse.urlencode([]))
805
806 def test_nonstring_values(self):
807 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
808 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
809
810 def test_nonstring_seq_values(self):
811 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
812 self.assertEqual("a=None&a=a",
813 urllib.parse.urlencode({"a": [None, "a"]}, True))
814 self.assertEqual("a=a&a=b",
815 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
816
Senthil Kumaranfe1ad152010-07-03 17:55:41 +0000817 def test_urlencode_encoding(self):
818 # ASCII encoding. Expect %3F with errors="replace'
819 given = (('\u00a0', '\u00c1'),)
820 expect = '%3F=%3F'
821 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
822 self.assertEqual(expect, result)
823
824 # Default is UTF-8 encoding.
825 given = (('\u00a0', '\u00c1'),)
826 expect = '%C2%A0=%C3%81'
827 result = urllib.parse.urlencode(given)
828 self.assertEqual(expect, result)
829
830 # Latin-1 encoding.
831 given = (('\u00a0', '\u00c1'),)
832 expect = '%A0=%C1'
833 result = urllib.parse.urlencode(given, encoding="latin-1")
834 self.assertEqual(expect, result)
835
836 def test_urlencode_encoding_doseq(self):
837 # ASCII Encoding. Expect %3F with errors="replace'
838 given = (('\u00a0', '\u00c1'),)
839 expect = '%3F=%3F'
840 result = urllib.parse.urlencode(given, doseq=True,
841 encoding="ASCII", errors="replace")
842 self.assertEqual(expect, result)
843
844 # ASCII Encoding. On a sequence of values.
845 given = (("\u00a0", (1, "\u00c1")),)
846 expect = '%3F=1&%3F=%3F'
847 result = urllib.parse.urlencode(given, True,
848 encoding="ASCII", errors="replace")
849 self.assertEqual(expect, result)
850
851 # Utf-8
852 given = (("\u00a0", "\u00c1"),)
853 expect = '%C2%A0=%C3%81'
854 result = urllib.parse.urlencode(given, True)
855 self.assertEqual(expect, result)
856
857 given = (("\u00a0", (42, "\u00c1")),)
858 expect = '%C2%A0=42&%C2%A0=%C3%81'
859 result = urllib.parse.urlencode(given, True)
860 self.assertEqual(expect, result)
861
862 # latin-1
863 given = (("\u00a0", "\u00c1"),)
864 expect = '%A0=%C1'
865 result = urllib.parse.urlencode(given, True, encoding="latin-1")
866 self.assertEqual(expect, result)
867
868 given = (("\u00a0", (42, "\u00c1")),)
869 expect = '%A0=42&%A0=%C1'
870 result = urllib.parse.urlencode(given, True, encoding="latin-1")
871 self.assertEqual(expect, result)
872
873 def test_urlencode_bytes(self):
874 given = ((b'\xa0\x24', b'\xc1\x24'),)
875 expect = '%A0%24=%C1%24'
876 result = urllib.parse.urlencode(given)
877 self.assertEqual(expect, result)
878 result = urllib.parse.urlencode(given, True)
879 self.assertEqual(expect, result)
880
881 # Sequence of values
882 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
883 expect = '%A0%24=42&%A0%24=%C1%24'
884 result = urllib.parse.urlencode(given, True)
885 self.assertEqual(expect, result)
886
887 def test_urlencode_encoding_safe_parameter(self):
888
889 # Send '$' (\x24) as safe character
890 # Default utf-8 encoding
891
892 given = ((b'\xa0\x24', b'\xc1\x24'),)
893 result = urllib.parse.urlencode(given, safe=":$")
894 expect = '%A0$=%C1$'
895 self.assertEqual(expect, result)
896
897 given = ((b'\xa0\x24', b'\xc1\x24'),)
898 result = urllib.parse.urlencode(given, doseq=True, safe=":$")
899 expect = '%A0$=%C1$'
900 self.assertEqual(expect, result)
901
902 # Safe parameter in sequence
903 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
904 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
905 result = urllib.parse.urlencode(given, True, safe=":$")
906 self.assertEqual(expect, result)
907
908 # Test all above in latin-1 encoding
909
910 given = ((b'\xa0\x24', b'\xc1\x24'),)
911 result = urllib.parse.urlencode(given, safe=":$",
912 encoding="latin-1")
913 expect = '%A0$=%C1$'
914 self.assertEqual(expect, result)
915
916 given = ((b'\xa0\x24', b'\xc1\x24'),)
917 expect = '%A0$=%C1$'
918 result = urllib.parse.urlencode(given, doseq=True, safe=":$",
919 encoding="latin-1")
920
921 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
922 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
923 result = urllib.parse.urlencode(given, True, safe=":$",
924 encoding="latin-1")
925 self.assertEqual(expect, result)
926
Brett Cannon74bfd702003-04-25 09:39:47 +0000927class Pathname_Tests(unittest.TestCase):
928 """Test pathname2url() and url2pathname()"""
929
930 def test_basic(self):
931 # Make sure simple tests pass
932 expected_path = os.path.join("parts", "of", "a", "path")
933 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000934 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000935 self.assertEqual(expected_url, result,
936 "pathname2url() failed; %s != %s" %
937 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000938 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000939 self.assertEqual(expected_path, result,
940 "url2pathame() failed; %s != %s" %
941 (result, expected_path))
942
943 def test_quoting(self):
944 # Test automatic quoting and unquoting works for pathnam2url() and
945 # url2pathname() respectively
946 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000947 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
948 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000949 self.assertEqual(expect, result,
950 "pathname2url() failed; %s != %s" %
951 (expect, result))
952 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000953 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000954 self.assertEqual(expect, result,
955 "url2pathname() failed; %s != %s" %
956 (expect, result))
957 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000958 expect = "%s/using_quote" % urllib.parse.quote("make sure")
959 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000960 self.assertEqual(expect, result,
961 "pathname2url() failed; %s != %s" %
962 (expect, result))
963 given = "make+sure/using_unquote"
964 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000965 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000966 self.assertEqual(expect, result,
967 "url2pathname() failed; %s != %s" %
968 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000969
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000970class Utility_Tests(unittest.TestCase):
971 """Testcase to test the various utility functions in the urllib."""
972
973 def test_splitpasswd(self):
974 """Some of password examples are not sensible, but it is added to
975 confirming to RFC2617 and addressing issue4675.
976 """
977 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
978 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
979 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
980 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
981 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
982 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
983 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
984
Senthil Kumaran690ce9b2009-05-05 18:41:13 +0000985
986class URLopener_Tests(unittest.TestCase):
987 """Testcase to test the open method of URLopener class."""
988
989 def test_quoted_open(self):
990 class DummyURLopener(urllib.request.URLopener):
991 def open_spam(self, url):
992 return url
993
994 self.assertEqual(DummyURLopener().open(
995 'spam://example/ /'),'//example/%20/')
996
Senthil Kumaran0e7e9ae2010-02-20 22:30:21 +0000997 # test the safe characters are not quoted by urlopen
998 self.assertEqual(DummyURLopener().open(
999 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
1000 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
1001
Guido van Rossume7ba4952007-06-06 23:52:48 +00001002# Just commented them out.
1003# Can't really tell why keep failing in windows and sparc.
1004# Everywhere else they work ok, but on those machines, someteimes
1005# fail in one of the tests, sometimes in other. I have a linux, and
1006# the tests go ok.
1007# If anybody has one of the problematic enviroments, please help!
1008# . Facundo
1009#
1010# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001011# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +00001012# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1013# serv.settimeout(3)
1014# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1015# serv.bind(("", 9093))
1016# serv.listen(5)
1017# try:
1018# conn, addr = serv.accept()
1019# conn.send("1 Hola mundo\n")
1020# cantdata = 0
1021# while cantdata < 13:
1022# data = conn.recv(13-cantdata)
1023# cantdata += len(data)
1024# time.sleep(.3)
1025# conn.send("2 No more lines\n")
1026# conn.close()
1027# except socket.timeout:
1028# pass
1029# finally:
1030# serv.close()
1031# evt.set()
1032#
1033# class FTPWrapperTests(unittest.TestCase):
1034#
1035# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001036# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +00001037# ftplib.FTP.port = 9093
1038# self.evt = threading.Event()
1039# threading.Thread(target=server, args=(self.evt,)).start()
1040# time.sleep(.1)
1041#
1042# def tearDown(self):
1043# self.evt.wait()
1044#
1045# def testBasic(self):
1046# # connects
1047# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +00001048# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001049#
1050# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001051# # global default timeout is ignored
1052# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001053# self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001054# socket.setdefaulttimeout(30)
1055# try:
1056# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1057# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +00001058# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001059# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001060# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001061#
Georg Brandlf78e02b2008-06-10 17:40:04 +00001062# def testTimeoutDefault(self):
1063# # global default timeout is used
1064# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001065# self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001066# socket.setdefaulttimeout(30)
1067# try:
1068# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1069# finally:
1070# socket.setdefaulttimeout(None)
1071# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1072# ftp.close()
1073#
1074# def testTimeoutValue(self):
1075# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1076# timeout=30)
1077# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1078# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001079
Skip Montanaro080c9972001-01-28 21:12:22 +00001080
1081
Brett Cannon74bfd702003-04-25 09:39:47 +00001082def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001083 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +00001084 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001085 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001086 urlretrieve_FileTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00001087 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001088 QuotingTests,
1089 UnquotingTests,
1090 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001091 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001092 Utility_Tests,
Senthil Kumaran690ce9b2009-05-05 18:41:13 +00001093 URLopener_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001094 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001095 )
Brett Cannon74bfd702003-04-25 09:39:47 +00001096
1097
1098
1099if __name__ == '__main__':
1100 test_main()