blob: acd55778248c6f91726db4655e483a65ae500151 [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):
106 self.assertEqual(self.returned_obj.getcode(), None)
107
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
135 self.assertEquals('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
Brett Cannon19691362003-04-29 05:08:06 +0000194class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000195 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000196
Brett Cannon19691362003-04-29 05:08:06 +0000197 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000198 # Create a list of temporary files. Each item in the list is a file
199 # name (absolute path or relative to the current working directory).
200 # All files in this list will be deleted in the tearDown method. Note,
201 # this only helps to makes sure temporary files get deleted, but it
202 # does nothing about trying to close files that may still be open. It
203 # is the responsibility of the developer to properly close files even
204 # when exceptional conditions occur.
205 self.tempFiles = []
206
Brett Cannon19691362003-04-29 05:08:06 +0000207 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000208 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000209 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000210 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000211 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000212 FILE.write(self.text)
213 FILE.close()
214 finally:
215 try: FILE.close()
216 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000217
218 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000219 # Delete the temporary files.
220 for each in self.tempFiles:
221 try: os.remove(each)
222 except: pass
223
224 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000225 return "file://%s" % urllib.request.pathname2url(
226 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000227
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000228 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000229 """Creates a new temporary file containing the specified data,
230 registers the file for deletion during the test fixture tear down, and
231 returns the absolute path of the file."""
232
233 newFd, newFilePath = tempfile.mkstemp()
234 try:
235 self.registerFileForCleanUp(newFilePath)
236 newFile = os.fdopen(newFd, "wb")
237 newFile.write(data)
238 newFile.close()
239 finally:
240 try: newFile.close()
241 except: pass
242 return newFilePath
243
244 def registerFileForCleanUp(self, fileName):
245 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000246
247 def test_basic(self):
248 # Make sure that a local file just gets its own location returned and
249 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000250 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000251 self.assertEqual(result[0], support.TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000252 self.assertTrue(isinstance(result[1], email.message.Message),
Barry Warsaw820c1202008-06-12 04:06:45 +0000253 "did not get a email.message.Message instance as second "
Brett Cannon19691362003-04-29 05:08:06 +0000254 "returned value")
255
256 def test_copy(self):
257 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000258 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000259 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000260 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000261 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000262 self.assertEqual(second_temp, result[0])
Georg Brandlab91fde2009-08-13 08:51:18 +0000263 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000264 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000265 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000266 try:
267 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000268 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000269 finally:
270 try: FILE.close()
271 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000272 self.assertEqual(self.text, text)
273
274 def test_reporthook(self):
275 # Make sure that the reporthook works.
276 def hooktester(count, block_size, total_size, count_holder=[0]):
Georg Brandlab91fde2009-08-13 08:51:18 +0000277 self.assertTrue(isinstance(count, int))
278 self.assertTrue(isinstance(block_size, int))
279 self.assertTrue(isinstance(total_size, int))
Brett Cannon19691362003-04-29 05:08:06 +0000280 self.assertEqual(count, count_holder[0])
281 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000282 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000283 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000284 urllib.request.urlretrieve(
285 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000286 second_temp, hooktester)
287
288 def test_reporthook_0_bytes(self):
289 # Test on zero length file. Should call reporthook only 1 time.
290 report = []
291 def hooktester(count, block_size, total_size, _report=report):
292 _report.append((count, block_size, total_size))
293 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000294 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000295 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000296 self.assertEqual(len(report), 1)
297 self.assertEqual(report[0][2], 0)
298
299 def test_reporthook_5_bytes(self):
300 # Test on 5 byte file. Should call reporthook only 2 times (once when
301 # the "network connection" is established and once when the block is
302 # read). Since the block size is 8192 bytes, only one block read is
303 # required to read the entire file.
304 report = []
305 def hooktester(count, block_size, total_size, _report=report):
306 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000307 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000308 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000309 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000310 self.assertEqual(len(report), 2)
311 self.assertEqual(report[0][1], 8192)
312 self.assertEqual(report[0][2], 5)
313
314 def test_reporthook_8193_bytes(self):
315 # Test on 8193 byte file. Should call reporthook only 3 times (once
316 # when the "network connection" is established, once for the next 8192
317 # bytes, and once for the last byte).
318 report = []
319 def hooktester(count, block_size, total_size, _report=report):
320 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000321 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000322 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000323 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000324 self.assertEqual(len(report), 3)
325 self.assertEqual(report[0][1], 8192)
326 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000327
Brett Cannon74bfd702003-04-25 09:39:47 +0000328class QuotingTests(unittest.TestCase):
329 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000330
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000331 According to RFC 2396 (Uniform Resource Identifiers), to escape a
332 character you write it as '%' + <2 character US-ASCII hex value>.
333 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
334 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000335
336 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000337
Brett Cannon74bfd702003-04-25 09:39:47 +0000338 Reserved characters : ";/?:@&=+$,"
339 Have special meaning in URIs and must be escaped if not being used for
340 their special meaning
341 Data characters : letters, digits, and "-_.!~*'()"
342 Unreserved and do not need to be escaped; can be, though, if desired
343 Control characters : 0x00 - 0x1F, 0x7F
344 Have no use in URIs so must be escaped
345 space : 0x20
346 Must be escaped
347 Delimiters : '<>#%"'
348 Must be escaped
349 Unwise : "{}|\^[]`"
350 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000351
Brett Cannon74bfd702003-04-25 09:39:47 +0000352 """
353
354 def test_never_quote(self):
355 # Make sure quote() does not quote letters, digits, and "_,.-"
356 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
357 "abcdefghijklmnopqrstuvwxyz",
358 "0123456789",
359 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000360 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000361 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000362 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000363 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000364 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000365 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000366
367 def test_default_safe(self):
368 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000369 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000370
371 def test_safe(self):
372 # Test setting 'safe' parameter does what it should do
373 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000374 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000375 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000376 "using quote(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000377 result = urllib.parse.quote_plus(quote_by_default,
378 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000379 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000380 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000381 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000382 # Safe expressed as bytes rather than str
383 result = urllib.parse.quote(quote_by_default, safe=b"<>")
384 self.assertEqual(quote_by_default, result,
385 "using quote(): %r != %r" % (quote_by_default, result))
386 # "Safe" non-ASCII characters should have no effect
387 # (Since URIs are not allowed to have non-ASCII characters)
388 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
389 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
390 self.assertEqual(expect, result,
391 "using quote(): %r != %r" %
392 (expect, result))
393 # Same as above, but using a bytes rather than str
394 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
395 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
396 self.assertEqual(expect, result,
397 "using quote(): %r != %r" %
398 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000399
400 def test_default_quoting(self):
401 # Make sure all characters that should be quoted are by default sans
402 # space (separate test for that).
403 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
404 should_quote.append('<>#%"{}|\^[]`')
405 should_quote.append(chr(127)) # For 0x7F
406 should_quote = ''.join(should_quote)
407 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000408 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000409 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000410 "using quote(): "
411 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000412 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000413 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000414 self.assertEqual(hexescape(char), result,
415 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000416 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000417 (char, hexescape(char), result))
418 del should_quote
419 partial_quote = "ab[]cd"
420 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000421 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000422 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000423 "using quote(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000424 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000425 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000426
427 def test_quoting_space(self):
428 # Make sure quote() and quote_plus() handle spaces as specified in
429 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000430 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000431 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000432 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000433 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000434 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000435 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000436 given = "a b cd e f"
437 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000438 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000439 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000440 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000441 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000442 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000443 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000444 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000445
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000446 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000447 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000448 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000449 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000450 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000451 # Test with bytes
452 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
453 'alpha%2Bbeta+gamma')
454 # Test with safe bytes
455 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
456 'alpha+beta+gamma')
457
458 def test_quote_bytes(self):
459 # Bytes should quote directly to percent-encoded values
460 given = b"\xa2\xd8ab\xff"
461 expect = "%A2%D8ab%FF"
462 result = urllib.parse.quote(given)
463 self.assertEqual(expect, result,
464 "using quote(): %r != %r" % (expect, result))
465 # Encoding argument should raise type error on bytes input
466 self.assertRaises(TypeError, urllib.parse.quote, given,
467 encoding="latin-1")
468 # quote_from_bytes should work the same
469 result = urllib.parse.quote_from_bytes(given)
470 self.assertEqual(expect, result,
471 "using quote_from_bytes(): %r != %r"
472 % (expect, result))
473
474 def test_quote_with_unicode(self):
475 # Characters in Latin-1 range, encoded by default in UTF-8
476 given = "\xa2\xd8ab\xff"
477 expect = "%C2%A2%C3%98ab%C3%BF"
478 result = urllib.parse.quote(given)
479 self.assertEqual(expect, result,
480 "using quote(): %r != %r" % (expect, result))
481 # Characters in Latin-1 range, encoded by with None (default)
482 result = urllib.parse.quote(given, encoding=None, errors=None)
483 self.assertEqual(expect, result,
484 "using quote(): %r != %r" % (expect, result))
485 # Characters in Latin-1 range, encoded with Latin-1
486 given = "\xa2\xd8ab\xff"
487 expect = "%A2%D8ab%FF"
488 result = urllib.parse.quote(given, encoding="latin-1")
489 self.assertEqual(expect, result,
490 "using quote(): %r != %r" % (expect, result))
491 # Characters in BMP, encoded by default in UTF-8
492 given = "\u6f22\u5b57" # "Kanji"
493 expect = "%E6%BC%A2%E5%AD%97"
494 result = urllib.parse.quote(given)
495 self.assertEqual(expect, result,
496 "using quote(): %r != %r" % (expect, result))
497 # Characters in BMP, encoded with Latin-1
498 given = "\u6f22\u5b57"
499 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
500 encoding="latin-1")
501 # Characters in BMP, encoded with Latin-1, with replace error handling
502 given = "\u6f22\u5b57"
503 expect = "%3F%3F" # "??"
504 result = urllib.parse.quote(given, encoding="latin-1",
505 errors="replace")
506 self.assertEqual(expect, result,
507 "using quote(): %r != %r" % (expect, result))
508 # Characters in BMP, Latin-1, with xmlcharref error handling
509 given = "\u6f22\u5b57"
510 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
511 result = urllib.parse.quote(given, encoding="latin-1",
512 errors="xmlcharrefreplace")
513 self.assertEqual(expect, result,
514 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000515
Georg Brandlfaf41492009-05-26 18:31:11 +0000516 def test_quote_plus_with_unicode(self):
517 # Encoding (latin-1) test for quote_plus
518 given = "\xa2\xd8 \xff"
519 expect = "%A2%D8+%FF"
520 result = urllib.parse.quote_plus(given, encoding="latin-1")
521 self.assertEqual(expect, result,
522 "using quote_plus(): %r != %r" % (expect, result))
523 # Errors test for quote_plus
524 given = "ab\u6f22\u5b57 cd"
525 expect = "ab%3F%3F+cd"
526 result = urllib.parse.quote_plus(given, encoding="latin-1",
527 errors="replace")
528 self.assertEqual(expect, result,
529 "using quote_plus(): %r != %r" % (expect, result))
530
Brett Cannon74bfd702003-04-25 09:39:47 +0000531class UnquotingTests(unittest.TestCase):
532 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000533
Brett Cannon74bfd702003-04-25 09:39:47 +0000534 See the doc string for quoting_Tests for details on quoting and such.
535
536 """
537
538 def test_unquoting(self):
539 # Make sure unquoting of all ASCII values works
540 escape_list = []
541 for num in range(128):
542 given = hexescape(chr(num))
543 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000544 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000545 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000546 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000547 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000548 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000549 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000550 (expect, result))
551 escape_list.append(given)
552 escape_string = ''.join(escape_list)
553 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000554 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000555 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000556 "using unquote(): not all characters escaped: "
557 "%s" % result)
558
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000559 def test_unquoting_badpercent(self):
560 # Test unquoting on bad percent-escapes
561 given = '%xab'
562 expect = given
563 result = urllib.parse.unquote(given)
564 self.assertEqual(expect, result, "using unquote(): %r != %r"
565 % (expect, result))
566 given = '%x'
567 expect = given
568 result = urllib.parse.unquote(given)
569 self.assertEqual(expect, result, "using unquote(): %r != %r"
570 % (expect, result))
571 given = '%'
572 expect = given
573 result = urllib.parse.unquote(given)
574 self.assertEqual(expect, result, "using unquote(): %r != %r"
575 % (expect, result))
576 # unquote_to_bytes
577 given = '%xab'
578 expect = bytes(given, 'ascii')
579 result = urllib.parse.unquote_to_bytes(given)
580 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
581 % (expect, result))
582 given = '%x'
583 expect = bytes(given, 'ascii')
584 result = urllib.parse.unquote_to_bytes(given)
585 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
586 % (expect, result))
587 given = '%'
588 expect = bytes(given, 'ascii')
589 result = urllib.parse.unquote_to_bytes(given)
590 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
591 % (expect, result))
592
593 def test_unquoting_mixed_case(self):
594 # Test unquoting on mixed-case hex digits in the percent-escapes
595 given = '%Ab%eA'
596 expect = b'\xab\xea'
597 result = urllib.parse.unquote_to_bytes(given)
598 self.assertEqual(expect, result,
599 "using unquote_to_bytes(): %r != %r"
600 % (expect, result))
601
Brett Cannon74bfd702003-04-25 09:39:47 +0000602 def test_unquoting_parts(self):
603 # Make sure unquoting works when have non-quoted characters
604 # interspersed
605 given = 'ab%sd' % hexescape('c')
606 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000607 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000608 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000609 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000610 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000611 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000612 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000613
Brett Cannon74bfd702003-04-25 09:39:47 +0000614 def test_unquoting_plus(self):
615 # Test difference between unquote() and unquote_plus()
616 given = "are+there+spaces..."
617 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000618 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000619 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000620 "using unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000621 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000622 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000623 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000624 "using unquote_plus(): %r != %r" % (expect, result))
625
626 def test_unquote_to_bytes(self):
627 given = 'br%C3%BCckner_sapporo_20050930.doc'
628 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
629 result = urllib.parse.unquote_to_bytes(given)
630 self.assertEqual(expect, result,
631 "using unquote_to_bytes(): %r != %r"
632 % (expect, result))
633 # Test on a string with unescaped non-ASCII characters
634 # (Technically an invalid URI; expect those characters to be UTF-8
635 # encoded).
636 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
637 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
638 self.assertEqual(expect, result,
639 "using unquote_to_bytes(): %r != %r"
640 % (expect, result))
641 # Test with a bytes as input
642 given = b'%A2%D8ab%FF'
643 expect = b'\xa2\xd8ab\xff'
644 result = urllib.parse.unquote_to_bytes(given)
645 self.assertEqual(expect, result,
646 "using unquote_to_bytes(): %r != %r"
647 % (expect, result))
648 # Test with a bytes as input, with unescaped non-ASCII bytes
649 # (Technically an invalid URI; expect those bytes to be preserved)
650 given = b'%A2\xd8ab%FF'
651 expect = b'\xa2\xd8ab\xff'
652 result = urllib.parse.unquote_to_bytes(given)
653 self.assertEqual(expect, result,
654 "using unquote_to_bytes(): %r != %r"
655 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000656
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000657 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000658 # Characters in the Latin-1 range, encoded with UTF-8
659 given = 'br%C3%BCckner_sapporo_20050930.doc'
660 expect = 'br\u00fcckner_sapporo_20050930.doc'
661 result = urllib.parse.unquote(given)
662 self.assertEqual(expect, result,
663 "using unquote(): %r != %r" % (expect, result))
664 # Characters in the Latin-1 range, encoded with None (default)
665 result = urllib.parse.unquote(given, encoding=None, errors=None)
666 self.assertEqual(expect, result,
667 "using unquote(): %r != %r" % (expect, result))
668
669 # Characters in the Latin-1 range, encoded with Latin-1
670 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
671 encoding="latin-1")
672 expect = 'br\u00fcckner_sapporo_20050930.doc'
673 self.assertEqual(expect, result,
674 "using unquote(): %r != %r" % (expect, result))
675
676 # Characters in BMP, encoded with UTF-8
677 given = "%E6%BC%A2%E5%AD%97"
678 expect = "\u6f22\u5b57" # "Kanji"
679 result = urllib.parse.unquote(given)
680 self.assertEqual(expect, result,
681 "using unquote(): %r != %r" % (expect, result))
682
683 # Decode with UTF-8, invalid sequence
684 given = "%F3%B1"
685 expect = "\ufffd" # Replacement character
686 result = urllib.parse.unquote(given)
687 self.assertEqual(expect, result,
688 "using unquote(): %r != %r" % (expect, result))
689
690 # Decode with UTF-8, invalid sequence, replace errors
691 result = urllib.parse.unquote(given, errors="replace")
692 self.assertEqual(expect, result,
693 "using unquote(): %r != %r" % (expect, result))
694
695 # Decode with UTF-8, invalid sequence, ignoring errors
696 given = "%F3%B1"
697 expect = ""
698 result = urllib.parse.unquote(given, errors="ignore")
699 self.assertEqual(expect, result,
700 "using unquote(): %r != %r" % (expect, result))
701
702 # A mix of non-ASCII and percent-encoded characters, UTF-8
703 result = urllib.parse.unquote("\u6f22%C3%BC")
704 expect = '\u6f22\u00fc'
705 self.assertEqual(expect, result,
706 "using unquote(): %r != %r" % (expect, result))
707
708 # A mix of non-ASCII and percent-encoded characters, Latin-1
709 # (Note, the string contains non-Latin-1-representable characters)
710 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
711 expect = '\u6f22\u00fc'
712 self.assertEqual(expect, result,
713 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000714
Brett Cannon74bfd702003-04-25 09:39:47 +0000715class urlencode_Tests(unittest.TestCase):
716 """Tests for urlencode()"""
717
718 def help_inputtype(self, given, test_type):
719 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000720
Brett Cannon74bfd702003-04-25 09:39:47 +0000721 'given' must lead to only the pairs:
722 * 1st, 1
723 * 2nd, 2
724 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000725
Brett Cannon74bfd702003-04-25 09:39:47 +0000726 Test cannot assume anything about order. Docs make no guarantee and
727 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000728
Brett Cannon74bfd702003-04-25 09:39:47 +0000729 """
730 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000731 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000732 for expected in expect_somewhere:
Georg Brandlab91fde2009-08-13 08:51:18 +0000733 self.assertTrue(expected in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000734 "testing %s: %s not found in %s" %
735 (test_type, expected, result))
736 self.assertEqual(result.count('&'), 2,
737 "testing %s: expected 2 '&'s; got %s" %
738 (test_type, result.count('&')))
739 amp_location = result.index('&')
740 on_amp_left = result[amp_location - 1]
741 on_amp_right = result[amp_location + 1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000742 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000743 "testing %s: '&' not located in proper place in %s" %
744 (test_type, result))
745 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
746 "testing %s: "
747 "unexpected number of characters: %s != %s" %
748 (test_type, len(result), (5 * 3) + 2))
749
750 def test_using_mapping(self):
751 # Test passing in a mapping object as an argument.
752 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
753 "using dict as input type")
754
755 def test_using_sequence(self):
756 # Test passing in a sequence of two-item sequences as an argument.
757 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
758 "using sequence of two-item tuples as input")
759
760 def test_quoting(self):
761 # Make sure keys and values are quoted using quote_plus()
762 given = {"&":"="}
763 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000764 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000765 self.assertEqual(expect, result)
766 given = {"key name":"A bunch of pluses"}
767 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000768 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000769 self.assertEqual(expect, result)
770
771 def test_doseq(self):
772 # Test that passing True for 'doseq' parameter works correctly
773 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000774 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
775 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000776 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000777 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000778 for value in given["sequence"]:
779 expect = "sequence=%s" % value
Georg Brandlab91fde2009-08-13 08:51:18 +0000780 self.assertTrue(expect in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000781 "%s not found in %s" % (expect, result))
782 self.assertEqual(result.count('&'), 2,
783 "Expected 2 '&'s, got %s" % result.count('&'))
784
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000785 def test_empty_sequence(self):
786 self.assertEqual("", urllib.parse.urlencode({}))
787 self.assertEqual("", urllib.parse.urlencode([]))
788
789 def test_nonstring_values(self):
790 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
791 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
792
793 def test_nonstring_seq_values(self):
794 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
795 self.assertEqual("a=None&a=a",
796 urllib.parse.urlencode({"a": [None, "a"]}, True))
797 self.assertEqual("a=a&a=b",
798 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
799
Senthil Kumaranfe1ad152010-07-03 17:55:41 +0000800 def test_urlencode_encoding(self):
801 # ASCII encoding. Expect %3F with errors="replace'
802 given = (('\u00a0', '\u00c1'),)
803 expect = '%3F=%3F'
804 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
805 self.assertEqual(expect, result)
806
807 # Default is UTF-8 encoding.
808 given = (('\u00a0', '\u00c1'),)
809 expect = '%C2%A0=%C3%81'
810 result = urllib.parse.urlencode(given)
811 self.assertEqual(expect, result)
812
813 # Latin-1 encoding.
814 given = (('\u00a0', '\u00c1'),)
815 expect = '%A0=%C1'
816 result = urllib.parse.urlencode(given, encoding="latin-1")
817 self.assertEqual(expect, result)
818
819 def test_urlencode_encoding_doseq(self):
820 # ASCII Encoding. Expect %3F with errors="replace'
821 given = (('\u00a0', '\u00c1'),)
822 expect = '%3F=%3F'
823 result = urllib.parse.urlencode(given, doseq=True,
824 encoding="ASCII", errors="replace")
825 self.assertEqual(expect, result)
826
827 # ASCII Encoding. On a sequence of values.
828 given = (("\u00a0", (1, "\u00c1")),)
829 expect = '%3F=1&%3F=%3F'
830 result = urllib.parse.urlencode(given, True,
831 encoding="ASCII", errors="replace")
832 self.assertEqual(expect, result)
833
834 # Utf-8
835 given = (("\u00a0", "\u00c1"),)
836 expect = '%C2%A0=%C3%81'
837 result = urllib.parse.urlencode(given, True)
838 self.assertEqual(expect, result)
839
840 given = (("\u00a0", (42, "\u00c1")),)
841 expect = '%C2%A0=42&%C2%A0=%C3%81'
842 result = urllib.parse.urlencode(given, True)
843 self.assertEqual(expect, result)
844
845 # latin-1
846 given = (("\u00a0", "\u00c1"),)
847 expect = '%A0=%C1'
848 result = urllib.parse.urlencode(given, True, encoding="latin-1")
849 self.assertEqual(expect, result)
850
851 given = (("\u00a0", (42, "\u00c1")),)
852 expect = '%A0=42&%A0=%C1'
853 result = urllib.parse.urlencode(given, True, encoding="latin-1")
854 self.assertEqual(expect, result)
855
856 def test_urlencode_bytes(self):
857 given = ((b'\xa0\x24', b'\xc1\x24'),)
858 expect = '%A0%24=%C1%24'
859 result = urllib.parse.urlencode(given)
860 self.assertEqual(expect, result)
861 result = urllib.parse.urlencode(given, True)
862 self.assertEqual(expect, result)
863
864 # Sequence of values
865 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
866 expect = '%A0%24=42&%A0%24=%C1%24'
867 result = urllib.parse.urlencode(given, True)
868 self.assertEqual(expect, result)
869
870 def test_urlencode_encoding_safe_parameter(self):
871
872 # Send '$' (\x24) as safe character
873 # Default utf-8 encoding
874
875 given = ((b'\xa0\x24', b'\xc1\x24'),)
876 result = urllib.parse.urlencode(given, safe=":$")
877 expect = '%A0$=%C1$'
878 self.assertEqual(expect, result)
879
880 given = ((b'\xa0\x24', b'\xc1\x24'),)
881 result = urllib.parse.urlencode(given, doseq=True, safe=":$")
882 expect = '%A0$=%C1$'
883 self.assertEqual(expect, result)
884
885 # Safe parameter in sequence
886 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
887 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
888 result = urllib.parse.urlencode(given, True, safe=":$")
889 self.assertEqual(expect, result)
890
891 # Test all above in latin-1 encoding
892
893 given = ((b'\xa0\x24', b'\xc1\x24'),)
894 result = urllib.parse.urlencode(given, safe=":$",
895 encoding="latin-1")
896 expect = '%A0$=%C1$'
897 self.assertEqual(expect, result)
898
899 given = ((b'\xa0\x24', b'\xc1\x24'),)
900 expect = '%A0$=%C1$'
901 result = urllib.parse.urlencode(given, doseq=True, safe=":$",
902 encoding="latin-1")
903
904 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
905 expect = '%A0$=%C1$&%A0$=13&%A0$=42'
906 result = urllib.parse.urlencode(given, True, safe=":$",
907 encoding="latin-1")
908 self.assertEqual(expect, result)
909
Brett Cannon74bfd702003-04-25 09:39:47 +0000910class Pathname_Tests(unittest.TestCase):
911 """Test pathname2url() and url2pathname()"""
912
913 def test_basic(self):
914 # Make sure simple tests pass
915 expected_path = os.path.join("parts", "of", "a", "path")
916 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000917 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000918 self.assertEqual(expected_url, result,
919 "pathname2url() failed; %s != %s" %
920 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000921 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000922 self.assertEqual(expected_path, result,
923 "url2pathame() failed; %s != %s" %
924 (result, expected_path))
925
926 def test_quoting(self):
927 # Test automatic quoting and unquoting works for pathnam2url() and
928 # url2pathname() respectively
929 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000930 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
931 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000932 self.assertEqual(expect, result,
933 "pathname2url() failed; %s != %s" %
934 (expect, result))
935 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000936 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000937 self.assertEqual(expect, result,
938 "url2pathname() failed; %s != %s" %
939 (expect, result))
940 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000941 expect = "%s/using_quote" % urllib.parse.quote("make sure")
942 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000943 self.assertEqual(expect, result,
944 "pathname2url() failed; %s != %s" %
945 (expect, result))
946 given = "make+sure/using_unquote"
947 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000948 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000949 self.assertEqual(expect, result,
950 "url2pathname() failed; %s != %s" %
951 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000952
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000953class Utility_Tests(unittest.TestCase):
954 """Testcase to test the various utility functions in the urllib."""
955
956 def test_splitpasswd(self):
957 """Some of password examples are not sensible, but it is added to
958 confirming to RFC2617 and addressing issue4675.
959 """
960 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
961 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
962 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
963 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
964 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
965 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
966 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
967
Senthil Kumaran690ce9b2009-05-05 18:41:13 +0000968
969class URLopener_Tests(unittest.TestCase):
970 """Testcase to test the open method of URLopener class."""
971
972 def test_quoted_open(self):
973 class DummyURLopener(urllib.request.URLopener):
974 def open_spam(self, url):
975 return url
976
977 self.assertEqual(DummyURLopener().open(
978 'spam://example/ /'),'//example/%20/')
979
Senthil Kumaran0e7e9ae2010-02-20 22:30:21 +0000980 # test the safe characters are not quoted by urlopen
981 self.assertEqual(DummyURLopener().open(
982 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
983 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
984
Guido van Rossume7ba4952007-06-06 23:52:48 +0000985# Just commented them out.
986# Can't really tell why keep failing in windows and sparc.
987# Everywhere else they work ok, but on those machines, someteimes
988# fail in one of the tests, sometimes in other. I have a linux, and
989# the tests go ok.
990# If anybody has one of the problematic enviroments, please help!
991# . Facundo
992#
993# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000994# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
996# serv.settimeout(3)
997# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
998# serv.bind(("", 9093))
999# serv.listen(5)
1000# try:
1001# conn, addr = serv.accept()
1002# conn.send("1 Hola mundo\n")
1003# cantdata = 0
1004# while cantdata < 13:
1005# data = conn.recv(13-cantdata)
1006# cantdata += len(data)
1007# time.sleep(.3)
1008# conn.send("2 No more lines\n")
1009# conn.close()
1010# except socket.timeout:
1011# pass
1012# finally:
1013# serv.close()
1014# evt.set()
1015#
1016# class FTPWrapperTests(unittest.TestCase):
1017#
1018# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001019# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +00001020# ftplib.FTP.port = 9093
1021# self.evt = threading.Event()
1022# threading.Thread(target=server, args=(self.evt,)).start()
1023# time.sleep(.1)
1024#
1025# def tearDown(self):
1026# self.evt.wait()
1027#
1028# def testBasic(self):
1029# # connects
1030# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +00001031# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001032#
1033# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001034# # global default timeout is ignored
1035# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001036# self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001037# socket.setdefaulttimeout(30)
1038# try:
1039# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1040# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +00001041# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001042# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001043# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001044#
Georg Brandlf78e02b2008-06-10 17:40:04 +00001045# def testTimeoutDefault(self):
1046# # global default timeout is used
1047# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +00001048# self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001049# socket.setdefaulttimeout(30)
1050# try:
1051# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1052# finally:
1053# socket.setdefaulttimeout(None)
1054# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1055# ftp.close()
1056#
1057# def testTimeoutValue(self):
1058# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1059# timeout=30)
1060# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1061# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001062
Skip Montanaro080c9972001-01-28 21:12:22 +00001063
1064
Brett Cannon74bfd702003-04-25 09:39:47 +00001065def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001066 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +00001067 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001068 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001069 urlretrieve_FileTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00001070 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001071 QuotingTests,
1072 UnquotingTests,
1073 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001074 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +00001075 Utility_Tests,
Senthil Kumaran690ce9b2009-05-05 18:41:13 +00001076 URLopener_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001077 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001078 )
Brett Cannon74bfd702003-04-25 09:39:47 +00001079
1080
1081
1082if __name__ == '__main__':
1083 test_main()