blob: db908399f43e4e6a013550317a6d5c258e420f9c [file] [log] [blame]
Brett Cannon74bfd702003-04-25 09:39:47 +00001"""Regresssion tests for urllib"""
2
Jeremy Hylton6102e292000-08-31 15:48:10 +00003import urllib
Hye-Shik Chang39aef792004-06-05 13:30:56 +00004import httplib
Brett Cannon74bfd702003-04-25 09:39:47 +00005import unittest
6from test import test_support
7import os
Senthil Kumarana99b7612011-04-14 12:54:35 +08008import sys
Brett Cannon74bfd702003-04-25 09:39:47 +00009import mimetools
Georg Brandl5a650a22005-08-26 08:51:34 +000010import tempfile
Hye-Shik Chang39aef792004-06-05 13:30:56 +000011import StringIO
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
Senthil Kumaran87e58552011-11-01 02:44:45 +080020
21class FakeHTTPMixin(object):
22 def fakehttp(self, fakedata):
23 class FakeSocket(StringIO.StringIO):
24
25 def sendall(self, str):
26 pass
27 def makefile(self, *args, **kwds):
28 return self
29
30 def read(self, amt=None):
31 if self.closed:
32 return ""
33 return StringIO.StringIO.read(self, amt)
34
35 def readline(self, length=None):
36 if self.closed:
37 return ""
38 return StringIO.StringIO.readline(self, length)
39
40 class FakeHTTPConnection(httplib.HTTPConnection):
41 def connect(self):
42 self.sock = FakeSocket(fakedata)
43 assert httplib.HTTP._connection_class == httplib.HTTPConnection
44 httplib.HTTP._connection_class = FakeHTTPConnection
45
46 def unfakehttp(self):
47 httplib.HTTP._connection_class = httplib.HTTPConnection
48
49
Brett Cannon74bfd702003-04-25 09:39:47 +000050class urlopen_FileTests(unittest.TestCase):
51 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000052
Brett Cannon74bfd702003-04-25 09:39:47 +000053 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000054 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000055
Brett Cannon74bfd702003-04-25 09:39:47 +000056 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000057
Brett Cannon74bfd702003-04-25 09:39:47 +000058 def setUp(self):
59 """Setup of a temp file to use for testing"""
60 self.text = "test_urllib: %s\n" % self.__class__.__name__
Guido van Rossum51735b02003-04-25 15:01:05 +000061 FILE = file(test_support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000062 try:
63 FILE.write(self.text)
64 finally:
65 FILE.close()
66 self.pathname = test_support.TESTFN
67 self.returned_obj = urllib.urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000068
Brett Cannon74bfd702003-04-25 09:39:47 +000069 def tearDown(self):
70 """Shut down the open object"""
71 self.returned_obj.close()
Brett Cannon19691362003-04-29 05:08:06 +000072 os.remove(test_support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000073
Brett Cannon74bfd702003-04-25 09:39:47 +000074 def test_interface(self):
75 # Make sure object returned by urlopen() has the specified methods
76 for attr in ("read", "readline", "readlines", "fileno",
Georg Brandl9b0d46d2008-01-20 11:43:03 +000077 "close", "info", "geturl", "getcode", "__iter__"):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000078 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +000079 "object returned by urlopen() lacks %s attribute" %
80 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000081
Brett Cannon74bfd702003-04-25 09:39:47 +000082 def test_read(self):
83 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +000084
Brett Cannon74bfd702003-04-25 09:39:47 +000085 def test_readline(self):
86 self.assertEqual(self.text, self.returned_obj.readline())
87 self.assertEqual('', self.returned_obj.readline(),
88 "calling readline() after exhausting the file did not"
89 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +000090
Brett Cannon74bfd702003-04-25 09:39:47 +000091 def test_readlines(self):
92 lines_list = self.returned_obj.readlines()
93 self.assertEqual(len(lines_list), 1,
94 "readlines() returned the wrong number of lines")
95 self.assertEqual(lines_list[0], self.text,
96 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +000097
Brett Cannon74bfd702003-04-25 09:39:47 +000098 def test_fileno(self):
99 file_num = self.returned_obj.fileno()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000100 self.assertIsInstance(file_num, int, "fileno() did not return an int")
Brett Cannon74bfd702003-04-25 09:39:47 +0000101 self.assertEqual(os.read(file_num, len(self.text)), self.text,
102 "Reading on the file descriptor returned by fileno() "
103 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000104
Brett Cannon74bfd702003-04-25 09:39:47 +0000105 def test_close(self):
106 # Test close() by calling it hear and then having it be called again
107 # by the tearDown() method for the test
108 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +0000109
Brett Cannon74bfd702003-04-25 09:39:47 +0000110 def test_info(self):
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000111 self.assertIsInstance(self.returned_obj.info(), mimetools.Message)
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000112
Brett Cannon74bfd702003-04-25 09:39:47 +0000113 def test_geturl(self):
114 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +0000115
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000116 def test_getcode(self):
117 self.assertEqual(self.returned_obj.getcode(), None)
118
Brett Cannon74bfd702003-04-25 09:39:47 +0000119 def test_iter(self):
120 # Test iterator
121 # Don't need to count number of iterations since test would fail the
122 # instant it returned anything beyond the first line from the
123 # comparison
124 for line in self.returned_obj.__iter__():
125 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +0000126
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000127class ProxyTests(unittest.TestCase):
128
129 def setUp(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000130 # Records changes to env vars
131 self.env = test_support.EnvironmentVarGuard()
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000132 # Delete all proxy related env vars
Senthil Kumaran7a2ee0b2010-01-08 19:20:25 +0000133 for k in os.environ.keys():
Walter Dörwald4b965f62009-04-26 20:51:44 +0000134 if 'proxy' in k.lower():
Senthil Kumarandc61ec32009-10-01 01:50:13 +0000135 self.env.unset(k)
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000136
137 def tearDown(self):
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000138 # Restore all proxy related env vars
Walter Dörwald4b965f62009-04-26 20:51:44 +0000139 self.env.__exit__()
140 del self.env
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000141
142 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000143 self.env.set('NO_PROXY', 'localhost')
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000144 proxies = urllib.getproxies_environment()
145 # getproxies_environment use lowered case truncated (no '_proxy') keys
Ezio Melotti2623a372010-11-21 13:34:58 +0000146 self.assertEqual('localhost', proxies['no'])
Senthil Kumaranb5bd4c82011-08-06 12:24:33 +0800147 # List of no_proxies with space.
148 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
149 self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com'))
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000150
151
Senthil Kumaran87e58552011-11-01 02:44:45 +0800152class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000153 """Test urlopen() opening a fake http connection."""
154
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000155 def test_read(self):
156 self.fakehttp('Hello!')
157 try:
158 fp = urllib.urlopen("http://python.org/")
159 self.assertEqual(fp.readline(), 'Hello!')
160 self.assertEqual(fp.readline(), '')
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000161 self.assertEqual(fp.geturl(), 'http://python.org/')
162 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000163 finally:
164 self.unfakehttp()
165
Senthil Kumaran49c44082011-04-13 07:31:45 +0800166 def test_url_fragment(self):
167 # Issue #11703: geturl() omits fragments in the original URL.
168 url = 'http://docs.python.org/library/urllib.html#OK'
169 self.fakehttp('Hello!')
170 try:
171 fp = urllib.urlopen(url)
172 self.assertEqual(fp.geturl(), url)
173 finally:
174 self.unfakehttp()
175
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000176 def test_read_bogus(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000177 # urlopen() should raise IOError for many error codes.
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000178 self.fakehttp('''HTTP/1.1 401 Authentication Required
179Date: Wed, 02 Jan 2008 03:03:54 GMT
180Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
181Connection: close
182Content-Type: text/html; charset=iso-8859-1
183''')
184 try:
185 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
186 finally:
187 self.unfakehttp()
188
guido@google.comf1509302011-03-28 13:47:01 -0700189 def test_invalid_redirect(self):
190 # urlopen() should raise IOError for many error codes.
191 self.fakehttp("""HTTP/1.1 302 Found
192Date: Wed, 02 Jan 2008 03:03:54 GMT
193Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
194Location: file:README
195Connection: close
196Content-Type: text/html; charset=iso-8859-1
197""")
198 try:
199 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
200 finally:
201 self.unfakehttp()
202
Georg Brandlf66b6032007-03-14 08:27:52 +0000203 def test_empty_socket(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000204 # urlopen() raises IOError if the underlying socket does not send any
205 # data. (#1680230)
Georg Brandlf66b6032007-03-14 08:27:52 +0000206 self.fakehttp('')
207 try:
208 self.assertRaises(IOError, urllib.urlopen, 'http://something')
209 finally:
210 self.unfakehttp()
211
Brett Cannon19691362003-04-29 05:08:06 +0000212class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000213 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000214
Brett Cannon19691362003-04-29 05:08:06 +0000215 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000216 # Create a list of temporary files. Each item in the list is a file
217 # name (absolute path or relative to the current working directory).
218 # All files in this list will be deleted in the tearDown method. Note,
219 # this only helps to makes sure temporary files get deleted, but it
220 # does nothing about trying to close files that may still be open. It
221 # is the responsibility of the developer to properly close files even
222 # when exceptional conditions occur.
223 self.tempFiles = []
224
Brett Cannon19691362003-04-29 05:08:06 +0000225 # Create a temporary file.
Georg Brandl5a650a22005-08-26 08:51:34 +0000226 self.registerFileForCleanUp(test_support.TESTFN)
Brett Cannon19691362003-04-29 05:08:06 +0000227 self.text = 'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000228 try:
229 FILE = file(test_support.TESTFN, 'wb')
230 FILE.write(self.text)
231 FILE.close()
232 finally:
233 try: FILE.close()
234 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000235
236 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000237 # Delete the temporary files.
238 for each in self.tempFiles:
239 try: os.remove(each)
240 except: pass
241
242 def constructLocalFileUrl(self, filePath):
243 return "file://%s" % urllib.pathname2url(os.path.abspath(filePath))
244
245 def createNewTempFile(self, data=""):
246 """Creates a new temporary file containing the specified data,
247 registers the file for deletion during the test fixture tear down, and
248 returns the absolute path of the file."""
249
250 newFd, newFilePath = tempfile.mkstemp()
251 try:
252 self.registerFileForCleanUp(newFilePath)
253 newFile = os.fdopen(newFd, "wb")
254 newFile.write(data)
255 newFile.close()
256 finally:
257 try: newFile.close()
258 except: pass
259 return newFilePath
260
261 def registerFileForCleanUp(self, fileName):
262 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000263
264 def test_basic(self):
265 # Make sure that a local file just gets its own location returned and
266 # a headers value is returned.
267 result = urllib.urlretrieve("file:%s" % test_support.TESTFN)
268 self.assertEqual(result[0], test_support.TESTFN)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000269 self.assertIsInstance(result[1], mimetools.Message,
270 "did not get a mimetools.Message instance as "
271 "second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000272
273 def test_copy(self):
274 # Test that setting the filename argument works.
275 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000276 self.registerFileForCleanUp(second_temp)
277 result = urllib.urlretrieve(self.constructLocalFileUrl(
278 test_support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000279 self.assertEqual(second_temp, result[0])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000280 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000281 "made")
282 FILE = file(second_temp, 'rb')
283 try:
284 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000285 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000286 finally:
287 try: FILE.close()
288 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000289 self.assertEqual(self.text, text)
290
291 def test_reporthook(self):
292 # Make sure that the reporthook works.
293 def hooktester(count, block_size, total_size, count_holder=[0]):
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000294 self.assertIsInstance(count, int)
295 self.assertIsInstance(block_size, int)
296 self.assertIsInstance(total_size, int)
Brett Cannon19691362003-04-29 05:08:06 +0000297 self.assertEqual(count, count_holder[0])
298 count_holder[0] = count_holder[0] + 1
299 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000300 self.registerFileForCleanUp(second_temp)
301 urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN),
302 second_temp, hooktester)
303
304 def test_reporthook_0_bytes(self):
305 # Test on zero length file. Should call reporthook only 1 time.
306 report = []
307 def hooktester(count, block_size, total_size, _report=report):
308 _report.append((count, block_size, total_size))
309 srcFileName = self.createNewTempFile()
310 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
311 test_support.TESTFN, hooktester)
312 self.assertEqual(len(report), 1)
313 self.assertEqual(report[0][2], 0)
314
315 def test_reporthook_5_bytes(self):
316 # Test on 5 byte file. Should call reporthook only 2 times (once when
317 # the "network connection" is established and once when the block is
318 # read). Since the block size is 8192 bytes, only one block read is
319 # required to read the entire file.
320 report = []
321 def hooktester(count, block_size, total_size, _report=report):
322 _report.append((count, block_size, total_size))
323 srcFileName = self.createNewTempFile("x" * 5)
324 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
325 test_support.TESTFN, hooktester)
326 self.assertEqual(len(report), 2)
327 self.assertEqual(report[0][1], 8192)
328 self.assertEqual(report[0][2], 5)
329
330 def test_reporthook_8193_bytes(self):
331 # Test on 8193 byte file. Should call reporthook only 3 times (once
332 # when the "network connection" is established, once for the next 8192
333 # bytes, and once for the last byte).
334 report = []
335 def hooktester(count, block_size, total_size, _report=report):
336 _report.append((count, block_size, total_size))
337 srcFileName = self.createNewTempFile("x" * 8193)
338 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
339 test_support.TESTFN, hooktester)
340 self.assertEqual(len(report), 3)
341 self.assertEqual(report[0][1], 8192)
342 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000343
Senthil Kumaran87e58552011-11-01 02:44:45 +0800344
345class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
346 """Test urllib.urlretrieve() using fake http connections"""
347
348 def test_short_content_raises_ContentTooShortError(self):
349 self.fakehttp('''HTTP/1.1 200 OK
350Date: Wed, 02 Jan 2008 03:03:54 GMT
351Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
352Connection: close
353Content-Length: 100
354Content-Type: text/html; charset=iso-8859-1
355
356FF
357''')
358
359 def _reporthook(par1, par2, par3):
360 pass
361
362 try:
363 self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve,
364 'http://example.com', reporthook=_reporthook)
365 finally:
366 self.unfakehttp()
367
368 def test_short_content_raises_ContentTooShortError_without_reporthook(self):
369 self.fakehttp('''HTTP/1.1 200 OK
370Date: Wed, 02 Jan 2008 03:03:54 GMT
371Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
372Connection: close
373Content-Length: 100
374Content-Type: text/html; charset=iso-8859-1
375
376FF
377''')
378 try:
379 self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve, 'http://example.com/')
380 finally:
381 self.unfakehttp()
382
Brett Cannon74bfd702003-04-25 09:39:47 +0000383class QuotingTests(unittest.TestCase):
384 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000385
Brett Cannon74bfd702003-04-25 09:39:47 +0000386 According to RFC 2396 ("Uniform Resource Identifiers), to escape a
387 character you write it as '%' + <2 character US-ASCII hex value>. The Python
388 code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly.
389 Case does not matter on the hex letters.
390
391 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000392
Brett Cannon74bfd702003-04-25 09:39:47 +0000393 Reserved characters : ";/?:@&=+$,"
394 Have special meaning in URIs and must be escaped if not being used for
395 their special meaning
396 Data characters : letters, digits, and "-_.!~*'()"
397 Unreserved and do not need to be escaped; can be, though, if desired
398 Control characters : 0x00 - 0x1F, 0x7F
399 Have no use in URIs so must be escaped
400 space : 0x20
401 Must be escaped
402 Delimiters : '<>#%"'
403 Must be escaped
404 Unwise : "{}|\^[]`"
405 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000406
Brett Cannon74bfd702003-04-25 09:39:47 +0000407 """
408
409 def test_never_quote(self):
410 # Make sure quote() does not quote letters, digits, and "_,.-"
411 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
412 "abcdefghijklmnopqrstuvwxyz",
413 "0123456789",
414 "_.-"])
415 result = urllib.quote(do_not_quote)
416 self.assertEqual(do_not_quote, result,
417 "using quote(): %s != %s" % (do_not_quote, result))
418 result = urllib.quote_plus(do_not_quote)
419 self.assertEqual(do_not_quote, result,
420 "using quote_plus(): %s != %s" % (do_not_quote, result))
421
422 def test_default_safe(self):
423 # Test '/' is default value for 'safe' parameter
424 self.assertEqual(urllib.quote.func_defaults[0], '/')
425
426 def test_safe(self):
427 # Test setting 'safe' parameter does what it should do
428 quote_by_default = "<>"
429 result = urllib.quote(quote_by_default, safe=quote_by_default)
430 self.assertEqual(quote_by_default, result,
431 "using quote(): %s != %s" % (quote_by_default, result))
432 result = urllib.quote_plus(quote_by_default, safe=quote_by_default)
433 self.assertEqual(quote_by_default, result,
434 "using quote_plus(): %s != %s" %
435 (quote_by_default, result))
436
437 def test_default_quoting(self):
438 # Make sure all characters that should be quoted are by default sans
439 # space (separate test for that).
440 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
441 should_quote.append('<>#%"{}|\^[]`')
442 should_quote.append(chr(127)) # For 0x7F
443 should_quote = ''.join(should_quote)
444 for char in should_quote:
445 result = urllib.quote(char)
446 self.assertEqual(hexescape(char), result,
447 "using quote(): %s should be escaped to %s, not %s" %
448 (char, hexescape(char), result))
449 result = urllib.quote_plus(char)
450 self.assertEqual(hexescape(char), result,
451 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000452 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000453 (char, hexescape(char), result))
454 del should_quote
455 partial_quote = "ab[]cd"
456 expected = "ab%5B%5Dcd"
457 result = urllib.quote(partial_quote)
458 self.assertEqual(expected, result,
459 "using quote(): %s != %s" % (expected, result))
Senthil Kumaran0d4c34c2011-09-13 06:42:21 +0800460 result = urllib.quote_plus(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000461 self.assertEqual(expected, result,
462 "using quote_plus(): %s != %s" % (expected, result))
Senthil Kumaranc7743aa2010-07-19 17:35:50 +0000463 self.assertRaises(TypeError, urllib.quote, None)
Brett Cannon74bfd702003-04-25 09:39:47 +0000464
465 def test_quoting_space(self):
466 # Make sure quote() and quote_plus() handle spaces as specified in
467 # their unique way
468 result = urllib.quote(' ')
469 self.assertEqual(result, hexescape(' '),
470 "using quote(): %s != %s" % (result, hexescape(' ')))
471 result = urllib.quote_plus(' ')
472 self.assertEqual(result, '+',
473 "using quote_plus(): %s != +" % result)
474 given = "a b cd e f"
475 expect = given.replace(' ', hexescape(' '))
476 result = urllib.quote(given)
477 self.assertEqual(expect, result,
478 "using quote(): %s != %s" % (expect, result))
479 expect = given.replace(' ', '+')
480 result = urllib.quote_plus(given)
481 self.assertEqual(expect, result,
482 "using quote_plus(): %s != %s" % (expect, result))
483
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000484 def test_quoting_plus(self):
485 self.assertEqual(urllib.quote_plus('alpha+beta gamma'),
486 'alpha%2Bbeta+gamma')
487 self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'),
488 'alpha+beta+gamma')
489
Brett Cannon74bfd702003-04-25 09:39:47 +0000490class UnquotingTests(unittest.TestCase):
491 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000492
Brett Cannon74bfd702003-04-25 09:39:47 +0000493 See the doc string for quoting_Tests for details on quoting and such.
494
495 """
496
497 def test_unquoting(self):
498 # Make sure unquoting of all ASCII values works
499 escape_list = []
500 for num in range(128):
501 given = hexescape(chr(num))
502 expect = chr(num)
503 result = urllib.unquote(given)
504 self.assertEqual(expect, result,
505 "using unquote(): %s != %s" % (expect, result))
506 result = urllib.unquote_plus(given)
507 self.assertEqual(expect, result,
508 "using unquote_plus(): %s != %s" %
509 (expect, result))
510 escape_list.append(given)
511 escape_string = ''.join(escape_list)
512 del escape_list
513 result = urllib.unquote(escape_string)
514 self.assertEqual(result.count('%'), 1,
515 "using quote(): not all characters escaped; %s" %
516 result)
517 result = urllib.unquote(escape_string)
518 self.assertEqual(result.count('%'), 1,
519 "using unquote(): not all characters escaped: "
520 "%s" % result)
521
Senthil Kumaranf3e9b2a2010-03-18 12:14:15 +0000522 def test_unquoting_badpercent(self):
523 # Test unquoting on bad percent-escapes
524 given = '%xab'
525 expect = given
526 result = urllib.unquote(given)
527 self.assertEqual(expect, result, "using unquote(): %r != %r"
528 % (expect, result))
529 given = '%x'
530 expect = given
531 result = urllib.unquote(given)
532 self.assertEqual(expect, result, "using unquote(): %r != %r"
533 % (expect, result))
534 given = '%'
535 expect = given
536 result = urllib.unquote(given)
537 self.assertEqual(expect, result, "using unquote(): %r != %r"
538 % (expect, result))
539
540 def test_unquoting_mixed_case(self):
541 # Test unquoting on mixed-case hex digits in the percent-escapes
542 given = '%Ab%eA'
543 expect = '\xab\xea'
544 result = urllib.unquote(given)
545 self.assertEqual(expect, result, "using unquote(): %r != %r"
546 % (expect, result))
547
Brett Cannon74bfd702003-04-25 09:39:47 +0000548 def test_unquoting_parts(self):
549 # Make sure unquoting works when have non-quoted characters
550 # interspersed
551 given = 'ab%sd' % hexescape('c')
552 expect = "abcd"
553 result = urllib.unquote(given)
554 self.assertEqual(expect, result,
555 "using quote(): %s != %s" % (expect, result))
556 result = urllib.unquote_plus(given)
557 self.assertEqual(expect, result,
558 "using unquote_plus(): %s != %s" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000559
Brett Cannon74bfd702003-04-25 09:39:47 +0000560 def test_unquoting_plus(self):
561 # Test difference between unquote() and unquote_plus()
562 given = "are+there+spaces..."
563 expect = given
564 result = urllib.unquote(given)
565 self.assertEqual(expect, result,
566 "using unquote(): %s != %s" % (expect, result))
567 expect = given.replace('+', ' ')
568 result = urllib.unquote_plus(given)
569 self.assertEqual(expect, result,
570 "using unquote_plus(): %s != %s" % (expect, result))
571
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000572 def test_unquote_with_unicode(self):
573 r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc')
574 self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc')
575
Brett Cannon74bfd702003-04-25 09:39:47 +0000576class urlencode_Tests(unittest.TestCase):
577 """Tests for urlencode()"""
578
579 def help_inputtype(self, given, test_type):
580 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000581
Brett Cannon74bfd702003-04-25 09:39:47 +0000582 'given' must lead to only the pairs:
583 * 1st, 1
584 * 2nd, 2
585 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000586
Brett Cannon74bfd702003-04-25 09:39:47 +0000587 Test cannot assume anything about order. Docs make no guarantee and
588 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000589
Brett Cannon74bfd702003-04-25 09:39:47 +0000590 """
591 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
592 result = urllib.urlencode(given)
593 for expected in expect_somewhere:
Ezio Melottiaa980582010-01-23 23:04:36 +0000594 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000595 "testing %s: %s not found in %s" %
596 (test_type, expected, result))
597 self.assertEqual(result.count('&'), 2,
598 "testing %s: expected 2 '&'s; got %s" %
599 (test_type, result.count('&')))
600 amp_location = result.index('&')
601 on_amp_left = result[amp_location - 1]
602 on_amp_right = result[amp_location + 1]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000603 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000604 "testing %s: '&' not located in proper place in %s" %
605 (test_type, result))
606 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
607 "testing %s: "
608 "unexpected number of characters: %s != %s" %
609 (test_type, len(result), (5 * 3) + 2))
610
611 def test_using_mapping(self):
612 # Test passing in a mapping object as an argument.
613 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
614 "using dict as input type")
615
616 def test_using_sequence(self):
617 # Test passing in a sequence of two-item sequences as an argument.
618 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
619 "using sequence of two-item tuples as input")
620
621 def test_quoting(self):
622 # Make sure keys and values are quoted using quote_plus()
623 given = {"&":"="}
624 expect = "%s=%s" % (hexescape('&'), hexescape('='))
625 result = urllib.urlencode(given)
626 self.assertEqual(expect, result)
627 given = {"key name":"A bunch of pluses"}
628 expect = "key+name=A+bunch+of+pluses"
629 result = urllib.urlencode(given)
630 self.assertEqual(expect, result)
631
632 def test_doseq(self):
633 # Test that passing True for 'doseq' parameter works correctly
634 given = {'sequence':['1', '2', '3']}
635 expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3']))
636 result = urllib.urlencode(given)
637 self.assertEqual(expect, result)
638 result = urllib.urlencode(given, True)
639 for value in given["sequence"]:
640 expect = "sequence=%s" % value
Ezio Melottiaa980582010-01-23 23:04:36 +0000641 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000642 self.assertEqual(result.count('&'), 2,
643 "Expected 2 '&'s, got %s" % result.count('&'))
644
645class Pathname_Tests(unittest.TestCase):
646 """Test pathname2url() and url2pathname()"""
647
648 def test_basic(self):
649 # Make sure simple tests pass
650 expected_path = os.path.join("parts", "of", "a", "path")
651 expected_url = "parts/of/a/path"
652 result = urllib.pathname2url(expected_path)
653 self.assertEqual(expected_url, result,
654 "pathname2url() failed; %s != %s" %
655 (result, expected_url))
656 result = urllib.url2pathname(expected_url)
657 self.assertEqual(expected_path, result,
658 "url2pathame() failed; %s != %s" %
659 (result, expected_path))
660
661 def test_quoting(self):
662 # Test automatic quoting and unquoting works for pathnam2url() and
663 # url2pathname() respectively
664 given = os.path.join("needs", "quot=ing", "here")
665 expect = "needs/%s/here" % urllib.quote("quot=ing")
666 result = urllib.pathname2url(given)
667 self.assertEqual(expect, result,
668 "pathname2url() failed; %s != %s" %
669 (expect, result))
670 expect = given
671 result = urllib.url2pathname(result)
672 self.assertEqual(expect, result,
673 "url2pathname() failed; %s != %s" %
674 (expect, result))
675 given = os.path.join("make sure", "using_quote")
676 expect = "%s/using_quote" % urllib.quote("make sure")
677 result = urllib.pathname2url(given)
678 self.assertEqual(expect, result,
679 "pathname2url() failed; %s != %s" %
680 (expect, result))
681 given = "make+sure/using_unquote"
682 expect = os.path.join("make+sure", "using_unquote")
683 result = urllib.url2pathname(given)
684 self.assertEqual(expect, result,
685 "url2pathname() failed; %s != %s" %
686 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000687
Senthil Kumarana99b7612011-04-14 12:54:35 +0800688 @unittest.skipUnless(sys.platform == 'win32',
689 'test specific to the nturl2path library')
690 def test_ntpath(self):
691 given = ('/C:/', '///C:/', '/C|//')
692 expect = 'C:\\'
693 for url in given:
694 result = urllib.url2pathname(url)
695 self.assertEqual(expect, result,
696 'nturl2path.url2pathname() failed; %s != %s' %
697 (expect, result))
698 given = '///C|/path'
699 expect = 'C:\\path'
700 result = urllib.url2pathname(given)
701 self.assertEqual(expect, result,
702 'nturl2path.url2pathname() failed; %s != %s' %
703 (expect, result))
704
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000705class Utility_Tests(unittest.TestCase):
706 """Testcase to test the various utility functions in the urllib."""
707
708 def test_splitpasswd(self):
709 """Some of the password examples are not sensible, but it is added to
710 confirming to RFC2617 and addressing issue4675.
711 """
712 self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab'))
713 self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb'))
714 self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb'))
715 self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb'))
716 self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb'))
717 self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb'))
718 self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b'))
719
720
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000721class URLopener_Tests(unittest.TestCase):
722 """Testcase to test the open method of URLopener class."""
723
724 def test_quoted_open(self):
725 class DummyURLopener(urllib.URLopener):
726 def open_spam(self, url):
727 return url
728
729 self.assertEqual(DummyURLopener().open(
730 'spam://example/ /'),'//example/%20/')
731
Senthil Kumaran18d5a692010-02-20 22:05:34 +0000732 # test the safe characters are not quoted by urlopen
733 self.assertEqual(DummyURLopener().open(
734 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
735 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
736
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000737
Facundo Batistad9880d02007-05-25 04:20:22 +0000738# Just commented them out.
739# Can't really tell why keep failing in windows and sparc.
Ezio Melottic2077b02011-03-16 12:34:31 +0200740# Everywhere else they work ok, but on those machines, sometimes
Facundo Batistad9880d02007-05-25 04:20:22 +0000741# fail in one of the tests, sometimes in other. I have a linux, and
742# the tests go ok.
743# If anybody has one of the problematic enviroments, please help!
744# . Facundo
745#
746# def server(evt):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000747# import socket, time
Facundo Batistad9880d02007-05-25 04:20:22 +0000748# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
749# serv.settimeout(3)
750# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
751# serv.bind(("", 9093))
752# serv.listen(5)
753# try:
754# conn, addr = serv.accept()
755# conn.send("1 Hola mundo\n")
756# cantdata = 0
757# while cantdata < 13:
758# data = conn.recv(13-cantdata)
759# cantdata += len(data)
760# time.sleep(.3)
761# conn.send("2 No more lines\n")
762# conn.close()
763# except socket.timeout:
764# pass
765# finally:
766# serv.close()
767# evt.set()
768#
769# class FTPWrapperTests(unittest.TestCase):
770#
771# def setUp(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000772# import ftplib, time, threading
Facundo Batistad9880d02007-05-25 04:20:22 +0000773# ftplib.FTP.port = 9093
774# self.evt = threading.Event()
775# threading.Thread(target=server, args=(self.evt,)).start()
776# time.sleep(.1)
777#
778# def tearDown(self):
779# self.evt.wait()
780#
781# def testBasic(self):
782# # connects
783# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000784# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000785#
786# def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000787# # global default timeout is ignored
788# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000789# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000790# socket.setdefaulttimeout(30)
791# try:
792# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
793# finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000794# socket.setdefaulttimeout(None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000795# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000796# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000797#
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000798# def testTimeoutDefault(self):
799# # global default timeout is used
800# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000801# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000802# socket.setdefaulttimeout(30)
803# try:
804# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
805# finally:
806# socket.setdefaulttimeout(None)
807# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
808# ftp.close()
809#
810# def testTimeoutValue(self):
811# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
812# timeout=30)
813# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
814# ftp.close()
Facundo Batista711a54e2007-05-24 17:50:54 +0000815
Skip Montanaro080c9972001-01-28 21:12:22 +0000816
817
Brett Cannon74bfd702003-04-25 09:39:47 +0000818def test_main():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000819 import warnings
Brett Cannon672237d2008-09-09 00:49:16 +0000820 with warnings.catch_warnings():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000821 warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
822 DeprecationWarning)
823 test_support.run_unittest(
824 urlopen_FileTests,
825 urlopen_HttpTests,
826 urlretrieve_FileTests,
Senthil Kumaran87e58552011-11-01 02:44:45 +0800827 urlretrieve_HttpTests,
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000828 ProxyTests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000829 QuotingTests,
830 UnquotingTests,
831 urlencode_Tests,
832 Pathname_Tests,
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000833 Utility_Tests,
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000834 URLopener_Tests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000835 #FTPWrapperTests,
836 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000837
838
839
840if __name__ == '__main__':
841 test_main()