blob: 10c8d90478f8e12561d0d2e06c8581cab7023837 [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
8import mimetools
Senthil Kumaran3ddc4352010-01-08 18:41:40 +00009import random
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()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000070 self.assertTrue(isinstance(file_num, int),
Brett Cannon74bfd702003-04-25 09:39:47 +000071 "fileno() did not return an int")
72 self.assertEqual(os.read(file_num, len(self.text)), self.text,
73 "Reading on the file descriptor returned by fileno() "
74 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +000075
Brett Cannon74bfd702003-04-25 09:39:47 +000076 def test_close(self):
77 # Test close() by calling it hear and then having it be called again
78 # by the tearDown() method for the test
79 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +000080
Brett Cannon74bfd702003-04-25 09:39:47 +000081 def test_info(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000082 self.assertTrue(isinstance(self.returned_obj.info(), mimetools.Message))
Skip Montanaroe78b92a2001-01-20 20:22:30 +000083
Brett Cannon74bfd702003-04-25 09:39:47 +000084 def test_geturl(self):
85 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +000086
Georg Brandl9b0d46d2008-01-20 11:43:03 +000087 def test_getcode(self):
88 self.assertEqual(self.returned_obj.getcode(), None)
89
Brett Cannon74bfd702003-04-25 09:39:47 +000090 def test_iter(self):
91 # Test iterator
92 # Don't need to count number of iterations since test would fail the
93 # instant it returned anything beyond the first line from the
94 # comparison
95 for line in self.returned_obj.__iter__():
96 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +000097
Benjamin Peterson2c7470d2008-09-21 21:27:51 +000098
99class ProxyTests(unittest.TestCase):
100
101 def setUp(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000102 # Records changes to env vars
103 self.env = test_support.EnvironmentVarGuard()
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000104 # Delete all proxy related env vars
Senthil Kumaran3ddc4352010-01-08 18:41:40 +0000105 for k in os.environ.keys():
Walter Dörwald4b965f62009-04-26 20:51:44 +0000106 if 'proxy' in k.lower():
Senthil Kumarandc61ec32009-10-01 01:50:13 +0000107 self.env.unset(k)
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000108
109 def tearDown(self):
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000110 # Restore all proxy related env vars
Walter Dörwald4b965f62009-04-26 20:51:44 +0000111 self.env.__exit__()
112 del self.env
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000113
114 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000115 self.env.set('NO_PROXY', 'localhost')
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000116 proxies = urllib.getproxies_environment()
117 # getproxies_environment use lowered case truncated (no '_proxy') keys
118 self.assertEquals('localhost', proxies['no'])
119
120
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000121class urlopen_HttpTests(unittest.TestCase):
122 """Test urlopen() opening a fake http connection."""
123
124 def fakehttp(self, fakedata):
125 class FakeSocket(StringIO.StringIO):
126 def sendall(self, str): pass
127 def makefile(self, mode, name): return self
128 def read(self, amt=None):
129 if self.closed: return ''
130 return StringIO.StringIO.read(self, amt)
131 def readline(self, length=None):
132 if self.closed: return ''
133 return StringIO.StringIO.readline(self, length)
134 class FakeHTTPConnection(httplib.HTTPConnection):
135 def connect(self):
136 self.sock = FakeSocket(fakedata)
137 assert httplib.HTTP._connection_class == httplib.HTTPConnection
138 httplib.HTTP._connection_class = FakeHTTPConnection
139
140 def unfakehttp(self):
141 httplib.HTTP._connection_class = httplib.HTTPConnection
142
143 def test_read(self):
144 self.fakehttp('Hello!')
145 try:
146 fp = urllib.urlopen("http://python.org/")
147 self.assertEqual(fp.readline(), 'Hello!')
148 self.assertEqual(fp.readline(), '')
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000149 self.assertEqual(fp.geturl(), 'http://python.org/')
150 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000151 finally:
152 self.unfakehttp()
153
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000154 def test_read_bogus(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000155 # urlopen() should raise IOError for many error codes.
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000156 self.fakehttp('''HTTP/1.1 401 Authentication Required
157Date: Wed, 02 Jan 2008 03:03:54 GMT
158Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
159Connection: close
160Content-Type: text/html; charset=iso-8859-1
161''')
162 try:
163 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
164 finally:
165 self.unfakehttp()
166
Georg Brandlf66b6032007-03-14 08:27:52 +0000167 def test_empty_socket(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000168 # urlopen() raises IOError if the underlying socket does not send any
169 # data. (#1680230)
Georg Brandlf66b6032007-03-14 08:27:52 +0000170 self.fakehttp('')
171 try:
172 self.assertRaises(IOError, urllib.urlopen, 'http://something')
173 finally:
174 self.unfakehttp()
175
Brett Cannon19691362003-04-29 05:08:06 +0000176class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000177 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000178
Brett Cannon19691362003-04-29 05:08:06 +0000179 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000180 # Create a list of temporary files. Each item in the list is a file
181 # name (absolute path or relative to the current working directory).
182 # All files in this list will be deleted in the tearDown method. Note,
183 # this only helps to makes sure temporary files get deleted, but it
184 # does nothing about trying to close files that may still be open. It
185 # is the responsibility of the developer to properly close files even
186 # when exceptional conditions occur.
187 self.tempFiles = []
188
Brett Cannon19691362003-04-29 05:08:06 +0000189 # Create a temporary file.
Georg Brandl5a650a22005-08-26 08:51:34 +0000190 self.registerFileForCleanUp(test_support.TESTFN)
Brett Cannon19691362003-04-29 05:08:06 +0000191 self.text = 'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000192 try:
193 FILE = file(test_support.TESTFN, 'wb')
194 FILE.write(self.text)
195 FILE.close()
196 finally:
197 try: FILE.close()
198 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000199
200 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000201 # Delete the temporary files.
202 for each in self.tempFiles:
203 try: os.remove(each)
204 except: pass
205
206 def constructLocalFileUrl(self, filePath):
207 return "file://%s" % urllib.pathname2url(os.path.abspath(filePath))
208
209 def createNewTempFile(self, data=""):
210 """Creates a new temporary file containing the specified data,
211 registers the file for deletion during the test fixture tear down, and
212 returns the absolute path of the file."""
213
214 newFd, newFilePath = tempfile.mkstemp()
215 try:
216 self.registerFileForCleanUp(newFilePath)
217 newFile = os.fdopen(newFd, "wb")
218 newFile.write(data)
219 newFile.close()
220 finally:
221 try: newFile.close()
222 except: pass
223 return newFilePath
224
225 def registerFileForCleanUp(self, fileName):
226 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000227
228 def test_basic(self):
229 # Make sure that a local file just gets its own location returned and
230 # a headers value is returned.
231 result = urllib.urlretrieve("file:%s" % test_support.TESTFN)
232 self.assertEqual(result[0], test_support.TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000233 self.assertTrue(isinstance(result[1], mimetools.Message),
Brett Cannon19691362003-04-29 05:08:06 +0000234 "did not get a mimetools.Message instance as second "
235 "returned value")
236
237 def test_copy(self):
238 # Test that setting the filename argument works.
239 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000240 self.registerFileForCleanUp(second_temp)
241 result = urllib.urlretrieve(self.constructLocalFileUrl(
242 test_support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000243 self.assertEqual(second_temp, result[0])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000244 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000245 "made")
246 FILE = file(second_temp, 'rb')
247 try:
248 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000249 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000250 finally:
251 try: FILE.close()
252 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000253 self.assertEqual(self.text, text)
254
255 def test_reporthook(self):
256 # Make sure that the reporthook works.
257 def hooktester(count, block_size, total_size, count_holder=[0]):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000258 self.assertTrue(isinstance(count, int))
259 self.assertTrue(isinstance(block_size, int))
260 self.assertTrue(isinstance(total_size, int))
Brett Cannon19691362003-04-29 05:08:06 +0000261 self.assertEqual(count, count_holder[0])
262 count_holder[0] = count_holder[0] + 1
263 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000264 self.registerFileForCleanUp(second_temp)
265 urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN),
266 second_temp, hooktester)
267
268 def test_reporthook_0_bytes(self):
269 # Test on zero length file. Should call reporthook only 1 time.
270 report = []
271 def hooktester(count, block_size, total_size, _report=report):
272 _report.append((count, block_size, total_size))
273 srcFileName = self.createNewTempFile()
274 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
275 test_support.TESTFN, hooktester)
276 self.assertEqual(len(report), 1)
277 self.assertEqual(report[0][2], 0)
278
279 def test_reporthook_5_bytes(self):
280 # Test on 5 byte file. Should call reporthook only 2 times (once when
281 # the "network connection" is established and once when the block is
282 # read). Since the block size is 8192 bytes, only one block read is
283 # required to read the entire file.
284 report = []
285 def hooktester(count, block_size, total_size, _report=report):
286 _report.append((count, block_size, total_size))
287 srcFileName = self.createNewTempFile("x" * 5)
288 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
289 test_support.TESTFN, hooktester)
290 self.assertEqual(len(report), 2)
291 self.assertEqual(report[0][1], 8192)
292 self.assertEqual(report[0][2], 5)
293
294 def test_reporthook_8193_bytes(self):
295 # Test on 8193 byte file. Should call reporthook only 3 times (once
296 # when the "network connection" is established, once for the next 8192
297 # bytes, and once for the last byte).
298 report = []
299 def hooktester(count, block_size, total_size, _report=report):
300 _report.append((count, block_size, total_size))
301 srcFileName = self.createNewTempFile("x" * 8193)
302 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
303 test_support.TESTFN, hooktester)
304 self.assertEqual(len(report), 3)
305 self.assertEqual(report[0][1], 8192)
306 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000307
Brett Cannon74bfd702003-04-25 09:39:47 +0000308class QuotingTests(unittest.TestCase):
309 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000310
Brett Cannon74bfd702003-04-25 09:39:47 +0000311 According to RFC 2396 ("Uniform Resource Identifiers), to escape a
312 character you write it as '%' + <2 character US-ASCII hex value>. The Python
313 code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly.
314 Case does not matter on the hex letters.
315
316 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000317
Brett Cannon74bfd702003-04-25 09:39:47 +0000318 Reserved characters : ";/?:@&=+$,"
319 Have special meaning in URIs and must be escaped if not being used for
320 their special meaning
321 Data characters : letters, digits, and "-_.!~*'()"
322 Unreserved and do not need to be escaped; can be, though, if desired
323 Control characters : 0x00 - 0x1F, 0x7F
324 Have no use in URIs so must be escaped
325 space : 0x20
326 Must be escaped
327 Delimiters : '<>#%"'
328 Must be escaped
329 Unwise : "{}|\^[]`"
330 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000331
Brett Cannon74bfd702003-04-25 09:39:47 +0000332 """
333
334 def test_never_quote(self):
335 # Make sure quote() does not quote letters, digits, and "_,.-"
336 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
337 "abcdefghijklmnopqrstuvwxyz",
338 "0123456789",
339 "_.-"])
340 result = urllib.quote(do_not_quote)
341 self.assertEqual(do_not_quote, result,
342 "using quote(): %s != %s" % (do_not_quote, result))
343 result = urllib.quote_plus(do_not_quote)
344 self.assertEqual(do_not_quote, result,
345 "using quote_plus(): %s != %s" % (do_not_quote, result))
346
347 def test_default_safe(self):
348 # Test '/' is default value for 'safe' parameter
349 self.assertEqual(urllib.quote.func_defaults[0], '/')
350
351 def test_safe(self):
352 # Test setting 'safe' parameter does what it should do
353 quote_by_default = "<>"
354 result = urllib.quote(quote_by_default, safe=quote_by_default)
355 self.assertEqual(quote_by_default, result,
356 "using quote(): %s != %s" % (quote_by_default, result))
357 result = urllib.quote_plus(quote_by_default, safe=quote_by_default)
358 self.assertEqual(quote_by_default, result,
359 "using quote_plus(): %s != %s" %
360 (quote_by_default, result))
361
362 def test_default_quoting(self):
363 # Make sure all characters that should be quoted are by default sans
364 # space (separate test for that).
365 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
366 should_quote.append('<>#%"{}|\^[]`')
367 should_quote.append(chr(127)) # For 0x7F
368 should_quote = ''.join(should_quote)
369 for char in should_quote:
370 result = urllib.quote(char)
371 self.assertEqual(hexescape(char), result,
372 "using quote(): %s should be escaped to %s, not %s" %
373 (char, hexescape(char), result))
374 result = urllib.quote_plus(char)
375 self.assertEqual(hexescape(char), result,
376 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000377 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000378 (char, hexescape(char), result))
379 del should_quote
380 partial_quote = "ab[]cd"
381 expected = "ab%5B%5Dcd"
382 result = urllib.quote(partial_quote)
383 self.assertEqual(expected, result,
384 "using quote(): %s != %s" % (expected, result))
385 self.assertEqual(expected, result,
386 "using quote_plus(): %s != %s" % (expected, result))
387
388 def test_quoting_space(self):
389 # Make sure quote() and quote_plus() handle spaces as specified in
390 # their unique way
391 result = urllib.quote(' ')
392 self.assertEqual(result, hexescape(' '),
393 "using quote(): %s != %s" % (result, hexescape(' ')))
394 result = urllib.quote_plus(' ')
395 self.assertEqual(result, '+',
396 "using quote_plus(): %s != +" % result)
397 given = "a b cd e f"
398 expect = given.replace(' ', hexescape(' '))
399 result = urllib.quote(given)
400 self.assertEqual(expect, result,
401 "using quote(): %s != %s" % (expect, result))
402 expect = given.replace(' ', '+')
403 result = urllib.quote_plus(given)
404 self.assertEqual(expect, result,
405 "using quote_plus(): %s != %s" % (expect, result))
406
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000407 def test_quoting_plus(self):
408 self.assertEqual(urllib.quote_plus('alpha+beta gamma'),
409 'alpha%2Bbeta+gamma')
410 self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'),
411 'alpha+beta+gamma')
412
Senthil Kumaran3ddc4352010-01-08 18:41:40 +0000413 def test_quote_leak(self):
414 # bug 5596 - highlight the refleak in the internal _safemaps cache
415 safe = ''.join(chr(random.randrange(128)) for i in '123456')
416 text = 'abcdefghijklmnopqrstuvwxyz'
417 result = urllib.quote(text, safe=safe)
418 self.assertEqual(result, text)
419
Brett Cannon74bfd702003-04-25 09:39:47 +0000420class UnquotingTests(unittest.TestCase):
421 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000422
Brett Cannon74bfd702003-04-25 09:39:47 +0000423 See the doc string for quoting_Tests for details on quoting and such.
424
425 """
426
427 def test_unquoting(self):
428 # Make sure unquoting of all ASCII values works
429 escape_list = []
430 for num in range(128):
431 given = hexescape(chr(num))
432 expect = chr(num)
433 result = urllib.unquote(given)
434 self.assertEqual(expect, result,
435 "using unquote(): %s != %s" % (expect, result))
436 result = urllib.unquote_plus(given)
437 self.assertEqual(expect, result,
438 "using unquote_plus(): %s != %s" %
439 (expect, result))
440 escape_list.append(given)
441 escape_string = ''.join(escape_list)
442 del escape_list
443 result = urllib.unquote(escape_string)
444 self.assertEqual(result.count('%'), 1,
445 "using quote(): not all characters escaped; %s" %
446 result)
447 result = urllib.unquote(escape_string)
448 self.assertEqual(result.count('%'), 1,
449 "using unquote(): not all characters escaped: "
450 "%s" % result)
451
452 def test_unquoting_parts(self):
453 # Make sure unquoting works when have non-quoted characters
454 # interspersed
455 given = 'ab%sd' % hexescape('c')
456 expect = "abcd"
457 result = urllib.unquote(given)
458 self.assertEqual(expect, result,
459 "using quote(): %s != %s" % (expect, result))
460 result = urllib.unquote_plus(given)
461 self.assertEqual(expect, result,
462 "using unquote_plus(): %s != %s" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000463
Brett Cannon74bfd702003-04-25 09:39:47 +0000464 def test_unquoting_plus(self):
465 # Test difference between unquote() and unquote_plus()
466 given = "are+there+spaces..."
467 expect = given
468 result = urllib.unquote(given)
469 self.assertEqual(expect, result,
470 "using unquote(): %s != %s" % (expect, result))
471 expect = given.replace('+', ' ')
472 result = urllib.unquote_plus(given)
473 self.assertEqual(expect, result,
474 "using unquote_plus(): %s != %s" % (expect, result))
475
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000476 def test_unquote_with_unicode(self):
477 r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc')
478 self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc')
479
Brett Cannon74bfd702003-04-25 09:39:47 +0000480class urlencode_Tests(unittest.TestCase):
481 """Tests for urlencode()"""
482
483 def help_inputtype(self, given, test_type):
484 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000485
Brett Cannon74bfd702003-04-25 09:39:47 +0000486 'given' must lead to only the pairs:
487 * 1st, 1
488 * 2nd, 2
489 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000490
Brett Cannon74bfd702003-04-25 09:39:47 +0000491 Test cannot assume anything about order. Docs make no guarantee and
492 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000493
Brett Cannon74bfd702003-04-25 09:39:47 +0000494 """
495 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
496 result = urllib.urlencode(given)
497 for expected in expect_somewhere:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000498 self.assertTrue(expected in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000499 "testing %s: %s not found in %s" %
500 (test_type, expected, result))
501 self.assertEqual(result.count('&'), 2,
502 "testing %s: expected 2 '&'s; got %s" %
503 (test_type, result.count('&')))
504 amp_location = result.index('&')
505 on_amp_left = result[amp_location - 1]
506 on_amp_right = result[amp_location + 1]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000507 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000508 "testing %s: '&' not located in proper place in %s" %
509 (test_type, result))
510 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
511 "testing %s: "
512 "unexpected number of characters: %s != %s" %
513 (test_type, len(result), (5 * 3) + 2))
514
515 def test_using_mapping(self):
516 # Test passing in a mapping object as an argument.
517 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
518 "using dict as input type")
519
520 def test_using_sequence(self):
521 # Test passing in a sequence of two-item sequences as an argument.
522 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
523 "using sequence of two-item tuples as input")
524
525 def test_quoting(self):
526 # Make sure keys and values are quoted using quote_plus()
527 given = {"&":"="}
528 expect = "%s=%s" % (hexescape('&'), hexescape('='))
529 result = urllib.urlencode(given)
530 self.assertEqual(expect, result)
531 given = {"key name":"A bunch of pluses"}
532 expect = "key+name=A+bunch+of+pluses"
533 result = urllib.urlencode(given)
534 self.assertEqual(expect, result)
535
536 def test_doseq(self):
537 # Test that passing True for 'doseq' parameter works correctly
538 given = {'sequence':['1', '2', '3']}
539 expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3']))
540 result = urllib.urlencode(given)
541 self.assertEqual(expect, result)
542 result = urllib.urlencode(given, True)
543 for value in given["sequence"]:
544 expect = "sequence=%s" % value
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000545 self.assertTrue(expect in result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000546 "%s not found in %s" % (expect, result))
547 self.assertEqual(result.count('&'), 2,
548 "Expected 2 '&'s, got %s" % result.count('&'))
549
550class Pathname_Tests(unittest.TestCase):
551 """Test pathname2url() and url2pathname()"""
552
553 def test_basic(self):
554 # Make sure simple tests pass
555 expected_path = os.path.join("parts", "of", "a", "path")
556 expected_url = "parts/of/a/path"
557 result = urllib.pathname2url(expected_path)
558 self.assertEqual(expected_url, result,
559 "pathname2url() failed; %s != %s" %
560 (result, expected_url))
561 result = urllib.url2pathname(expected_url)
562 self.assertEqual(expected_path, result,
563 "url2pathame() failed; %s != %s" %
564 (result, expected_path))
565
566 def test_quoting(self):
567 # Test automatic quoting and unquoting works for pathnam2url() and
568 # url2pathname() respectively
569 given = os.path.join("needs", "quot=ing", "here")
570 expect = "needs/%s/here" % urllib.quote("quot=ing")
571 result = urllib.pathname2url(given)
572 self.assertEqual(expect, result,
573 "pathname2url() failed; %s != %s" %
574 (expect, result))
575 expect = given
576 result = urllib.url2pathname(result)
577 self.assertEqual(expect, result,
578 "url2pathname() failed; %s != %s" %
579 (expect, result))
580 given = os.path.join("make sure", "using_quote")
581 expect = "%s/using_quote" % urllib.quote("make sure")
582 result = urllib.pathname2url(given)
583 self.assertEqual(expect, result,
584 "pathname2url() failed; %s != %s" %
585 (expect, result))
586 given = "make+sure/using_unquote"
587 expect = os.path.join("make+sure", "using_unquote")
588 result = urllib.url2pathname(given)
589 self.assertEqual(expect, result,
590 "url2pathname() failed; %s != %s" %
591 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000592
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000593class Utility_Tests(unittest.TestCase):
594 """Testcase to test the various utility functions in the urllib."""
595
596 def test_splitpasswd(self):
597 """Some of the password examples are not sensible, but it is added to
598 confirming to RFC2617 and addressing issue4675.
599 """
600 self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab'))
601 self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb'))
602 self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb'))
603 self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb'))
604 self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb'))
605 self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb'))
606 self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b'))
607
608
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000609class URLopener_Tests(unittest.TestCase):
610 """Testcase to test the open method of URLopener class."""
611
612 def test_quoted_open(self):
613 class DummyURLopener(urllib.URLopener):
614 def open_spam(self, url):
615 return url
616
617 self.assertEqual(DummyURLopener().open(
618 'spam://example/ /'),'//example/%20/')
619
620
Facundo Batistad9880d02007-05-25 04:20:22 +0000621# Just commented them out.
622# Can't really tell why keep failing in windows and sparc.
623# Everywhere else they work ok, but on those machines, someteimes
624# fail in one of the tests, sometimes in other. I have a linux, and
625# the tests go ok.
626# If anybody has one of the problematic enviroments, please help!
627# . Facundo
628#
629# def server(evt):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000630# import socket, time
Facundo Batistad9880d02007-05-25 04:20:22 +0000631# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
632# serv.settimeout(3)
633# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
634# serv.bind(("", 9093))
635# serv.listen(5)
636# try:
637# conn, addr = serv.accept()
638# conn.send("1 Hola mundo\n")
639# cantdata = 0
640# while cantdata < 13:
641# data = conn.recv(13-cantdata)
642# cantdata += len(data)
643# time.sleep(.3)
644# conn.send("2 No more lines\n")
645# conn.close()
646# except socket.timeout:
647# pass
648# finally:
649# serv.close()
650# evt.set()
651#
652# class FTPWrapperTests(unittest.TestCase):
653#
654# def setUp(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000655# import ftplib, time, threading
Facundo Batistad9880d02007-05-25 04:20:22 +0000656# ftplib.FTP.port = 9093
657# self.evt = threading.Event()
658# threading.Thread(target=server, args=(self.evt,)).start()
659# time.sleep(.1)
660#
661# def tearDown(self):
662# self.evt.wait()
663#
664# def testBasic(self):
665# # connects
666# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000667# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000668#
669# def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000670# # global default timeout is ignored
671# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000672# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000673# socket.setdefaulttimeout(30)
674# try:
675# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
676# finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000677# socket.setdefaulttimeout(None)
Facundo Batistad9880d02007-05-25 04:20:22 +0000678# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000679# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +0000680#
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000681# def testTimeoutDefault(self):
682# # global default timeout is used
683# import socket
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000684# self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000685# socket.setdefaulttimeout(30)
686# try:
687# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
688# finally:
689# socket.setdefaulttimeout(None)
690# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
691# ftp.close()
692#
693# def testTimeoutValue(self):
694# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
695# timeout=30)
696# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
697# ftp.close()
Facundo Batista711a54e2007-05-24 17:50:54 +0000698
Skip Montanaro080c9972001-01-28 21:12:22 +0000699
700
Brett Cannon74bfd702003-04-25 09:39:47 +0000701def test_main():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000702 import warnings
Brett Cannon672237d2008-09-09 00:49:16 +0000703 with warnings.catch_warnings():
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000704 warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
705 DeprecationWarning)
706 test_support.run_unittest(
707 urlopen_FileTests,
708 urlopen_HttpTests,
709 urlretrieve_FileTests,
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000710 ProxyTests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000711 QuotingTests,
712 UnquotingTests,
713 urlencode_Tests,
714 Pathname_Tests,
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000715 Utility_Tests,
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000716 URLopener_Tests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000717 #FTPWrapperTests,
718 )
Brett Cannon74bfd702003-04-25 09:39:47 +0000719
720
721
722if __name__ == '__main__':
723 test_main()