blob: c16f30dc7d9166b108deb31c67044f4cf978ea44 [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__"):
Brett Cannon74bfd702003-04-25 09:39:47 +000066 self.assert_(hasattr(self.returned_obj, attr),
67 "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()
88 self.assert_(isinstance(file_num, int),
89 "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):
Barry Warsaw820c1202008-06-12 04:06:45 +0000100 self.assert_(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):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000120 # Save all proxy related env vars
121 self._saved_environ = dict([(k, v) for k, v in os.environ.items()
122 if k.lower().find('proxy') >= 0])
123 # Delete all proxy related env vars
124 for k in self._saved_environ:
125 del os.environ[k]
126
127 def tearDown(self):
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000128 # Restore all proxy related env vars
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000129 for k, v in self._saved_environ.items():
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000130 os.environ[k] = v
131
132 def test_getproxies_environment_keep_no_proxies(self):
Antoine Pitrouff96b2a2009-01-03 22:55:38 +0000133 try:
134 os.environ['NO_PROXY'] = 'localhost'
135 proxies = urllib.request.getproxies_environment()
136 # getproxies_environment use lowered case truncated (no '_proxy') keys
137 self.assertEquals('localhost', proxies['no'])
138 finally:
139 # The old value will be restored by tearDown, if applicable.
140 del os.environ['NO_PROXY']
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000141
142
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000143class urlopen_HttpTests(unittest.TestCase):
144 """Test urlopen() opening a fake http connection."""
145
146 def fakehttp(self, fakedata):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000147 class FakeSocket(io.BytesIO):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000148 def sendall(self, str): pass
Nick Coghlan598c3a82009-02-08 04:01:00 +0000149 def makefile(self, *args, **kwds):
150 return self
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000151 def read(self, amt=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000152 if self.closed: return b""
153 return io.BytesIO.read(self, amt)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000154 def readline(self, length=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000155 if self.closed: return b""
156 return io.BytesIO.readline(self, length)
Georg Brandl24420152008-05-26 16:32:26 +0000157 class FakeHTTPConnection(http.client.HTTPConnection):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000158 def connect(self):
159 self.sock = FakeSocket(fakedata)
Georg Brandl24420152008-05-26 16:32:26 +0000160 self._connection_class = http.client.HTTPConnection
161 http.client.HTTPConnection = FakeHTTPConnection
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000162
163 def unfakehttp(self):
Georg Brandl24420152008-05-26 16:32:26 +0000164 http.client.HTTPConnection = self._connection_class
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000165
166 def test_read(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000167 self.fakehttp(b"Hello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000168 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000169 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000170 self.assertEqual(fp.readline(), b"Hello!")
171 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000172 self.assertEqual(fp.geturl(), 'http://python.org/')
173 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000174 finally:
175 self.unfakehttp()
176
Christian Heimes57dddfb2008-01-02 18:30:52 +0000177 def test_read_bogus(self):
178 # urlopen() should raise IOError for many error codes.
179 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
180Date: Wed, 02 Jan 2008 03:03:54 GMT
181Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
182Connection: close
183Content-Type: text/html; charset=iso-8859-1
184''')
185 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000186 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000187 finally:
188 self.unfakehttp()
189
Guido van Rossumd8faa362007-04-27 19:54:29 +0000190 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000191 # urlopen() raises IOError if the underlying socket does not send any
192 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000193 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000195 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000196 finally:
197 self.unfakehttp()
198
Brett Cannon19691362003-04-29 05:08:06 +0000199class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000200 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000201
Brett Cannon19691362003-04-29 05:08:06 +0000202 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000203 # Create a list of temporary files. Each item in the list is a file
204 # name (absolute path or relative to the current working directory).
205 # All files in this list will be deleted in the tearDown method. Note,
206 # this only helps to makes sure temporary files get deleted, but it
207 # does nothing about trying to close files that may still be open. It
208 # is the responsibility of the developer to properly close files even
209 # when exceptional conditions occur.
210 self.tempFiles = []
211
Brett Cannon19691362003-04-29 05:08:06 +0000212 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000213 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000214 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000215 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000216 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000217 FILE.write(self.text)
218 FILE.close()
219 finally:
220 try: FILE.close()
221 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000222
223 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000224 # Delete the temporary files.
225 for each in self.tempFiles:
226 try: os.remove(each)
227 except: pass
228
229 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000230 return "file://%s" % urllib.request.pathname2url(
231 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000232
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000233 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000234 """Creates a new temporary file containing the specified data,
235 registers the file for deletion during the test fixture tear down, and
236 returns the absolute path of the file."""
237
238 newFd, newFilePath = tempfile.mkstemp()
239 try:
240 self.registerFileForCleanUp(newFilePath)
241 newFile = os.fdopen(newFd, "wb")
242 newFile.write(data)
243 newFile.close()
244 finally:
245 try: newFile.close()
246 except: pass
247 return newFilePath
248
249 def registerFileForCleanUp(self, fileName):
250 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000251
252 def test_basic(self):
253 # Make sure that a local file just gets its own location returned and
254 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000255 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000256 self.assertEqual(result[0], support.TESTFN)
Barry Warsaw820c1202008-06-12 04:06:45 +0000257 self.assert_(isinstance(result[1], email.message.Message),
258 "did not get a email.message.Message instance as second "
Brett Cannon19691362003-04-29 05:08:06 +0000259 "returned value")
260
261 def test_copy(self):
262 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000263 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000264 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000265 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000266 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000267 self.assertEqual(second_temp, result[0])
268 self.assert_(os.path.exists(second_temp), "copy of the file was not "
269 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000270 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000271 try:
272 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000273 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000274 finally:
275 try: FILE.close()
276 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000277 self.assertEqual(self.text, text)
278
279 def test_reporthook(self):
280 # Make sure that the reporthook works.
281 def hooktester(count, block_size, total_size, count_holder=[0]):
282 self.assert_(isinstance(count, int))
283 self.assert_(isinstance(block_size, int))
284 self.assert_(isinstance(total_size, int))
285 self.assertEqual(count, count_holder[0])
286 count_holder[0] = count_holder[0] + 1
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000287 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000288 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000289 urllib.request.urlretrieve(
290 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000291 second_temp, hooktester)
292
293 def test_reporthook_0_bytes(self):
294 # Test on zero length file. Should call reporthook only 1 time.
295 report = []
296 def hooktester(count, block_size, total_size, _report=report):
297 _report.append((count, block_size, total_size))
298 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000299 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000300 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000301 self.assertEqual(len(report), 1)
302 self.assertEqual(report[0][2], 0)
303
304 def test_reporthook_5_bytes(self):
305 # Test on 5 byte file. Should call reporthook only 2 times (once when
306 # the "network connection" is established and once when the block is
307 # read). Since the block size is 8192 bytes, only one block read is
308 # required to read the entire file.
309 report = []
310 def hooktester(count, block_size, total_size, _report=report):
311 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000312 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000313 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000314 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000315 self.assertEqual(len(report), 2)
316 self.assertEqual(report[0][1], 8192)
317 self.assertEqual(report[0][2], 5)
318
319 def test_reporthook_8193_bytes(self):
320 # Test on 8193 byte file. Should call reporthook only 3 times (once
321 # when the "network connection" is established, once for the next 8192
322 # bytes, and once for the last byte).
323 report = []
324 def hooktester(count, block_size, total_size, _report=report):
325 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000326 srcFileName = self.createNewTempFile(b"x" * 8193)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000327 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000328 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000329 self.assertEqual(len(report), 3)
330 self.assertEqual(report[0][1], 8192)
331 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000332
Brett Cannon74bfd702003-04-25 09:39:47 +0000333class QuotingTests(unittest.TestCase):
334 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000335
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000336 According to RFC 2396 (Uniform Resource Identifiers), to escape a
337 character you write it as '%' + <2 character US-ASCII hex value>.
338 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
339 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000340
341 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000342
Brett Cannon74bfd702003-04-25 09:39:47 +0000343 Reserved characters : ";/?:@&=+$,"
344 Have special meaning in URIs and must be escaped if not being used for
345 their special meaning
346 Data characters : letters, digits, and "-_.!~*'()"
347 Unreserved and do not need to be escaped; can be, though, if desired
348 Control characters : 0x00 - 0x1F, 0x7F
349 Have no use in URIs so must be escaped
350 space : 0x20
351 Must be escaped
352 Delimiters : '<>#%"'
353 Must be escaped
354 Unwise : "{}|\^[]`"
355 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000356
Brett Cannon74bfd702003-04-25 09:39:47 +0000357 """
358
359 def test_never_quote(self):
360 # Make sure quote() does not quote letters, digits, and "_,.-"
361 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
362 "abcdefghijklmnopqrstuvwxyz",
363 "0123456789",
364 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000365 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000366 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000367 "using quote(): %r != %r" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000368 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000369 self.assertEqual(do_not_quote, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000370 "using quote_plus(): %r != %r" % (do_not_quote, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000371
372 def test_default_safe(self):
373 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000374 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000375
376 def test_safe(self):
377 # Test setting 'safe' parameter does what it should do
378 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000379 result = urllib.parse.quote(quote_by_default, 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(): %r != %r" % (quote_by_default, result))
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000382 result = urllib.parse.quote_plus(quote_by_default,
383 safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000384 self.assertEqual(quote_by_default, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000385 "using quote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000386 (quote_by_default, result))
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000387 # Safe expressed as bytes rather than str
388 result = urllib.parse.quote(quote_by_default, safe=b"<>")
389 self.assertEqual(quote_by_default, result,
390 "using quote(): %r != %r" % (quote_by_default, result))
391 # "Safe" non-ASCII characters should have no effect
392 # (Since URIs are not allowed to have non-ASCII characters)
393 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
394 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
395 self.assertEqual(expect, result,
396 "using quote(): %r != %r" %
397 (expect, result))
398 # Same as above, but using a bytes rather than str
399 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
400 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
401 self.assertEqual(expect, result,
402 "using quote(): %r != %r" %
403 (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000404
405 def test_default_quoting(self):
406 # Make sure all characters that should be quoted are by default sans
407 # space (separate test for that).
408 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
409 should_quote.append('<>#%"{}|\^[]`')
410 should_quote.append(chr(127)) # For 0x7F
411 should_quote = ''.join(should_quote)
412 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000413 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000414 self.assertEqual(hexescape(char), result,
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000415 "using quote(): "
416 "%s should be escaped to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000417 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000418 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000419 self.assertEqual(hexescape(char), result,
420 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000421 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000422 (char, hexescape(char), result))
423 del should_quote
424 partial_quote = "ab[]cd"
425 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000426 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000427 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000428 "using quote(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000429 self.assertEqual(expected, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000430 "using quote_plus(): %r != %r" % (expected, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000431
432 def test_quoting_space(self):
433 # Make sure quote() and quote_plus() handle spaces as specified in
434 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000435 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000436 self.assertEqual(result, hexescape(' '),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000437 "using quote(): %r != %r" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000438 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000439 self.assertEqual(result, '+',
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000440 "using quote_plus(): %r != +" % result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000441 given = "a b cd e f"
442 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000443 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000444 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000445 "using quote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000446 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000447 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000448 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000449 "using quote_plus(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000450
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000451 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000452 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000453 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000454 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000455 'alpha+beta+gamma')
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000456 # Test with bytes
457 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
458 'alpha%2Bbeta+gamma')
459 # Test with safe bytes
460 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
461 'alpha+beta+gamma')
462
463 def test_quote_bytes(self):
464 # Bytes should quote directly to percent-encoded values
465 given = b"\xa2\xd8ab\xff"
466 expect = "%A2%D8ab%FF"
467 result = urllib.parse.quote(given)
468 self.assertEqual(expect, result,
469 "using quote(): %r != %r" % (expect, result))
470 # Encoding argument should raise type error on bytes input
471 self.assertRaises(TypeError, urllib.parse.quote, given,
472 encoding="latin-1")
473 # quote_from_bytes should work the same
474 result = urllib.parse.quote_from_bytes(given)
475 self.assertEqual(expect, result,
476 "using quote_from_bytes(): %r != %r"
477 % (expect, result))
478
479 def test_quote_with_unicode(self):
480 # Characters in Latin-1 range, encoded by default in UTF-8
481 given = "\xa2\xd8ab\xff"
482 expect = "%C2%A2%C3%98ab%C3%BF"
483 result = urllib.parse.quote(given)
484 self.assertEqual(expect, result,
485 "using quote(): %r != %r" % (expect, result))
486 # Characters in Latin-1 range, encoded by with None (default)
487 result = urllib.parse.quote(given, encoding=None, errors=None)
488 self.assertEqual(expect, result,
489 "using quote(): %r != %r" % (expect, result))
490 # Characters in Latin-1 range, encoded with Latin-1
491 given = "\xa2\xd8ab\xff"
492 expect = "%A2%D8ab%FF"
493 result = urllib.parse.quote(given, encoding="latin-1")
494 self.assertEqual(expect, result,
495 "using quote(): %r != %r" % (expect, result))
496 # Characters in BMP, encoded by default in UTF-8
497 given = "\u6f22\u5b57" # "Kanji"
498 expect = "%E6%BC%A2%E5%AD%97"
499 result = urllib.parse.quote(given)
500 self.assertEqual(expect, result,
501 "using quote(): %r != %r" % (expect, result))
502 # Characters in BMP, encoded with Latin-1
503 given = "\u6f22\u5b57"
504 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
505 encoding="latin-1")
506 # Characters in BMP, encoded with Latin-1, with replace error handling
507 given = "\u6f22\u5b57"
508 expect = "%3F%3F" # "??"
509 result = urllib.parse.quote(given, encoding="latin-1",
510 errors="replace")
511 self.assertEqual(expect, result,
512 "using quote(): %r != %r" % (expect, result))
513 # Characters in BMP, Latin-1, with xmlcharref error handling
514 given = "\u6f22\u5b57"
515 expect = "%26%2328450%3B%26%2323383%3B" # "&#28450;&#23383;"
516 result = urllib.parse.quote(given, encoding="latin-1",
517 errors="xmlcharrefreplace")
518 self.assertEqual(expect, result,
519 "using quote(): %r != %r" % (expect, result))
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000520
Brett Cannon74bfd702003-04-25 09:39:47 +0000521class UnquotingTests(unittest.TestCase):
522 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000523
Brett Cannon74bfd702003-04-25 09:39:47 +0000524 See the doc string for quoting_Tests for details on quoting and such.
525
526 """
527
528 def test_unquoting(self):
529 # Make sure unquoting of all ASCII values works
530 escape_list = []
531 for num in range(128):
532 given = hexescape(chr(num))
533 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000534 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000535 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000536 "using unquote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000537 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000538 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000539 "using unquote_plus(): %r != %r" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000540 (expect, result))
541 escape_list.append(given)
542 escape_string = ''.join(escape_list)
543 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000544 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000545 self.assertEqual(result.count('%'), 1,
Brett Cannon74bfd702003-04-25 09:39:47 +0000546 "using unquote(): not all characters escaped: "
547 "%s" % result)
548
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000549 def test_unquoting_badpercent(self):
550 # Test unquoting on bad percent-escapes
551 given = '%xab'
552 expect = given
553 result = urllib.parse.unquote(given)
554 self.assertEqual(expect, result, "using unquote(): %r != %r"
555 % (expect, result))
556 given = '%x'
557 expect = given
558 result = urllib.parse.unquote(given)
559 self.assertEqual(expect, result, "using unquote(): %r != %r"
560 % (expect, result))
561 given = '%'
562 expect = given
563 result = urllib.parse.unquote(given)
564 self.assertEqual(expect, result, "using unquote(): %r != %r"
565 % (expect, result))
566 # unquote_to_bytes
567 given = '%xab'
568 expect = bytes(given, 'ascii')
569 result = urllib.parse.unquote_to_bytes(given)
570 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
571 % (expect, result))
572 given = '%x'
573 expect = bytes(given, 'ascii')
574 result = urllib.parse.unquote_to_bytes(given)
575 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
576 % (expect, result))
577 given = '%'
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
583 def test_unquoting_mixed_case(self):
584 # Test unquoting on mixed-case hex digits in the percent-escapes
585 given = '%Ab%eA'
586 expect = b'\xab\xea'
587 result = urllib.parse.unquote_to_bytes(given)
588 self.assertEqual(expect, result,
589 "using unquote_to_bytes(): %r != %r"
590 % (expect, result))
591
Brett Cannon74bfd702003-04-25 09:39:47 +0000592 def test_unquoting_parts(self):
593 # Make sure unquoting works when have non-quoted characters
594 # interspersed
595 given = 'ab%sd' % hexescape('c')
596 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000597 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000598 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000599 "using quote(): %r != %r" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000600 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000601 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000602 "using unquote_plus(): %r != %r" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000603
Brett Cannon74bfd702003-04-25 09:39:47 +0000604 def test_unquoting_plus(self):
605 # Test difference between unquote() and unquote_plus()
606 given = "are+there+spaces..."
607 expect = given
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 unquote(): %r != %r" % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000611 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000612 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000613 self.assertEqual(expect, result,
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000614 "using unquote_plus(): %r != %r" % (expect, result))
615
616 def test_unquote_to_bytes(self):
617 given = 'br%C3%BCckner_sapporo_20050930.doc'
618 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
619 result = urllib.parse.unquote_to_bytes(given)
620 self.assertEqual(expect, result,
621 "using unquote_to_bytes(): %r != %r"
622 % (expect, result))
623 # Test on a string with unescaped non-ASCII characters
624 # (Technically an invalid URI; expect those characters to be UTF-8
625 # encoded).
626 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
627 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"
628 self.assertEqual(expect, result,
629 "using unquote_to_bytes(): %r != %r"
630 % (expect, result))
631 # Test with a bytes as input
632 given = b'%A2%D8ab%FF'
633 expect = b'\xa2\xd8ab\xff'
634 result = urllib.parse.unquote_to_bytes(given)
635 self.assertEqual(expect, result,
636 "using unquote_to_bytes(): %r != %r"
637 % (expect, result))
638 # Test with a bytes as input, with unescaped non-ASCII bytes
639 # (Technically an invalid URI; expect those bytes to be preserved)
640 given = b'%A2\xd8ab%FF'
641 expect = b'\xa2\xd8ab\xff'
642 result = urllib.parse.unquote_to_bytes(given)
643 self.assertEqual(expect, result,
644 "using unquote_to_bytes(): %r != %r"
645 % (expect, result))
Brett Cannon74bfd702003-04-25 09:39:47 +0000646
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000647 def test_unquote_with_unicode(self):
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000648 # Characters in the Latin-1 range, encoded with UTF-8
649 given = 'br%C3%BCckner_sapporo_20050930.doc'
650 expect = 'br\u00fcckner_sapporo_20050930.doc'
651 result = urllib.parse.unquote(given)
652 self.assertEqual(expect, result,
653 "using unquote(): %r != %r" % (expect, result))
654 # Characters in the Latin-1 range, encoded with None (default)
655 result = urllib.parse.unquote(given, encoding=None, errors=None)
656 self.assertEqual(expect, result,
657 "using unquote(): %r != %r" % (expect, result))
658
659 # Characters in the Latin-1 range, encoded with Latin-1
660 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
661 encoding="latin-1")
662 expect = 'br\u00fcckner_sapporo_20050930.doc'
663 self.assertEqual(expect, result,
664 "using unquote(): %r != %r" % (expect, result))
665
666 # Characters in BMP, encoded with UTF-8
667 given = "%E6%BC%A2%E5%AD%97"
668 expect = "\u6f22\u5b57" # "Kanji"
669 result = urllib.parse.unquote(given)
670 self.assertEqual(expect, result,
671 "using unquote(): %r != %r" % (expect, result))
672
673 # Decode with UTF-8, invalid sequence
674 given = "%F3%B1"
675 expect = "\ufffd" # Replacement character
676 result = urllib.parse.unquote(given)
677 self.assertEqual(expect, result,
678 "using unquote(): %r != %r" % (expect, result))
679
680 # Decode with UTF-8, invalid sequence, replace errors
681 result = urllib.parse.unquote(given, errors="replace")
682 self.assertEqual(expect, result,
683 "using unquote(): %r != %r" % (expect, result))
684
685 # Decode with UTF-8, invalid sequence, ignoring errors
686 given = "%F3%B1"
687 expect = ""
688 result = urllib.parse.unquote(given, errors="ignore")
689 self.assertEqual(expect, result,
690 "using unquote(): %r != %r" % (expect, result))
691
692 # A mix of non-ASCII and percent-encoded characters, UTF-8
693 result = urllib.parse.unquote("\u6f22%C3%BC")
694 expect = '\u6f22\u00fc'
695 self.assertEqual(expect, result,
696 "using unquote(): %r != %r" % (expect, result))
697
698 # A mix of non-ASCII and percent-encoded characters, Latin-1
699 # (Note, the string contains non-Latin-1-representable characters)
700 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
701 expect = '\u6f22\u00fc'
702 self.assertEqual(expect, result,
703 "using unquote(): %r != %r" % (expect, result))
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000704
Brett Cannon74bfd702003-04-25 09:39:47 +0000705class urlencode_Tests(unittest.TestCase):
706 """Tests for urlencode()"""
707
708 def help_inputtype(self, given, test_type):
709 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000710
Brett Cannon74bfd702003-04-25 09:39:47 +0000711 'given' must lead to only the pairs:
712 * 1st, 1
713 * 2nd, 2
714 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000715
Brett Cannon74bfd702003-04-25 09:39:47 +0000716 Test cannot assume anything about order. Docs make no guarantee and
717 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000718
Brett Cannon74bfd702003-04-25 09:39:47 +0000719 """
720 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000721 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000722 for expected in expect_somewhere:
723 self.assert_(expected in result,
724 "testing %s: %s not found in %s" %
725 (test_type, expected, result))
726 self.assertEqual(result.count('&'), 2,
727 "testing %s: expected 2 '&'s; got %s" %
728 (test_type, result.count('&')))
729 amp_location = result.index('&')
730 on_amp_left = result[amp_location - 1]
731 on_amp_right = result[amp_location + 1]
732 self.assert_(on_amp_left.isdigit() and on_amp_right.isdigit(),
733 "testing %s: '&' not located in proper place in %s" %
734 (test_type, result))
735 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
736 "testing %s: "
737 "unexpected number of characters: %s != %s" %
738 (test_type, len(result), (5 * 3) + 2))
739
740 def test_using_mapping(self):
741 # Test passing in a mapping object as an argument.
742 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
743 "using dict as input type")
744
745 def test_using_sequence(self):
746 # Test passing in a sequence of two-item sequences as an argument.
747 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
748 "using sequence of two-item tuples as input")
749
750 def test_quoting(self):
751 # Make sure keys and values are quoted using quote_plus()
752 given = {"&":"="}
753 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000754 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000755 self.assertEqual(expect, result)
756 given = {"key name":"A bunch of pluses"}
757 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000758 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000759 self.assertEqual(expect, result)
760
761 def test_doseq(self):
762 # Test that passing True for 'doseq' parameter works correctly
763 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000764 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
765 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000766 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000767 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000768 for value in given["sequence"]:
769 expect = "sequence=%s" % value
770 self.assert_(expect in result,
771 "%s not found in %s" % (expect, result))
772 self.assertEqual(result.count('&'), 2,
773 "Expected 2 '&'s, got %s" % result.count('&'))
774
Jeremy Hylton1ef7c6b2009-03-26 16:57:30 +0000775 def test_empty_sequence(self):
776 self.assertEqual("", urllib.parse.urlencode({}))
777 self.assertEqual("", urllib.parse.urlencode([]))
778
779 def test_nonstring_values(self):
780 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
781 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))
782
783 def test_nonstring_seq_values(self):
784 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
785 self.assertEqual("a=None&a=a",
786 urllib.parse.urlencode({"a": [None, "a"]}, True))
787 self.assertEqual("a=a&a=b",
788 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
789
Brett Cannon74bfd702003-04-25 09:39:47 +0000790class Pathname_Tests(unittest.TestCase):
791 """Test pathname2url() and url2pathname()"""
792
793 def test_basic(self):
794 # Make sure simple tests pass
795 expected_path = os.path.join("parts", "of", "a", "path")
796 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000797 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000798 self.assertEqual(expected_url, result,
799 "pathname2url() failed; %s != %s" %
800 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000801 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000802 self.assertEqual(expected_path, result,
803 "url2pathame() failed; %s != %s" %
804 (result, expected_path))
805
806 def test_quoting(self):
807 # Test automatic quoting and unquoting works for pathnam2url() and
808 # url2pathname() respectively
809 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000810 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
811 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000812 self.assertEqual(expect, result,
813 "pathname2url() failed; %s != %s" %
814 (expect, result))
815 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000816 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000817 self.assertEqual(expect, result,
818 "url2pathname() failed; %s != %s" %
819 (expect, result))
820 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000821 expect = "%s/using_quote" % urllib.parse.quote("make sure")
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 given = "make+sure/using_unquote"
827 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000828 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000829 self.assertEqual(expect, result,
830 "url2pathname() failed; %s != %s" %
831 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000832
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000833class Utility_Tests(unittest.TestCase):
834 """Testcase to test the various utility functions in the urllib."""
835
836 def test_splitpasswd(self):
837 """Some of password examples are not sensible, but it is added to
838 confirming to RFC2617 and addressing issue4675.
839 """
840 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))
841 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))
842 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))
843 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))
844 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
845 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
846 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
847
Guido van Rossume7ba4952007-06-06 23:52:48 +0000848# Just commented them out.
849# Can't really tell why keep failing in windows and sparc.
850# Everywhere else they work ok, but on those machines, someteimes
851# fail in one of the tests, sometimes in other. I have a linux, and
852# the tests go ok.
853# If anybody has one of the problematic enviroments, please help!
854# . Facundo
855#
856# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000857# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +0000858# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
859# serv.settimeout(3)
860# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
861# serv.bind(("", 9093))
862# serv.listen(5)
863# try:
864# conn, addr = serv.accept()
865# conn.send("1 Hola mundo\n")
866# cantdata = 0
867# while cantdata < 13:
868# data = conn.recv(13-cantdata)
869# cantdata += len(data)
870# time.sleep(.3)
871# conn.send("2 No more lines\n")
872# conn.close()
873# except socket.timeout:
874# pass
875# finally:
876# serv.close()
877# evt.set()
878#
879# class FTPWrapperTests(unittest.TestCase):
880#
881# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000882# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +0000883# ftplib.FTP.port = 9093
884# self.evt = threading.Event()
885# threading.Thread(target=server, args=(self.evt,)).start()
886# time.sleep(.1)
887#
888# def tearDown(self):
889# self.evt.wait()
890#
891# def testBasic(self):
892# # connects
893# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +0000894# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000895#
896# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000897# # global default timeout is ignored
898# import socket
899# self.assert_(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000900# socket.setdefaulttimeout(30)
901# try:
902# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
903# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000904# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000905# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000906# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000907#
Georg Brandlf78e02b2008-06-10 17:40:04 +0000908# def testTimeoutDefault(self):
909# # global default timeout is used
910# import socket
911# self.assert_(socket.getdefaulttimeout() is None)
912# socket.setdefaulttimeout(30)
913# try:
914# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
915# finally:
916# socket.setdefaulttimeout(None)
917# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
918# ftp.close()
919#
920# def testTimeoutValue(self):
921# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
922# timeout=30)
923# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
924# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000925
Skip Montanaro080c9972001-01-28 21:12:22 +0000926
927
Brett Cannon74bfd702003-04-25 09:39:47 +0000928def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000929 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000930 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000931 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000932 urlretrieve_FileTests,
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000933 ProxyTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000934 QuotingTests,
935 UnquotingTests,
936 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 Pathname_Tests,
Senthil Kumaraneaaec272009-03-30 21:54:41 +0000938 Utility_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000939 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000940 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000941
942
943
944if __name__ == '__main__':
945 test_main()