blob: 3f644fbea800adc5b37cf6032a0c980219c9034d [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
Brett Cannon74bfd702003-04-25 09:39:47 +000020class urlopen_FileTests(unittest.TestCase):
21 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000022
Brett Cannon74bfd702003-04-25 09:39:47 +000023 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000024 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000025
Brett Cannon74bfd702003-04-25 09:39:47 +000026 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000027
Brett Cannon74bfd702003-04-25 09:39:47 +000028 def setUp(self):
29 """Setup of a temp file to use for testing"""
30 self.text = "test_urllib: %s\n" % self.__class__.__name__
Guido van Rossum51735b02003-04-25 15:01:05 +000031 FILE = file(test_support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000032 try:
33 FILE.write(self.text)
34 finally:
35 FILE.close()
36 self.pathname = test_support.TESTFN
37 self.returned_obj = urllib.urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000038
Brett Cannon74bfd702003-04-25 09:39:47 +000039 def tearDown(self):
40 """Shut down the open object"""
41 self.returned_obj.close()
Brett Cannon19691362003-04-29 05:08:06 +000042 os.remove(test_support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000043
Brett Cannon74bfd702003-04-25 09:39:47 +000044 def test_interface(self):
45 # Make sure object returned by urlopen() has the specified methods
46 for attr in ("read", "readline", "readlines", "fileno",
Georg Brandl9b0d46d2008-01-20 11:43:03 +000047 "close", "info", "geturl", "getcode", "__iter__"):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000048 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +000049 "object returned by urlopen() lacks %s attribute" %
50 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000051
Brett Cannon74bfd702003-04-25 09:39:47 +000052 def test_read(self):
53 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +000054
Brett Cannon74bfd702003-04-25 09:39:47 +000055 def test_readline(self):
56 self.assertEqual(self.text, self.returned_obj.readline())
57 self.assertEqual('', self.returned_obj.readline(),
58 "calling readline() after exhausting the file did not"
59 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +000060
Brett Cannon74bfd702003-04-25 09:39:47 +000061 def test_readlines(self):
62 lines_list = self.returned_obj.readlines()
63 self.assertEqual(len(lines_list), 1,
64 "readlines() returned the wrong number of lines")
65 self.assertEqual(lines_list[0], self.text,
66 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +000067
Brett Cannon74bfd702003-04-25 09:39:47 +000068 def test_fileno(self):
69 file_num = self.returned_obj.fileno()
Ezio Melottib0f5adc2010-01-24 16:58:36 +000070 self.assertIsInstance(file_num, int, "fileno() did not return an int")
Brett Cannon74bfd702003-04-25 09:39:47 +000071 self.assertEqual(os.read(file_num, len(self.text)), self.text,
72 "Reading on the file descriptor returned by fileno() "
73 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +000074
Brett Cannon74bfd702003-04-25 09:39:47 +000075 def test_close(self):
76 # Test close() by calling it hear and then having it be called again
77 # by the tearDown() method for the test
78 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +000079
Brett Cannon74bfd702003-04-25 09:39:47 +000080 def test_info(self):
Ezio Melottib0f5adc2010-01-24 16:58:36 +000081 self.assertIsInstance(self.returned_obj.info(), mimetools.Message)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000082
Brett Cannon74bfd702003-04-25 09:39:47 +000083 def test_geturl(self):
84 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +000085
Georg Brandl9b0d46d2008-01-20 11:43:03 +000086 def test_getcode(self):
87 self.assertEqual(self.returned_obj.getcode(), None)
88
Brett Cannon74bfd702003-04-25 09:39:47 +000089 def test_iter(self):
90 # Test iterator
91 # Don't need to count number of iterations since test would fail the
92 # instant it returned anything beyond the first line from the
93 # comparison
94 for line in self.returned_obj.__iter__():
95 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +000096
Benjamin Peterson2c7470d2008-09-21 21:27:51 +000097class ProxyTests(unittest.TestCase):
98
99 def setUp(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000100 # Records changes to env vars
101 self.env = test_support.EnvironmentVarGuard()
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000102 # Delete all proxy related env vars
Senthil Kumaran7a2ee0b2010-01-08 19:20:25 +0000103 for k in os.environ.keys():
Walter Dörwald4b965f62009-04-26 20:51:44 +0000104 if 'proxy' in k.lower():
Senthil Kumarandc61ec32009-10-01 01:50:13 +0000105 self.env.unset(k)
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000106
107 def tearDown(self):
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000108 # Restore all proxy related env vars
Walter Dörwald4b965f62009-04-26 20:51:44 +0000109 self.env.__exit__()
110 del self.env
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000111
112 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000113 self.env.set('NO_PROXY', 'localhost')
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000114 proxies = urllib.getproxies_environment()
115 # getproxies_environment use lowered case truncated (no '_proxy') keys
Ezio Melotti2623a372010-11-21 13:34:58 +0000116 self.assertEqual('localhost', proxies['no'])
Senthil Kumaranb5bd4c82011-08-06 12:24:33 +0800117 # List of no_proxies with space.
118 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
119 self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com'))
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000120
121
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000122class urlopen_HttpTests(unittest.TestCase):
123 """Test urlopen() opening a fake http connection."""
124
125 def fakehttp(self, fakedata):
126 class FakeSocket(StringIO.StringIO):
127 def sendall(self, str): pass
128 def makefile(self, mode, name): return self
129 def read(self, amt=None):
130 if self.closed: return ''
131 return StringIO.StringIO.read(self, amt)
132 def readline(self, length=None):
133 if self.closed: return ''
134 return StringIO.StringIO.readline(self, length)
135 class FakeHTTPConnection(httplib.HTTPConnection):
136 def connect(self):
137 self.sock = FakeSocket(fakedata)
138 assert httplib.HTTP._connection_class == httplib.HTTPConnection
139 httplib.HTTP._connection_class = FakeHTTPConnection
140
141 def unfakehttp(self):
142 httplib.HTTP._connection_class = httplib.HTTPConnection
143
144 def test_read(self):
145 self.fakehttp('Hello!')
146 try:
147 fp = urllib.urlopen("http://python.org/")
148 self.assertEqual(fp.readline(), 'Hello!')
149 self.assertEqual(fp.readline(), '')
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000150 self.assertEqual(fp.geturl(), 'http://python.org/')
151 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000152 finally:
153 self.unfakehttp()
154
Senthil Kumaran49c44082011-04-13 07:31:45 +0800155 def test_url_fragment(self):
156 # Issue #11703: geturl() omits fragments in the original URL.
157 url = 'http://docs.python.org/library/urllib.html#OK'
158 self.fakehttp('Hello!')
159 try:
160 fp = urllib.urlopen(url)
161 self.assertEqual(fp.geturl(), url)
162 finally:
163 self.unfakehttp()
164
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000165 def test_read_bogus(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000166 # urlopen() should raise IOError for many error codes.
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000167 self.fakehttp('''HTTP/1.1 401 Authentication Required
168Date: Wed, 02 Jan 2008 03:03:54 GMT
169Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
170Connection: close
171Content-Type: text/html; charset=iso-8859-1
172''')
173 try:
174 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
175 finally:
176 self.unfakehttp()
177
guido@google.comf1509302011-03-28 13:47:01 -0700178 def test_invalid_redirect(self):
179 # urlopen() should raise IOError for many error codes.
180 self.fakehttp("""HTTP/1.1 302 Found
181Date: Wed, 02 Jan 2008 03:03:54 GMT
182Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
183Location: file:README
184Connection: close
185Content-Type: text/html; charset=iso-8859-1
186""")
187 try:
188 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
189 finally:
190 self.unfakehttp()
191
Georg Brandlf66b6032007-03-14 08:27:52 +0000192 def test_empty_socket(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000193 # urlopen() raises IOError if the underlying socket does not send any
194 # data. (#1680230)
Georg Brandlf66b6032007-03-14 08:27:52 +0000195 self.fakehttp('')
196 try:
197 self.assertRaises(IOError, urllib.urlopen, 'http://something')
198 finally:
199 self.unfakehttp()
200
Brett Cannon19691362003-04-29 05:08:06 +0000201class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000202 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000203
Brett Cannon19691362003-04-29 05:08:06 +0000204 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000205 # Create a list of temporary files. Each item in the list is a file
206 # name (absolute path or relative to the current working directory).
207 # All files in this list will be deleted in the tearDown method. Note,
208 # this only helps to makes sure temporary files get deleted, but it
209 # does nothing about trying to close files that may still be open. It
210 # is the responsibility of the developer to properly close files even
211 # when exceptional conditions occur.
212 self.tempFiles = []
213
Brett Cannon19691362003-04-29 05:08:06 +0000214 # Create a temporary file.
Georg Brandl5a650a22005-08-26 08:51:34 +0000215 self.registerFileForCleanUp(test_support.TESTFN)
Brett Cannon19691362003-04-29 05:08:06 +0000216 self.text = 'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000217 try:
218 FILE = file(test_support.TESTFN, 'wb')
219 FILE.write(self.text)
220 FILE.close()
221 finally:
222 try: FILE.close()
223 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000224
225 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000226 # Delete the temporary files.
227 for each in self.tempFiles:
228 try: os.remove(each)
229 except: pass
230
231 def constructLocalFileUrl(self, filePath):
232 return "file://%s" % urllib.pathname2url(os.path.abspath(filePath))
233
234 def createNewTempFile(self, data=""):
235 """Creates a new temporary file containing the specified data,
236 registers the file for deletion during the test fixture tear down, and
237 returns the absolute path of the file."""
238
239 newFd, newFilePath = tempfile.mkstemp()
240 try:
241 self.registerFileForCleanUp(newFilePath)
242 newFile = os.fdopen(newFd, "wb")
243 newFile.write(data)
244 newFile.close()
245 finally:
246 try: newFile.close()
247 except: pass
248 return newFilePath
249
250 def registerFileForCleanUp(self, fileName):
251 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000252
253 def test_basic(self):
254 # Make sure that a local file just gets its own location returned and
255 # a headers value is returned.
256 result = urllib.urlretrieve("file:%s" % test_support.TESTFN)
257 self.assertEqual(result[0], test_support.TESTFN)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000258 self.assertIsInstance(result[1], mimetools.Message,
259 "did not get a mimetools.Message instance as "
260 "second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000261
262 def test_copy(self):
263 # Test that setting the filename argument works.
264 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000265 self.registerFileForCleanUp(second_temp)
266 result = urllib.urlretrieve(self.constructLocalFileUrl(
267 test_support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000268 self.assertEqual(second_temp, result[0])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000269 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000270 "made")
271 FILE = file(second_temp, 'rb')
272 try:
273 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000274 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000275 finally:
276 try: FILE.close()
277 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000278 self.assertEqual(self.text, text)
279
280 def test_reporthook(self):
281 # Make sure that the reporthook works.
282 def hooktester(count, block_size, total_size, count_holder=[0]):
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000283 self.assertIsInstance(count, int)
284 self.assertIsInstance(block_size, int)
285 self.assertIsInstance(total_size, int)
Brett Cannon19691362003-04-29 05:08:06 +0000286 self.assertEqual(count, count_holder[0])
287 count_holder[0] = count_holder[0] + 1
288 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000289 self.registerFileForCleanUp(second_temp)
290 urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN),
291 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()
299 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
300 test_support.TESTFN, hooktester)
301 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))
312 srcFileName = self.createNewTempFile("x" * 5)
313 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
314 test_support.TESTFN, hooktester)
315 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))
326 srcFileName = self.createNewTempFile("x" * 8193)
327 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
328 test_support.TESTFN, hooktester)
329 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
Brett Cannon74bfd702003-04-25 09:39:47 +0000336 According to RFC 2396 ("Uniform Resource Identifiers), to escape a
337 character you write it as '%' + <2 character US-ASCII hex value>. The Python
338 code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly.
339 Case does not matter on the hex letters.
340
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 "_.-"])
365 result = urllib.quote(do_not_quote)
366 self.assertEqual(do_not_quote, result,
367 "using quote(): %s != %s" % (do_not_quote, result))
368 result = urllib.quote_plus(do_not_quote)
369 self.assertEqual(do_not_quote, result,
370 "using quote_plus(): %s != %s" % (do_not_quote, result))
371
372 def test_default_safe(self):
373 # Test '/' is default value for 'safe' parameter
374 self.assertEqual(urllib.quote.func_defaults[0], '/')
375
376 def test_safe(self):
377 # Test setting 'safe' parameter does what it should do
378 quote_by_default = "<>"
379 result = urllib.quote(quote_by_default, safe=quote_by_default)
380 self.assertEqual(quote_by_default, result,
381 "using quote(): %s != %s" % (quote_by_default, result))
382 result = urllib.quote_plus(quote_by_default, safe=quote_by_default)
383 self.assertEqual(quote_by_default, result,
384 "using quote_plus(): %s != %s" %
385 (quote_by_default, result))
386
387 def test_default_quoting(self):
388 # Make sure all characters that should be quoted are by default sans
389 # space (separate test for that).
390 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
391 should_quote.append('<>#%"{}|\^[]`')
392 should_quote.append(chr(127)) # For 0x7F
393 should_quote = ''.join(should_quote)
394 for char in should_quote:
395 result = urllib.quote(char)
396 self.assertEqual(hexescape(char), result,
397 "using quote(): %s should be escaped to %s, not %s" %
398 (char, hexescape(char), result))
399 result = urllib.quote_plus(char)
400 self.assertEqual(hexescape(char), result,
401 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000402 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000403 (char, hexescape(char), result))
404 del should_quote
405 partial_quote = "ab[]cd"
406 expected = "ab%5B%5Dcd"
407 result = urllib.quote(partial_quote)
408 self.assertEqual(expected, result,
409 "using quote(): %s != %s" % (expected, result))
Senthil Kumaran0d4c34c2011-09-13 06:42:21 +0800410 result = urllib.quote_plus(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000411 self.assertEqual(expected, result,
412 "using quote_plus(): %s != %s" % (expected, result))
Senthil Kumaranc7743aa2010-07-19 17:35:50 +0000413 self.assertRaises(TypeError, urllib.quote, None)
Brett Cannon74bfd702003-04-25 09:39:47 +0000414
415 def test_quoting_space(self):
416 # Make sure quote() and quote_plus() handle spaces as specified in
417 # their unique way
418 result = urllib.quote(' ')
419 self.assertEqual(result, hexescape(' '),
420 "using quote(): %s != %s" % (result, hexescape(' ')))
421 result = urllib.quote_plus(' ')
422 self.assertEqual(result, '+',
423 "using quote_plus(): %s != +" % result)
424 given = "a b cd e f"
425 expect = given.replace(' ', hexescape(' '))
426 result = urllib.quote(given)
427 self.assertEqual(expect, result,
428 "using quote(): %s != %s" % (expect, result))
429 expect = given.replace(' ', '+')
430 result = urllib.quote_plus(given)
431 self.assertEqual(expect, result,
432 "using quote_plus(): %s != %s" % (expect, result))
433
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000434 def test_quoting_plus(self):
435 self.assertEqual(urllib.quote_plus('alpha+beta gamma'),
436 'alpha%2Bbeta+gamma')
437 self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'),
438 'alpha+beta+gamma')
439
Brett Cannon74bfd702003-04-25 09:39:47 +0000440class UnquotingTests(unittest.TestCase):
441 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000442
Brett Cannon74bfd702003-04-25 09:39:47 +0000443 See the doc string for quoting_Tests for details on quoting and such.
444
445 """
446
447 def test_unquoting(self):
448 # Make sure unquoting of all ASCII values works
449 escape_list = []
450 for num in range(128):
451 given = hexescape(chr(num))
452 expect = chr(num)
453 result = urllib.unquote(given)
454 self.assertEqual(expect, result,
455 "using unquote(): %s != %s" % (expect, result))
456 result = urllib.unquote_plus(given)
457 self.assertEqual(expect, result,
458 "using unquote_plus(): %s != %s" %
459 (expect, result))
460 escape_list.append(given)
461 escape_string = ''.join(escape_list)
462 del escape_list
463 result = urllib.unquote(escape_string)
464 self.assertEqual(result.count('%'), 1,
465 "using quote(): not all characters escaped; %s" %
466 result)
467 result = urllib.unquote(escape_string)
468 self.assertEqual(result.count('%'), 1,
469 "using unquote(): not all characters escaped: "
470 "%s" % result)
471
Senthil Kumaranf3e9b2a2010-03-18 12:14:15 +0000472 def test_unquoting_badpercent(self):
473 # Test unquoting on bad percent-escapes
474 given = '%xab'
475 expect = given
476 result = urllib.unquote(given)
477 self.assertEqual(expect, result, "using unquote(): %r != %r"
478 % (expect, result))
479 given = '%x'
480 expect = given
481 result = urllib.unquote(given)
482 self.assertEqual(expect, result, "using unquote(): %r != %r"
483 % (expect, result))
484 given = '%'
485 expect = given
486 result = urllib.unquote(given)
487 self.assertEqual(expect, result, "using unquote(): %r != %r"
488 % (expect, result))
489
490 def test_unquoting_mixed_case(self):
491 # Test unquoting on mixed-case hex digits in the percent-escapes
492 given = '%Ab%eA'
493 expect = '\xab\xea'
494 result = urllib.unquote(given)
495 self.assertEqual(expect, result, "using unquote(): %r != %r"
496 % (expect, result))
497
Brett Cannon74bfd702003-04-25 09:39:47 +0000498 def test_unquoting_parts(self):
499 # Make sure unquoting works when have non-quoted characters
500 # interspersed
501 given = 'ab%sd' % hexescape('c')
502 expect = "abcd"
503 result = urllib.unquote(given)
504 self.assertEqual(expect, result,
505 "using quote(): %s != %s" % (expect, result))
506 result = urllib.unquote_plus(given)
507 self.assertEqual(expect, result,
508 "using unquote_plus(): %s != %s" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000509
Brett Cannon74bfd702003-04-25 09:39:47 +0000510 def test_unquoting_plus(self):
511 # Test difference between unquote() and unquote_plus()
512 given = "are+there+spaces..."
513 expect = given
514 result = urllib.unquote(given)
515 self.assertEqual(expect, result,
516 "using unquote(): %s != %s" % (expect, result))
517 expect = given.replace('+', ' ')
518 result = urllib.unquote_plus(given)
519 self.assertEqual(expect, result,
520 "using unquote_plus(): %s != %s" % (expect, result))
521
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000522 def test_unquote_with_unicode(self):
523 r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc')
524 self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc')
525
Brett Cannon74bfd702003-04-25 09:39:47 +0000526class urlencode_Tests(unittest.TestCase):
527 """Tests for urlencode()"""
528
529 def help_inputtype(self, given, test_type):
530 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000531
Brett Cannon74bfd702003-04-25 09:39:47 +0000532 'given' must lead to only the pairs:
533 * 1st, 1
534 * 2nd, 2
535 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000536
Brett Cannon74bfd702003-04-25 09:39:47 +0000537 Test cannot assume anything about order. Docs make no guarantee and
538 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000539
Brett Cannon74bfd702003-04-25 09:39:47 +0000540 """
541 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
542 result = urllib.urlencode(given)
543 for expected in expect_somewhere:
Ezio Melottiaa980582010-01-23 23:04:36 +0000544 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000545 "testing %s: %s not found in %s" %
546 (test_type, expected, result))
547 self.assertEqual(result.count('&'), 2,
548 "testing %s: expected 2 '&'s; got %s" %
549 (test_type, result.count('&')))
550 amp_location = result.index('&')
551 on_amp_left = result[amp_location - 1]
552 on_amp_right = result[amp_location + 1]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000553 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000554 "testing %s: '&' not located in proper place in %s" %
555 (test_type, result))
556 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
557 "testing %s: "
558 "unexpected number of characters: %s != %s" %
559 (test_type, len(result), (5 * 3) + 2))
560
561 def test_using_mapping(self):
562 # Test passing in a mapping object as an argument.
563 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
564 "using dict as input type")
565
566 def test_using_sequence(self):
567 # Test passing in a sequence of two-item sequences as an argument.
568 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
569 "using sequence of two-item tuples as input")
570
571 def test_quoting(self):
572 # Make sure keys and values are quoted using quote_plus()
573 given = {"&":"="}
574 expect = "%s=%s" % (hexescape('&'), hexescape('='))
575 result = urllib.urlencode(given)
576 self.assertEqual(expect, result)
577 given = {"key name":"A bunch of pluses"}
578 expect = "key+name=A+bunch+of+pluses"
579 result = urllib.urlencode(given)
580 self.assertEqual(expect, result)
581
582 def test_doseq(self):
583 # Test that passing True for 'doseq' parameter works correctly
584 given = {'sequence':['1', '2', '3']}
585 expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3']))
586 result = urllib.urlencode(given)
587 self.assertEqual(expect, result)
588 result = urllib.urlencode(given, True)
589 for value in given["sequence"]:
590 expect = "sequence=%s" % value
Ezio Melottiaa980582010-01-23 23:04:36 +0000591 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000592 self.assertEqual(result.count('&'), 2,
593 "Expected 2 '&'s, got %s" % result.count('&'))
594
595class Pathname_Tests(unittest.TestCase):
596 """Test pathname2url() and url2pathname()"""
597
598 def test_basic(self):
599 # Make sure simple tests pass
600 expected_path = os.path.join("parts", "of", "a", "path")
601 expected_url = "parts/of/a/path"
602 result = urllib.pathname2url(expected_path)
603 self.assertEqual(expected_url, result,
604 "pathname2url() failed; %s != %s" %
605 (result, expected_url))
606 result = urllib.url2pathname(expected_url)
607 self.assertEqual(expected_path, result,
608 "url2pathame() failed; %s != %s" %
609 (result, expected_path))
610
611 def test_quoting(self):
612 # Test automatic quoting and unquoting works for pathnam2url() and
613 # url2pathname() respectively
614 given = os.path.join("needs", "quot=ing", "here")
615 expect = "needs/%s/here" % urllib.quote("quot=ing")
616 result = urllib.pathname2url(given)
617 self.assertEqual(expect, result,
618 "pathname2url() failed; %s != %s" %
619 (expect, result))
620 expect = given
621 result = urllib.url2pathname(result)
622 self.assertEqual(expect, result,
623 "url2pathname() failed; %s != %s" %
624 (expect, result))
625 given = os.path.join("make sure", "using_quote")
626 expect = "%s/using_quote" % urllib.quote("make sure")
627 result = urllib.pathname2url(given)
628 self.assertEqual(expect, result,
629 "pathname2url() failed; %s != %s" %
630 (expect, result))
631 given = "make+sure/using_unquote"
632 expect = os.path.join("make+sure", "using_unquote")
633 result = urllib.url2pathname(given)
634 self.assertEqual(expect, result,
635 "url2pathname() failed; %s != %s" %
636 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000637
Senthil Kumarana99b7612011-04-14 12:54:35 +0800638 @unittest.skipUnless(sys.platform == 'win32',
639 'test specific to the nturl2path library')
640 def test_ntpath(self):
641 given = ('/C:/', '///C:/', '/C|//')
642 expect = 'C:\\'
643 for url in given:
644 result = urllib.url2pathname(url)
645 self.assertEqual(expect, result,
646 'nturl2path.url2pathname() failed; %s != %s' %
647 (expect, result))
648 given = '///C|/path'
649 expect = 'C:\\path'
650 result = urllib.url2pathname(given)
651 self.assertEqual(expect, result,
652 'nturl2path.url2pathname() failed; %s != %s' %
653 (expect, result))
654
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000655class Utility_Tests(unittest.TestCase):
656 """Testcase to test the various utility functions in the urllib."""
657
658 def test_splitpasswd(self):
659 """Some of the password examples are not sensible, but it is added to
660 confirming to RFC2617 and addressing issue4675.
661 """
662 self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab'))
663 self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb'))
664 self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb'))
665 self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb'))
666 self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb'))
667 self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb'))
668 self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b'))
669
670
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000671class URLopener_Tests(unittest.TestCase):
672 """Testcase to test the open method of URLopener class."""
673
674 def test_quoted_open(self):
675 class DummyURLopener(urllib.URLopener):
676 def open_spam(self, url):
677 return url
678
679 self.assertEqual(DummyURLopener().open(
680 'spam://example/ /'),'//example/%20/')
681
Senthil Kumaran18d5a692010-02-20 22:05:34 +0000682 # test the safe characters are not quoted by urlopen
683 self.assertEqual(DummyURLopener().open(
684 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
685 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
686
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000687
Facundo Batistad9880d02007-05-25 04:20:22 +0000688# Just commented them out.
689# Can't really tell why keep failing in windows and sparc.
Ezio Melottic2077b02011-03-16 12:34:31 +0200690# Everywhere else they work ok, but on those machines, sometimes
Facundo Batistad9880d02007-05-25 04:20:22 +0000691# fail in one of the tests, sometimes in other. I have a linux, and
692# the tests go ok.
693# If anybody has one of the problematic enviroments, please help!
694# . Facundo
695#
696# def server(evt):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000697# import socket, time
Facundo Batistad9880d02007-05-25 04:20:22 +0000698# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
699# serv.settimeout(3)
700# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
701# serv.bind(("", 9093))
702# serv.listen(5)
703# try:
704# conn, addr = serv.accept()
705# conn.send("1 Hola mundo\n")
706# cantdata = 0
707# while cantdata < 13:
708# data = conn.recv(13-cantdata)
709# cantdata += len(data)
710# time.sleep(.3)
711# conn.send("2 No more lines\n")
712# conn.close()
713# except socket.timeout:
714# pass
715# finally:
716# serv.close()
717# evt.set()
718#
719# class FTPWrapperTests(unittest.TestCase):
720#
721# def setUp(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000722# import ftplib, time, threading
Facundo Batistad9880d02007-05-25 04:20:22 +0000723# ftplib.FTP.port = 9093
724# self.evt = threading.Event()
725# threading.Thread(target=server, args=(self.evt,)).start()
726# time.sleep(.1)
727#
728# def tearDown(self):
729# self.evt.wait()
730#
731# def testBasic(self):
732# # connects
733# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000734# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000735#
736# def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000737# # global default timeout is ignored
738# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000739# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000740# socket.setdefaulttimeout(30)
741# try:
742# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
743# finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000744# socket.setdefaulttimeout(None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000745# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000746# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000747#
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000748# def testTimeoutDefault(self):
749# # global default timeout is used
750# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000751# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000752# socket.setdefaulttimeout(30)
753# try:
754# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
755# finally:
756# socket.setdefaulttimeout(None)
757# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
758# ftp.close()
759#
760# def testTimeoutValue(self):
761# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
762# timeout=30)
763# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
764# ftp.close()
Facundo Batista711a54e2007-05-24 17:50:54 +0000765
Skip Montanaro080c9972001-01-28 21:12:22 +0000766
767
Brett Cannon74bfd702003-04-25 09:39:47 +0000768def test_main():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000769 import warnings
Brett Cannon672237d2008-09-09 00:49:16 +0000770 with warnings.catch_warnings():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000771 warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
772 DeprecationWarning)
773 test_support.run_unittest(
774 urlopen_FileTests,
775 urlopen_HttpTests,
776 urlretrieve_FileTests,
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000777 ProxyTests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000778 QuotingTests,
779 UnquotingTests,
780 urlencode_Tests,
781 Pathname_Tests,
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000782 Utility_Tests,
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000783 URLopener_Tests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000784 #FTPWrapperTests,
785 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000786
787
788
789if __name__ == '__main__':
790 test_main()