blob: ad0c10000349f64c3c481e56fa253612eb190b5f [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 +0000116
117class ProxyTests(unittest.TestCase):
118
119 def setUp(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000120 # Records changes to env vars
121 self.env = support.EnvironmentVarGuard()
Benjamin Petersonffeda292010-01-09 18:48:46 +0000122 # Delete all proxy related env vars
123 for k in os.environ.keys():
124 if 'proxy' in k.lower():
125 self.env.unset(k)
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000126
127 def tearDown(self):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000128 # Restore all proxy related env vars
Walter Dörwaldb525e182009-04-26 21:39:21 +0000129 self.env.__exit__()
130 del self.env
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000131
132 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwaldb525e182009-04-26 21:39:21 +0000133 self.env.set('NO_PROXY', 'localhost')
134 proxies = urllib.request.getproxies_environment()
135 # getproxies_environment use lowered case truncated (no '_proxy') keys
136 self.assertEquals('localhost', proxies['no'])
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000137
138
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000139class urlopen_HttpTests(unittest.TestCase):
140 """Test urlopen() opening a fake http connection."""
141
142 def fakehttp(self, fakedata):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000143 class FakeSocket(io.BytesIO):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000144 def sendall(self, str): pass
Nick Coghlan598c3a82009-02-08 04:01:00 +0000145 def makefile(self, *args, **kwds):
146 return self
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000147 def read(self, amt=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000148 if self.closed: return b""
149 return io.BytesIO.read(self, amt)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000150 def readline(self, length=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000151 if self.closed: return b""
152 return io.BytesIO.readline(self, length)
Georg Brandl24420152008-05-26 16:32:26 +0000153 class FakeHTTPConnection(http.client.HTTPConnection):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000154 def connect(self):
155 self.sock = FakeSocket(fakedata)
Georg Brandl24420152008-05-26 16:32:26 +0000156 self._connection_class = http.client.HTTPConnection
157 http.client.HTTPConnection = FakeHTTPConnection
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000158
159 def unfakehttp(self):
Georg Brandl24420152008-05-26 16:32:26 +0000160 http.client.HTTPConnection = self._connection_class
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000161
162 def test_read(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000163 self.fakehttp(b"Hello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000164 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000165 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000166 self.assertEqual(fp.readline(), b"Hello!")
167 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000168 self.assertEqual(fp.geturl(), 'http://python.org/')
169 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000170 finally:
171 self.unfakehttp()
172
Christian Heimes57dddfb2008-01-02 18:30:52 +0000173 def test_read_bogus(self):
174 # urlopen() should raise IOError for many error codes.
175 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
176Date: Wed, 02 Jan 2008 03:03:54 GMT
177Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
178Connection: close
179Content-Type: text/html; charset=iso-8859-1
180''')
181 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000182 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000183 finally:
184 self.unfakehttp()
185
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000187 # urlopen() raises IOError if the underlying socket does not send any
188 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000189 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000190 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000191 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000192 finally:
193 self.unfakehttp()
194
Brett Cannon19691362003-04-29 05:08:06 +0000195class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000196 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000197
Brett Cannon19691362003-04-29 05:08:06 +0000198 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000199 # Create a list of temporary files. Each item in the list is a file
200 # name (absolute path or relative to the current working directory).
201 # All files in this list will be deleted in the tearDown method. Note,
202 # this only helps to makes sure temporary files get deleted, but it
203 # does nothing about trying to close files that may still be open. It
204 # is the responsibility of the developer to properly close files even
205 # when exceptional conditions occur.
206 self.tempFiles = []
207
Brett Cannon19691362003-04-29 05:08:06 +0000208 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000209 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000210 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000211 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000212 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000213 FILE.write(self.text)
214 FILE.close()
215 finally:
216 try: FILE.close()
217 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000218
219 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000220 # Delete the temporary files.
221 for each in self.tempFiles:
222 try: os.remove(each)
223 except: pass
224
225 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000226 return "file://%s" % urllib.request.pathname2url(
227 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000228
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000229 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000230 """Creates a new temporary file containing the specified data,
231 registers the file for deletion during the test fixture tear down, and
232 returns the absolute path of the file."""
233
234 newFd, newFilePath = tempfile.mkstemp()
235 try:
236 self.registerFileForCleanUp(newFilePath)
237 newFile = os.fdopen(newFd, "wb")
238 newFile.write(data)
239 newFile.close()
240 finally:
241 try: newFile.close()
242 except: pass
243 return newFilePath
244
245 def registerFileForCleanUp(self, fileName):
246 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000247
248 def test_basic(self):
249 # Make sure that a local file just gets its own location returned and
250 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000251 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000252 self.assertEqual(result[0], support.TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000253 self.assertTrue(isinstance(result[1], email.message.Message),
Barry Warsaw820c1202008-06-12 04:06:45 +0000254 "did not get a email.message.Message instance as second "
Brett Cannon19691362003-04-29 05:08:06 +0000255 "returned value")
256
257 def test_copy(self):
258 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000259 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000260 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000261 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000262 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000263 self.assertEqual(second_temp, result[0])
Georg Brandlab91fde2009-08-13 08:51:18 +0000264 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000265 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000266 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000267 try:
268 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000269 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000270 finally:
271 try: FILE.close()
272 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000273 self.assertEqual(self.text, text)
274
275 def test_reporthook(self):
276 # Make sure that the reporthook works.
277 def hooktester(count, block_size, total_size, count_holder=[0]):
Georg Brandlab91fde2009-08-13 08:51:18 +0000278 self.assertTrue(isinstance(count, int))
279 self.assertTrue(isinstance(block_size, int))
280 self.assertTrue(isinstance(total_size, int))
Brett Cannon19691362003-04-29 05:08:06 +0000281 self.assertEqual(count, count_holder[0])
282 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000283 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000284 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000285 urllib.request.urlretrieve(
286 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000287 second_temp, hooktester)
288
289 def test_reporthook_0_bytes(self):
290 # Test on zero length file. Should call reporthook only 1 time.
291 report = []
292 def hooktester(count, block_size, total_size, _report=report):
293 _report.append((count, block_size, total_size))
294 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000295 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000296 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000297 self.assertEqual(len(report), 1)
298 self.assertEqual(report[0][2], 0)
299
300 def test_reporthook_5_bytes(self):
301 # Test on 5 byte file. Should call reporthook only 2 times (once when
302 # the "network connection" is established and once when the block is
303 # read). Since the block size is 8192 bytes, only one block read is
304 # required to read the entire file.
305 report = []
306 def hooktester(count, block_size, total_size, _report=report):
307 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000308 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000309 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000310 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000311 self.assertEqual(len(report), 2)
312 self.assertEqual(report[0][1], 8192)
313 self.assertEqual(report[0][2], 5)
314
315 def test_reporthook_8193_bytes(self):
316 # Test on 8193 byte file. Should call reporthook only 3 times (once
317 # when the "network connection" is established, once for the next 8192
318 # bytes, and once for the last byte).
319 report = []
320 def hooktester(count, block_size, total_size, _report=report):
321 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000322 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000323 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000324 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000325 self.assertEqual(len(report), 3)
326 self.assertEqual(report[0][1], 8192)
327 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000328
Brett Cannon74bfd702003-04-25 09:39:47 +0000329class QuotingTests(unittest.TestCase):
330 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000331
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000332 According to RFC 2396 (Uniform Resource Identifiers), to escape a
333 character you write it as '%' + <2 character US-ASCII hex value>.
334 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
335 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000336
337 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000338
Brett Cannon74bfd702003-04-25 09:39:47 +0000339 Reserved characters : ";/?:@&=+$,"
340 Have special meaning in URIs and must be escaped if not being used for
341 their special meaning
342 Data characters : letters, digits, and "-_.!~*'()"
343 Unreserved and do not need to be escaped; can be, though, if desired
344 Control characters : 0x00 - 0x1F, 0x7F
345 Have no use in URIs so must be escaped
346 space : 0x20
347 Must be escaped
348 Delimiters : '<>#%"'
349 Must be escaped
350 Unwise : "{}|\^[]`"
351 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000352
Brett Cannon74bfd702003-04-25 09:39:47 +0000353 """
354
355 def test_never_quote(self):
356 # Make sure quote() does not quote letters, digits, and "_,.-"
357 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
358 "abcdefghijklmnopqrstuvwxyz",
359 "0123456789",
360 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000361 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000362 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000363 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000364 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000365 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000366 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000367
368 def test_default_safe(self):
369 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000370 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000371
372 def test_safe(self):
373 # Test setting 'safe' parameter does what it should do
374 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000375 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000376 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000377 "using quote(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000378 result = urllib.parse.quote_plus(quote_by_default,
379 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000380 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000381 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000382 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000383 # Safe expressed as bytes rather than str
384 result = urllib.parse.quote(quote_by_default, safe=b"<>")
385 self.assertEqual(quote_by_default, result,
386 "using quote(): %r != %r" % (quote_by_default, result))
387 # "Safe" non-ASCII characters should have no effect
388 # (Since URIs are not allowed to have non-ASCII characters)
389 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
390 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
391 self.assertEqual(expect, result,
392 "using quote(): %r != %r" %
393 (expect, result))
394 # Same as above, but using a bytes rather than str
395 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
396 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
397 self.assertEqual(expect, result,
398 "using quote(): %r != %r" %
399 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000400
401 def test_default_quoting(self):
402 # Make sure all characters that should be quoted are by default sans
403 # space (separate test for that).
404 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
405 should_quote.append('<>#%"{}|\^[]`')
406 should_quote.append(chr(127)) # For 0x7F
407 should_quote = ''.join(should_quote)
408 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000409 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000410 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000411 "using quote(): "
412 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000413 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000414 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000415 self.assertEqual(hexescape(char), result,
416 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000417 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000418 (char, hexescape(char), result))
419 del should_quote
420 partial_quote = "ab[]cd"
421 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000422 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000423 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000424 "using quote(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000425 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000426 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000427
428 def test_quoting_space(self):
429 # Make sure quote() and quote_plus() handle spaces as specified in
430 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000431 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000432 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000433 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000434 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000435 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000436 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000437 given = "a b cd e f"
438 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000439 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000440 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000441 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000442 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000443 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000444 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000445 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000446
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000447 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000448 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000449 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000450 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000451 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000452 # Test with bytes
453 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
454 'alpha%2Bbeta+gamma')
455 # Test with safe bytes
456 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
457 'alpha+beta+gamma')
458
459 def test_quote_bytes(self):
460 # Bytes should quote directly to percent-encoded values
461 given = b"\xa2\xd8ab\xff"
462 expect = "%A2%D8ab%FF"
463 result = urllib.parse.quote(given)
464 self.assertEqual(expect, result,
465 "using quote(): %r != %r" % (expect, result))
466 # Encoding argument should raise type error on bytes input
467 self.assertRaises(TypeError, urllib.parse.quote, given,
468 encoding="latin-1")
469 # quote_from_bytes should work the same
470 result = urllib.parse.quote_from_bytes(given)
471 self.assertEqual(expect, result,
472 "using quote_from_bytes(): %r != %r"
473 % (expect, result))
474
475 def test_quote_with_unicode(self):
476 # Characters in Latin-1 range, encoded by default in UTF-8
477 given = "\xa2\xd8ab\xff"
478 expect = "%C2%A2%C3%98ab%C3%BF"
479 result = urllib.parse.quote(given)
480 self.assertEqual(expect, result,
481 "using quote(): %r != %r" % (expect, result))
482 # Characters in Latin-1 range, encoded by with None (default)
483 result = urllib.parse.quote(given, encoding=None, errors=None)
484 self.assertEqual(expect, result,
485 "using quote(): %r != %r" % (expect, result))
486 # Characters in Latin-1 range, encoded with Latin-1
487 given = "\xa2\xd8ab\xff"
488 expect = "%A2%D8ab%FF"
489 result = urllib.parse.quote(given, encoding="latin-1")
490 self.assertEqual(expect, result,
491 "using quote(): %r != %r" % (expect, result))
492 # Characters in BMP, encoded by default in UTF-8
493 given = "\u6f22\u5b57" # "Kanji"
494 expect = "%E6%BC%A2%E5%AD%97"
495 result = urllib.parse.quote(given)
496 self.assertEqual(expect, result,
497 "using quote(): %r != %r" % (expect, result))
498 # Characters in BMP, encoded with Latin-1
499 given = "\u6f22\u5b57"
500 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
501 encoding="latin-1")
502 # Characters in BMP, encoded with Latin-1, with replace error handling
503 given = "\u6f22\u5b57"
504 expect = "%3F%3F" # "??"
505 result = urllib.parse.quote(given, encoding="latin-1",
506 errors="replace")
507 self.assertEqual(expect, result,
508 "using quote(): %r != %r" % (expect, result))
509 # Characters in BMP, Latin-1, with xmlcharref error handling
510 given = "\u6f22\u5b57"
511 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
512 result = urllib.parse.quote(given, encoding="latin-1",
513 errors="xmlcharrefreplace")
514 self.assertEqual(expect, result,
515 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000516
Georg Brandlfaf41492009-05-26 18:31:11 +0000517 def test_quote_plus_with_unicode(self):
518 # Encoding (latin-1) test for quote_plus
519 given = "\xa2\xd8 \xff"
520 expect = "%A2%D8+%FF"
521 result = urllib.parse.quote_plus(given, encoding="latin-1")
522 self.assertEqual(expect, result,
523 "using quote_plus(): %r != %r" % (expect, result))
524 # Errors test for quote_plus
525 given = "ab\u6f22\u5b57 cd"
526 expect = "ab%3F%3F+cd"
527 result = urllib.parse.quote_plus(given, encoding="latin-1",
528 errors="replace")
529 self.assertEqual(expect, result,
530 "using quote_plus(): %r != %r" % (expect, result))
531
Brett Cannon74bfd702003-04-25 09:39:47 +0000532class UnquotingTests(unittest.TestCase):
533 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000534
Brett Cannon74bfd702003-04-25 09:39:47 +0000535 See the doc string for quoting_Tests for details on quoting and such.
536
537 """
538
539 def test_unquoting(self):
540 # Make sure unquoting of all ASCII values works
541 escape_list = []
542 for num in range(128):
543 given = hexescape(chr(num))
544 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000545 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000546 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000547 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000548 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000549 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000550 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000551 (expect, result))
552 escape_list.append(given)
553 escape_string = ''.join(escape_list)
554 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000555 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000556 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000557 "using unquote(): not all characters escaped: "
558 "%s" % result)
559
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000560 def test_unquoting_badpercent(self):
561 # Test unquoting on bad percent-escapes
562 given = '%xab'
563 expect = given
564 result = urllib.parse.unquote(given)
565 self.assertEqual(expect, result, "using unquote(): %r != %r"
566 % (expect, result))
567 given = '%x'
568 expect = given
569 result = urllib.parse.unquote(given)
570 self.assertEqual(expect, result, "using unquote(): %r != %r"
571 % (expect, result))
572 given = '%'
573 expect = given
574 result = urllib.parse.unquote(given)
575 self.assertEqual(expect, result, "using unquote(): %r != %r"
576 % (expect, result))
577 # unquote_to_bytes
578 given = '%xab'
579 expect = bytes(given, 'ascii')
580 result = urllib.parse.unquote_to_bytes(given)
581 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
582 % (expect, result))
583 given = '%x'
584 expect = bytes(given, 'ascii')
585 result = urllib.parse.unquote_to_bytes(given)
586 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
587 % (expect, result))
588 given = '%'
589 expect = bytes(given, 'ascii')
590 result = urllib.parse.unquote_to_bytes(given)
591 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
592 % (expect, result))
593
594 def test_unquoting_mixed_case(self):
595 # Test unquoting on mixed-case hex digits in the percent-escapes
596 given = '%Ab%eA'
597 expect = b'\xab\xea'
598 result = urllib.parse.unquote_to_bytes(given)
599 self.assertEqual(expect, result,
600 "using unquote_to_bytes(): %r != %r"
601 % (expect, result))
602
Brett Cannon74bfd702003-04-25 09:39:47 +0000603 def test_unquoting_parts(self):
604 # Make sure unquoting works when have non-quoted characters
605 # interspersed
606 given = 'ab%sd' % hexescape('c')
607 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000608 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000609 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000610 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000611 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000612 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000613 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000614
Brett Cannon74bfd702003-04-25 09:39:47 +0000615 def test_unquoting_plus(self):
616 # Test difference between unquote() and unquote_plus()
617 given = "are+there+spaces..."
618 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000619 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000620 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000621 "using unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000622 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000623 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000624 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000625 "using unquote_plus(): %r != %r" % (expect, result))
626
627 def test_unquote_to_bytes(self):
628 given = 'br%C3%BCckner_sapporo_20050930.doc'
629 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
630 result = urllib.parse.unquote_to_bytes(given)
631 self.assertEqual(expect, result,
632 "using unquote_to_bytes(): %r != %r"
633 % (expect, result))
634 # Test on a string with unescaped non-ASCII characters
635 # (Technically an invalid URI; expect those characters to be UTF-8
636 # encoded).
637 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
638 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
639 self.assertEqual(expect, result,
640 "using unquote_to_bytes(): %r != %r"
641 % (expect, result))
642 # Test with a bytes as input
643 given = b'%A2%D8ab%FF'
644 expect = b'\xa2\xd8ab\xff'
645 result = urllib.parse.unquote_to_bytes(given)
646 self.assertEqual(expect, result,
647 "using unquote_to_bytes(): %r != %r"
648 % (expect, result))
649 # Test with a bytes as input, with unescaped non-ASCII bytes
650 # (Technically an invalid URI; expect those bytes to be preserved)
651 given = b'%A2\xd8ab%FF'
652 expect = b'\xa2\xd8ab\xff'
653 result = urllib.parse.unquote_to_bytes(given)
654 self.assertEqual(expect, result,
655 "using unquote_to_bytes(): %r != %r"
656 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000657
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000658 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000659 # Characters in the Latin-1 range, encoded with UTF-8
660 given = 'br%C3%BCckner_sapporo_20050930.doc'
661 expect = 'br\u00fcckner_sapporo_20050930.doc'
662 result = urllib.parse.unquote(given)
663 self.assertEqual(expect, result,
664 "using unquote(): %r != %r" % (expect, result))
665 # Characters in the Latin-1 range, encoded with None (default)
666 result = urllib.parse.unquote(given, encoding=None, errors=None)
667 self.assertEqual(expect, result,
668 "using unquote(): %r != %r" % (expect, result))
669
670 # Characters in the Latin-1 range, encoded with Latin-1
671 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
672 encoding="latin-1")
673 expect = 'br\u00fcckner_sapporo_20050930.doc'
674 self.assertEqual(expect, result,
675 "using unquote(): %r != %r" % (expect, result))
676
677 # Characters in BMP, encoded with UTF-8
678 given = "%E6%BC%A2%E5%AD%97"
679 expect = "\u6f22\u5b57" # "Kanji"
680 result = urllib.parse.unquote(given)
681 self.assertEqual(expect, result,
682 "using unquote(): %r != %r" % (expect, result))
683
684 # Decode with UTF-8, invalid sequence
685 given = "%F3%B1"
686 expect = "\ufffd" # Replacement character
687 result = urllib.parse.unquote(given)
688 self.assertEqual(expect, result,
689 "using unquote(): %r != %r" % (expect, result))
690
691 # Decode with UTF-8, invalid sequence, replace errors
692 result = urllib.parse.unquote(given, errors="replace")
693 self.assertEqual(expect, result,
694 "using unquote(): %r != %r" % (expect, result))
695
696 # Decode with UTF-8, invalid sequence, ignoring errors
697 given = "%F3%B1"
698 expect = ""
699 result = urllib.parse.unquote(given, errors="ignore")
700 self.assertEqual(expect, result,
701 "using unquote(): %r != %r" % (expect, result))
702
703 # A mix of non-ASCII and percent-encoded characters, UTF-8
704 result = urllib.parse.unquote("\u6f22%C3%BC")
705 expect = '\u6f22\u00fc'
706 self.assertEqual(expect, result,
707 "using unquote(): %r != %r" % (expect, result))
708
709 # A mix of non-ASCII and percent-encoded characters, Latin-1
710 # (Note, the string contains non-Latin-1-representable characters)
711 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
712 expect = '\u6f22\u00fc'
713 self.assertEqual(expect, result,
714 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000715
Brett Cannon74bfd702003-04-25 09:39:47 +0000716class urlencode_Tests(unittest.TestCase):
717 """Tests for urlencode()"""
718
719 def help_inputtype(self, given, test_type):
720 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000721
Brett Cannon74bfd702003-04-25 09:39:47 +0000722 'given' must lead to only the pairs:
723 * 1st, 1
724 * 2nd, 2
725 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000726
Brett Cannon74bfd702003-04-25 09:39:47 +0000727 Test cannot assume anything about order. Docs make no guarantee and
728 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000729
Brett Cannon74bfd702003-04-25 09:39:47 +0000730 """
731 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000732 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000733 for expected in expect_somewhere:
Georg Brandlab91fde2009-08-13 08:51:18 +0000734 self.assertTrue(expected in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000735 "testing %s: %s not found in %s" %
736 (test_type, expected, result))
737 self.assertEqual(result.count('&'), 2,
738 "testing %s: expected 2 '&'s; got %s" %
739 (test_type, result.count('&')))
740 amp_location = result.index('&')
741 on_amp_left = result[amp_location - 1]
742 on_amp_right = result[amp_location + 1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000743 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000744 "testing %s: '&' not located in proper place in %s" %
745 (test_type, result))
746 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
747 "testing %s: "
748 "unexpected number of characters: %s != %s" %
749 (test_type, len(result), (5 * 3) + 2))
750
751 def test_using_mapping(self):
752 # Test passing in a mapping object as an argument.
753 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
754 "using dict as input type")
755
756 def test_using_sequence(self):
757 # Test passing in a sequence of two-item sequences as an argument.
758 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
759 "using sequence of two-item tuples as input")
760
761 def test_quoting(self):
762 # Make sure keys and values are quoted using quote_plus()
763 given = {"&":"="}
764 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000765 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000766 self.assertEqual(expect, result)
767 given = {"key name":"A bunch of pluses"}
768 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000769 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000770 self.assertEqual(expect, result)
771
772 def test_doseq(self):
773 # Test that passing True for 'doseq' parameter works correctly
774 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000775 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
776 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000777 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000778 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000779 for value in given["sequence"]:
780 expect = "sequence=%s" % value
Georg Brandlab91fde2009-08-13 08:51:18 +0000781 self.assertTrue(expect in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000782 "%s not found in %s" % (expect, result))
783 self.assertEqual(result.count('&'), 2,
784 "Expected 2 '&'s, got %s" % result.count('&'))
785
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000786 def test_empty_sequence(self):
787 self.assertEqual("", urllib.parse.urlencode({}))
788 self.assertEqual("", urllib.parse.urlencode([]))
789
790 def test_nonstring_values(self):
791 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
792 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
793
794 def test_nonstring_seq_values(self):
795 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
796 self.assertEqual("a=None&a=a",
797 urllib.parse.urlencode({"a": [None, "a"]}, True))
798 self.assertEqual("a=a&a=b",
799 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
800
Brett Cannon74bfd702003-04-25 09:39:47 +0000801class Pathname_Tests(unittest.TestCase):
802 """Test pathname2url() and url2pathname()"""
803
804 def test_basic(self):
805 # Make sure simple tests pass
806 expected_path = os.path.join("parts", "of", "a", "path")
807 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000808 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000809 self.assertEqual(expected_url, result,
810 "pathname2url() failed; %s != %s" %
811 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000812 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000813 self.assertEqual(expected_path, result,
814 "url2pathame() failed; %s != %s" %
815 (result, expected_path))
816
817 def test_quoting(self):
818 # Test automatic quoting and unquoting works for pathnam2url() and
819 # url2pathname() respectively
820 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000821 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
822 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000823 self.assertEqual(expect, result,
824 "pathname2url() failed; %s != %s" %
825 (expect, result))
826 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000827 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000828 self.assertEqual(expect, result,
829 "url2pathname() failed; %s != %s" %
830 (expect, result))
831 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000832 expect = "%s/using_quote" % urllib.parse.quote("make sure")
833 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000834 self.assertEqual(expect, result,
835 "pathname2url() failed; %s != %s" %
836 (expect, result))
837 given = "make+sure/using_unquote"
838 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000839 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000840 self.assertEqual(expect, result,
841 "url2pathname() failed; %s != %s" %
842 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000843
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000844class Utility_Tests(unittest.TestCase):
845 """Testcase to test the various utility functions in the urllib."""
846
847 def test_splitpasswd(self):
848 """Some of password examples are not sensible, but it is added to
849 confirming to RFC2617 and addressing issue4675.
850 """
851 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
852 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
853 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
854 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
855 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
856 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
857 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
858
Senthil Kumaran690ce9b2009-05-05 18:41:13 +0000859
860class URLopener_Tests(unittest.TestCase):
861 """Testcase to test the open method of URLopener class."""
862
863 def test_quoted_open(self):
864 class DummyURLopener(urllib.request.URLopener):
865 def open_spam(self, url):
866 return url
867
868 self.assertEqual(DummyURLopener().open(
869 'spam://example/ /'),'//example/%20/')
870
Guido van Rossume7ba4952007-06-06 23:52:48 +0000871# Just commented them out.
872# Can't really tell why keep failing in windows and sparc.
873# Everywhere else they work ok, but on those machines, someteimes
874# fail in one of the tests, sometimes in other. I have a linux, and
875# the tests go ok.
876# If anybody has one of the problematic enviroments, please help!
877# . Facundo
878#
879# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000880# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +0000881# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
882# serv.settimeout(3)
883# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
884# serv.bind(("", 9093))
885# serv.listen(5)
886# try:
887# conn, addr = serv.accept()
888# conn.send("1 Hola mundo\n")
889# cantdata = 0
890# while cantdata < 13:
891# data = conn.recv(13-cantdata)
892# cantdata += len(data)
893# time.sleep(.3)
894# conn.send("2 No more lines\n")
895# conn.close()
896# except socket.timeout:
897# pass
898# finally:
899# serv.close()
900# evt.set()
901#
902# class FTPWrapperTests(unittest.TestCase):
903#
904# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000905# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +0000906# ftplib.FTP.port = 9093
907# self.evt = threading.Event()
908# threading.Thread(target=server, args=(self.evt,)).start()
909# time.sleep(.1)
910#
911# def tearDown(self):
912# self.evt.wait()
913#
914# def testBasic(self):
915# # connects
916# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +0000917# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000918#
919# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000920# # global default timeout is ignored
921# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +0000922# self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000923# socket.setdefaulttimeout(30)
924# try:
925# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
926# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000927# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000928# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000929# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000930#
Georg Brandlf78e02b2008-06-10 17:40:04 +0000931# def testTimeoutDefault(self):
932# # global default timeout is used
933# import socket
Georg Brandlab91fde2009-08-13 08:51:18 +0000934# self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000935# socket.setdefaulttimeout(30)
936# try:
937# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
938# finally:
939# socket.setdefaulttimeout(None)
940# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
941# ftp.close()
942#
943# def testTimeoutValue(self):
944# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
945# timeout=30)
946# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
947# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000948
Skip Montanaro080c9972001-01-28 21:12:22 +0000949
950
Brett Cannon74bfd702003-04-25 09:39:47 +0000951def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000952 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000953 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000954 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000955 urlretrieve_FileTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000956 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000957 QuotingTests,
958 UnquotingTests,
959 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000960 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000961 Utility_Tests,
Senthil Kumaran690ce9b2009-05-05 18:41:13 +0000962 URLopener_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000963 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000964 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000965
966
967
968if __name__ == '__main__':
969 test_main()