blob: f5a9d5d20eec54e6563523aaa50131b2278b7c08 [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
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000116class urlopen_HttpTests(unittest.TestCase):
117 """Test urlopen() opening a fake http connection."""
118
119 def fakehttp(self, fakedata):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000120 class FakeSocket(io.BytesIO):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000121 def sendall(self, str): pass
122 def makefile(self, mode, name): return self
123 def read(self, amt=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000124 if self.closed: return b""
125 return io.BytesIO.read(self, amt)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000126 def readline(self, length=None):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000127 if self.closed: return b""
128 return io.BytesIO.readline(self, length)
Georg Brandl24420152008-05-26 16:32:26 +0000129 class FakeHTTPConnection(http.client.HTTPConnection):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000130 def connect(self):
131 self.sock = FakeSocket(fakedata)
Georg Brandl24420152008-05-26 16:32:26 +0000132 self._connection_class = http.client.HTTPConnection
133 http.client.HTTPConnection = FakeHTTPConnection
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000134
135 def unfakehttp(self):
Georg Brandl24420152008-05-26 16:32:26 +0000136 http.client.HTTPConnection = self._connection_class
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000137
138 def test_read(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000139 self.fakehttp(b"Hello!")
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000140 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000141 fp = urlopen("http://python.org/")
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000142 self.assertEqual(fp.readline(), b"Hello!")
143 self.assertEqual(fp.readline(), b"")
Christian Heimes9bd667a2008-01-20 15:14:11 +0000144 self.assertEqual(fp.geturl(), 'http://python.org/')
145 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000146 finally:
147 self.unfakehttp()
148
Christian Heimes57dddfb2008-01-02 18:30:52 +0000149 def test_read_bogus(self):
150 # urlopen() should raise IOError for many error codes.
151 self.fakehttp(b'''HTTP/1.1 401 Authentication Required
152Date: Wed, 02 Jan 2008 03:03:54 GMT
153Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
154Connection: close
155Content-Type: text/html; charset=iso-8859-1
156''')
157 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000158 self.assertRaises(IOError, urlopen, "http://python.org/")
Christian Heimes57dddfb2008-01-02 18:30:52 +0000159 finally:
160 self.unfakehttp()
161
Guido van Rossumd8faa362007-04-27 19:54:29 +0000162 def test_empty_socket(self):
Jeremy Hylton66dc8c52007-08-04 03:42:26 +0000163 # urlopen() raises IOError if the underlying socket does not send any
164 # data. (#1680230)
Christian Heimes57dddfb2008-01-02 18:30:52 +0000165 self.fakehttp(b'')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166 try:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000167 self.assertRaises(IOError, urlopen, "http://something")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000168 finally:
169 self.unfakehttp()
170
Brett Cannon19691362003-04-29 05:08:06 +0000171class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000172 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000173
Brett Cannon19691362003-04-29 05:08:06 +0000174 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000175 # Create a list of temporary files. Each item in the list is a file
176 # name (absolute path or relative to the current working directory).
177 # All files in this list will be deleted in the tearDown method. Note,
178 # this only helps to makes sure temporary files get deleted, but it
179 # does nothing about trying to close files that may still be open. It
180 # is the responsibility of the developer to properly close files even
181 # when exceptional conditions occur.
182 self.tempFiles = []
183
Brett Cannon19691362003-04-29 05:08:06 +0000184 # Create a temporary file.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000185 self.registerFileForCleanUp(support.TESTFN)
Guido van Rossuma0982942007-07-10 08:30:03 +0000186 self.text = b'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000187 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000188 FILE = open(support.TESTFN, 'wb')
Georg Brandl5a650a22005-08-26 08:51:34 +0000189 FILE.write(self.text)
190 FILE.close()
191 finally:
192 try: FILE.close()
193 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000194
195 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000196 # Delete the temporary files.
197 for each in self.tempFiles:
198 try: os.remove(each)
199 except: pass
200
201 def constructLocalFileUrl(self, filePath):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000202 return "file://%s" % urllib.request.pathname2url(
203 os.path.abspath(filePath))
Georg Brandl5a650a22005-08-26 08:51:34 +0000204
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000205 def createNewTempFile(self, data=b""):
Georg Brandl5a650a22005-08-26 08:51:34 +0000206 """Creates a new temporary file containing the specified data,
207 registers the file for deletion during the test fixture tear down, and
208 returns the absolute path of the file."""
209
210 newFd, newFilePath = tempfile.mkstemp()
211 try:
212 self.registerFileForCleanUp(newFilePath)
213 newFile = os.fdopen(newFd, "wb")
214 newFile.write(data)
215 newFile.close()
216 finally:
217 try: newFile.close()
218 except: pass
219 return newFilePath
220
221 def registerFileForCleanUp(self, fileName):
222 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000223
224 def test_basic(self):
225 # Make sure that a local file just gets its own location returned and
226 # a headers value is returned.
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000227 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000228 self.assertEqual(result[0], support.TESTFN)
Barry Warsaw820c1202008-06-12 04:06:45 +0000229 self.assert_(isinstance(result[1], email.message.Message),
230 "did not get a email.message.Message instance as second "
Brett Cannon19691362003-04-29 05:08:06 +0000231 "returned value")
232
233 def test_copy(self):
234 # Test that setting the filename argument works.
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000235 second_temp = "%s.2" % support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000236 self.registerFileForCleanUp(second_temp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000237 result = urllib.request.urlretrieve(self.constructLocalFileUrl(
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000238 support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000239 self.assertEqual(second_temp, result[0])
240 self.assert_(os.path.exists(second_temp), "copy of the file was not "
241 "made")
Alex Martelli01c77c62006-08-24 02:58:11 +0000242 FILE = open(second_temp, 'rb')
Brett Cannon19691362003-04-29 05:08:06 +0000243 try:
244 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000245 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000246 finally:
247 try: FILE.close()
248 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000249 self.assertEqual(self.text, text)
250
251 def test_reporthook(self):
252 # Make sure that the reporthook works.
253 def hooktester(count, block_size, total_size, count_holder=[0]):
254 self.assert_(isinstance(count, int))
255 self.assert_(isinstance(block_size, int))
256 self.assert_(isinstance(total_size, int))
257 self.assertEqual(count, count_holder[0])
258 count_holder[0] = count_holder[0] + 1
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 urllib.request.urlretrieve(
262 self.constructLocalFileUrl(support.TESTFN),
Georg Brandl5a650a22005-08-26 08:51:34 +0000263 second_temp, hooktester)
264
265 def test_reporthook_0_bytes(self):
266 # Test on zero length file. Should call reporthook only 1 time.
267 report = []
268 def hooktester(count, block_size, total_size, _report=report):
269 _report.append((count, block_size, total_size))
270 srcFileName = self.createNewTempFile()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000271 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000272 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000273 self.assertEqual(len(report), 1)
274 self.assertEqual(report[0][2], 0)
275
276 def test_reporthook_5_bytes(self):
277 # Test on 5 byte file. Should call reporthook only 2 times (once when
278 # the "network connection" is established and once when the block is
279 # read). Since the block size is 8192 bytes, only one block read is
280 # required to read the entire file.
281 report = []
282 def hooktester(count, block_size, total_size, _report=report):
283 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000284 srcFileName = self.createNewTempFile(b"x" * 5)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000285 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000286 support.TESTFN, hooktester)
Georg Brandl5a650a22005-08-26 08:51:34 +0000287 self.assertEqual(len(report), 2)
288 self.assertEqual(report[0][1], 8192)
289 self.assertEqual(report[0][2], 5)
290
291 def test_reporthook_8193_bytes(self):
292 # Test on 8193 byte file. Should call reporthook only 3 times (once
293 # when the "network connection" is established, once for the next 8192
294 # bytes, and once for the last byte).
295 report = []
296 def hooktester(count, block_size, total_size, _report=report):
297 _report.append((count, block_size, total_size))
Guido van Rossum70d0dda2007-08-29 01:53:26 +0000298 srcFileName = self.createNewTempFile(b"x" * 8193)
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), 3)
302 self.assertEqual(report[0][1], 8192)
303 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000304
Brett Cannon74bfd702003-04-25 09:39:47 +0000305class QuotingTests(unittest.TestCase):
306 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000307
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000308 According to RFC 2396 (Uniform Resource Identifiers), to escape a
309 character you write it as '%' + <2 character US-ASCII hex value>.
310 The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
311 character properly. Case does not matter on the hex letters.
Brett Cannon74bfd702003-04-25 09:39:47 +0000312
313 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000314
Brett Cannon74bfd702003-04-25 09:39:47 +0000315 Reserved characters : ";/?:@&=+$,"
316 Have special meaning in URIs and must be escaped if not being used for
317 their special meaning
318 Data characters : letters, digits, and "-_.!~*'()"
319 Unreserved and do not need to be escaped; can be, though, if desired
320 Control characters : 0x00 - 0x1F, 0x7F
321 Have no use in URIs so must be escaped
322 space : 0x20
323 Must be escaped
324 Delimiters : '<>#%"'
325 Must be escaped
326 Unwise : "{}|\^[]`"
327 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000328
Brett Cannon74bfd702003-04-25 09:39:47 +0000329 """
330
331 def test_never_quote(self):
332 # Make sure quote() does not quote letters, digits, and "_,.-"
333 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
334 "abcdefghijklmnopqrstuvwxyz",
335 "0123456789",
336 "_.-"])
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000337 result = urllib.parse.quote(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000338 self.assertEqual(do_not_quote, result,
339 "using quote(): %s != %s" % (do_not_quote, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000340 result = urllib.parse.quote_plus(do_not_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000341 self.assertEqual(do_not_quote, result,
342 "using quote_plus(): %s != %s" % (do_not_quote, result))
343
344 def test_default_safe(self):
345 # Test '/' is default value for 'safe' parameter
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000346 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')
Brett Cannon74bfd702003-04-25 09:39:47 +0000347
348 def test_safe(self):
349 # Test setting 'safe' parameter does what it should do
350 quote_by_default = "<>"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000351 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000352 self.assertEqual(quote_by_default, result,
353 "using quote(): %s != %s" % (quote_by_default, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000354 result = urllib.parse.quote_plus(quote_by_default, safe=quote_by_default)
Brett Cannon74bfd702003-04-25 09:39:47 +0000355 self.assertEqual(quote_by_default, result,
356 "using quote_plus(): %s != %s" %
357 (quote_by_default, result))
358
359 def test_default_quoting(self):
360 # Make sure all characters that should be quoted are by default sans
361 # space (separate test for that).
362 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
363 should_quote.append('<>#%"{}|\^[]`')
364 should_quote.append(chr(127)) # For 0x7F
365 should_quote = ''.join(should_quote)
366 for char in should_quote:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000367 result = urllib.parse.quote(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000368 self.assertEqual(hexescape(char), result,
369 "using quote(): %s should be escaped to %s, not %s" %
370 (char, hexescape(char), result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000371 result = urllib.parse.quote_plus(char)
Brett Cannon74bfd702003-04-25 09:39:47 +0000372 self.assertEqual(hexescape(char), result,
373 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000374 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000375 (char, hexescape(char), result))
376 del should_quote
377 partial_quote = "ab[]cd"
378 expected = "ab%5B%5Dcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000379 result = urllib.parse.quote(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000380 self.assertEqual(expected, result,
381 "using quote(): %s != %s" % (expected, result))
382 self.assertEqual(expected, result,
383 "using quote_plus(): %s != %s" % (expected, result))
384
385 def test_quoting_space(self):
386 # Make sure quote() and quote_plus() handle spaces as specified in
387 # their unique way
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000388 result = urllib.parse.quote(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000389 self.assertEqual(result, hexescape(' '),
390 "using quote(): %s != %s" % (result, hexescape(' ')))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000391 result = urllib.parse.quote_plus(' ')
Brett Cannon74bfd702003-04-25 09:39:47 +0000392 self.assertEqual(result, '+',
393 "using quote_plus(): %s != +" % result)
394 given = "a b cd e f"
395 expect = given.replace(' ', hexescape(' '))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000396 result = urllib.parse.quote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000397 self.assertEqual(expect, result,
398 "using quote(): %s != %s" % (expect, result))
399 expect = given.replace(' ', '+')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000400 result = urllib.parse.quote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000401 self.assertEqual(expect, result,
402 "using quote_plus(): %s != %s" % (expect, result))
403
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000404 def test_quoting_plus(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000405 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000406 'alpha%2Bbeta+gamma')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000407 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000408 'alpha+beta+gamma')
409
Brett Cannon74bfd702003-04-25 09:39:47 +0000410class UnquotingTests(unittest.TestCase):
411 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000412
Brett Cannon74bfd702003-04-25 09:39:47 +0000413 See the doc string for quoting_Tests for details on quoting and such.
414
415 """
416
417 def test_unquoting(self):
418 # Make sure unquoting of all ASCII values works
419 escape_list = []
420 for num in range(128):
421 given = hexescape(chr(num))
422 expect = chr(num)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000423 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000424 self.assertEqual(expect, result,
425 "using unquote(): %s != %s" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000426 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000427 self.assertEqual(expect, result,
428 "using unquote_plus(): %s != %s" %
429 (expect, result))
430 escape_list.append(given)
431 escape_string = ''.join(escape_list)
432 del escape_list
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000433 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000434 self.assertEqual(result.count('%'), 1,
435 "using quote(): not all characters escaped; %s" %
436 result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000437 result = urllib.parse.unquote(escape_string)
Brett Cannon74bfd702003-04-25 09:39:47 +0000438 self.assertEqual(result.count('%'), 1,
439 "using unquote(): not all characters escaped: "
440 "%s" % result)
441
442 def test_unquoting_parts(self):
443 # Make sure unquoting works when have non-quoted characters
444 # interspersed
445 given = 'ab%sd' % hexescape('c')
446 expect = "abcd"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000447 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000448 self.assertEqual(expect, result,
449 "using quote(): %s != %s" % (expect, result))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000450 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000451 self.assertEqual(expect, result,
452 "using unquote_plus(): %s != %s" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000453
Brett Cannon74bfd702003-04-25 09:39:47 +0000454 def test_unquoting_plus(self):
455 # Test difference between unquote() and unquote_plus()
456 given = "are+there+spaces..."
457 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000458 result = urllib.parse.unquote(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000459 self.assertEqual(expect, result,
460 "using unquote(): %s != %s" % (expect, result))
461 expect = given.replace('+', ' ')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000462 result = urllib.parse.unquote_plus(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000463 self.assertEqual(expect, result,
464 "using unquote_plus(): %s != %s" % (expect, result))
465
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000466 def test_unquote_with_unicode(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000467 r = urllib.parse.unquote('br%C3%BCckner_sapporo_20050930.doc')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000468 self.assertEqual(r, 'br\xc3\xbcckner_sapporo_20050930.doc')
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000469
Brett Cannon74bfd702003-04-25 09:39:47 +0000470class urlencode_Tests(unittest.TestCase):
471 """Tests for urlencode()"""
472
473 def help_inputtype(self, given, test_type):
474 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000475
Brett Cannon74bfd702003-04-25 09:39:47 +0000476 'given' must lead to only the pairs:
477 * 1st, 1
478 * 2nd, 2
479 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000480
Brett Cannon74bfd702003-04-25 09:39:47 +0000481 Test cannot assume anything about order. Docs make no guarantee and
482 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000483
Brett Cannon74bfd702003-04-25 09:39:47 +0000484 """
485 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000486 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000487 for expected in expect_somewhere:
488 self.assert_(expected in result,
489 "testing %s: %s not found in %s" %
490 (test_type, expected, result))
491 self.assertEqual(result.count('&'), 2,
492 "testing %s: expected 2 '&'s; got %s" %
493 (test_type, result.count('&')))
494 amp_location = result.index('&')
495 on_amp_left = result[amp_location - 1]
496 on_amp_right = result[amp_location + 1]
497 self.assert_(on_amp_left.isdigit() and on_amp_right.isdigit(),
498 "testing %s: '&' not located in proper place in %s" %
499 (test_type, result))
500 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
501 "testing %s: "
502 "unexpected number of characters: %s != %s" %
503 (test_type, len(result), (5 * 3) + 2))
504
505 def test_using_mapping(self):
506 # Test passing in a mapping object as an argument.
507 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
508 "using dict as input type")
509
510 def test_using_sequence(self):
511 # Test passing in a sequence of two-item sequences as an argument.
512 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
513 "using sequence of two-item tuples as input")
514
515 def test_quoting(self):
516 # Make sure keys and values are quoted using quote_plus()
517 given = {"&":"="}
518 expect = "%s=%s" % (hexescape('&'), hexescape('='))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000519 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000520 self.assertEqual(expect, result)
521 given = {"key name":"A bunch of pluses"}
522 expect = "key+name=A+bunch+of+pluses"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000523 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000524 self.assertEqual(expect, result)
525
526 def test_doseq(self):
527 # Test that passing True for 'doseq' parameter works correctly
528 given = {'sequence':['1', '2', '3']}
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000529 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
530 result = urllib.parse.urlencode(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000531 self.assertEqual(expect, result)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000532 result = urllib.parse.urlencode(given, True)
Brett Cannon74bfd702003-04-25 09:39:47 +0000533 for value in given["sequence"]:
534 expect = "sequence=%s" % value
535 self.assert_(expect in result,
536 "%s not found in %s" % (expect, result))
537 self.assertEqual(result.count('&'), 2,
538 "Expected 2 '&'s, got %s" % result.count('&'))
539
540class Pathname_Tests(unittest.TestCase):
541 """Test pathname2url() and url2pathname()"""
542
543 def test_basic(self):
544 # Make sure simple tests pass
545 expected_path = os.path.join("parts", "of", "a", "path")
546 expected_url = "parts/of/a/path"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000547 result = urllib.request.pathname2url(expected_path)
Brett Cannon74bfd702003-04-25 09:39:47 +0000548 self.assertEqual(expected_url, result,
549 "pathname2url() failed; %s != %s" %
550 (result, expected_url))
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000551 result = urllib.request.url2pathname(expected_url)
Brett Cannon74bfd702003-04-25 09:39:47 +0000552 self.assertEqual(expected_path, result,
553 "url2pathame() failed; %s != %s" %
554 (result, expected_path))
555
556 def test_quoting(self):
557 # Test automatic quoting and unquoting works for pathnam2url() and
558 # url2pathname() respectively
559 given = os.path.join("needs", "quot=ing", "here")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000560 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")
561 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000562 self.assertEqual(expect, result,
563 "pathname2url() failed; %s != %s" %
564 (expect, result))
565 expect = given
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000566 result = urllib.request.url2pathname(result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000567 self.assertEqual(expect, result,
568 "url2pathname() failed; %s != %s" %
569 (expect, result))
570 given = os.path.join("make sure", "using_quote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000571 expect = "%s/using_quote" % urllib.parse.quote("make sure")
572 result = urllib.request.pathname2url(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000573 self.assertEqual(expect, result,
574 "pathname2url() failed; %s != %s" %
575 (expect, result))
576 given = "make+sure/using_unquote"
577 expect = os.path.join("make+sure", "using_unquote")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000578 result = urllib.request.url2pathname(given)
Brett Cannon74bfd702003-04-25 09:39:47 +0000579 self.assertEqual(expect, result,
580 "url2pathname() failed; %s != %s" %
581 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000582
Guido van Rossume7ba4952007-06-06 23:52:48 +0000583# Just commented them out.
584# Can't really tell why keep failing in windows and sparc.
585# Everywhere else they work ok, but on those machines, someteimes
586# fail in one of the tests, sometimes in other. I have a linux, and
587# the tests go ok.
588# If anybody has one of the problematic enviroments, please help!
589# . Facundo
590#
591# def server(evt):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000592# import socket, time
Guido van Rossume7ba4952007-06-06 23:52:48 +0000593# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
594# serv.settimeout(3)
595# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
596# serv.bind(("", 9093))
597# serv.listen(5)
598# try:
599# conn, addr = serv.accept()
600# conn.send("1 Hola mundo\n")
601# cantdata = 0
602# while cantdata < 13:
603# data = conn.recv(13-cantdata)
604# cantdata += len(data)
605# time.sleep(.3)
606# conn.send("2 No more lines\n")
607# conn.close()
608# except socket.timeout:
609# pass
610# finally:
611# serv.close()
612# evt.set()
613#
614# class FTPWrapperTests(unittest.TestCase):
615#
616# def setUp(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000617# import ftplib, time, threading
Guido van Rossume7ba4952007-06-06 23:52:48 +0000618# ftplib.FTP.port = 9093
619# self.evt = threading.Event()
620# threading.Thread(target=server, args=(self.evt,)).start()
621# time.sleep(.1)
622#
623# def tearDown(self):
624# self.evt.wait()
625#
626# def testBasic(self):
627# # connects
628# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Georg Brandlf78e02b2008-06-10 17:40:04 +0000629# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000630#
631# def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000632# # global default timeout is ignored
633# import socket
634# self.assert_(socket.getdefaulttimeout() is None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000635# socket.setdefaulttimeout(30)
636# try:
637# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
638# finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000639# socket.setdefaulttimeout(None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000640# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000641# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000642#
Georg Brandlf78e02b2008-06-10 17:40:04 +0000643# def testTimeoutDefault(self):
644# # global default timeout is used
645# import socket
646# self.assert_(socket.getdefaulttimeout() is None)
647# socket.setdefaulttimeout(30)
648# try:
649# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
650# finally:
651# socket.setdefaulttimeout(None)
652# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
653# ftp.close()
654#
655# def testTimeoutValue(self):
656# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
657# timeout=30)
658# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
659# ftp.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000660
Skip Montanaro080c9972001-01-28 21:12:22 +0000661
662
Brett Cannon74bfd702003-04-25 09:39:47 +0000663def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000664 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000665 urlopen_FileTests,
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000666 urlopen_HttpTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000667 urlretrieve_FileTests,
668 QuotingTests,
669 UnquotingTests,
670 urlencode_Tests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000671 Pathname_Tests,
672 #FTPWrapperTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000673 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000674
675
676
677if __name__ == '__main__':
678 test_main()