blob: 434d533e0561494e35f6e0d2697115b98a3e4925 [file] [log] [blame]
Brett Cannon74bfd702003-04-25 09:39:47 +00001"""Regresssion tests for urllib"""
2
Senthil Kumaranb31c87b2016-04-25 09:17:54 -07003import collections
Jeremy Hylton6102e292000-08-31 15:48:10 +00004import urllib
Hye-Shik Chang39aef792004-06-05 13:30:56 +00005import httplib
Serhiy Storchakaa898abd2014-09-06 21:41:39 +03006import io
Brett Cannon74bfd702003-04-25 09:39:47 +00007import unittest
Brett Cannon74bfd702003-04-25 09:39:47 +00008import os
Senthil Kumarana99b7612011-04-14 12:54:35 +08009import sys
Brett Cannon74bfd702003-04-25 09:39:47 +000010import mimetools
Georg Brandl5a650a22005-08-26 08:51:34 +000011import tempfile
Jeremy Hylton6102e292000-08-31 15:48:10 +000012
Senthil Kumaranbcd833f2012-01-11 00:09:24 +080013from test import test_support
14from base64 import b64encode
15
16
Brett Cannon74bfd702003-04-25 09:39:47 +000017def hexescape(char):
18 """Escape char as RFC 2396 specifies"""
19 hex_repr = hex(ord(char))[2:].upper()
20 if len(hex_repr) == 1:
21 hex_repr = "0%s" % hex_repr
22 return "%" + hex_repr
Jeremy Hylton6102e292000-08-31 15:48:10 +000023
Senthil Kumaran87e58552011-11-01 02:44:45 +080024
Serhiy Storchakaa898abd2014-09-06 21:41:39 +030025def fakehttp(fakedata):
26 class FakeSocket(io.BytesIO):
27
28 def sendall(self, data):
29 FakeHTTPConnection.buf = data
30
31 def makefile(self, *args, **kwds):
32 return self
33
34 def read(self, amt=None):
35 if self.closed:
36 return b""
37 return io.BytesIO.read(self, amt)
38
39 def readline(self, length=None):
40 if self.closed:
41 return b""
42 return io.BytesIO.readline(self, length)
43
44 class FakeHTTPConnection(httplib.HTTPConnection):
45
46 # buffer to store data for verification in urlopen tests.
47 buf = ""
Serhiy Storchakaa898abd2014-09-06 21:41:39 +030048
49 def connect(self):
Martin Panter3079bbe2016-05-16 01:07:13 +000050 self.sock = FakeSocket(self.fakedata)
51 self.__class__.fakesock = self.sock
52 FakeHTTPConnection.fakedata = fakedata
Serhiy Storchakaa898abd2014-09-06 21:41:39 +030053
54 return FakeHTTPConnection
55
56
Senthil Kumaran87e58552011-11-01 02:44:45 +080057class FakeHTTPMixin(object):
58 def fakehttp(self, fakedata):
Senthil Kumaran87e58552011-11-01 02:44:45 +080059 assert httplib.HTTP._connection_class == httplib.HTTPConnection
Senthil Kumaranbcd833f2012-01-11 00:09:24 +080060
Serhiy Storchakaa898abd2014-09-06 21:41:39 +030061 httplib.HTTP._connection_class = fakehttp(fakedata)
Senthil Kumaran87e58552011-11-01 02:44:45 +080062
63 def unfakehttp(self):
64 httplib.HTTP._connection_class = httplib.HTTPConnection
65
66
Brett Cannon74bfd702003-04-25 09:39:47 +000067class urlopen_FileTests(unittest.TestCase):
68 """Test urlopen() opening a temporary file.
Jeremy Hylton6102e292000-08-31 15:48:10 +000069
Brett Cannon74bfd702003-04-25 09:39:47 +000070 Try to test as much functionality as possible so as to cut down on reliance
Andrew M. Kuchlingf1a2f9e2004-06-29 13:07:53 +000071 on connecting to the Net for testing.
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000072
Brett Cannon74bfd702003-04-25 09:39:47 +000073 """
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000074
Brett Cannon74bfd702003-04-25 09:39:47 +000075 def setUp(self):
76 """Setup of a temp file to use for testing"""
77 self.text = "test_urllib: %s\n" % self.__class__.__name__
Guido van Rossum51735b02003-04-25 15:01:05 +000078 FILE = file(test_support.TESTFN, 'wb')
Brett Cannon74bfd702003-04-25 09:39:47 +000079 try:
80 FILE.write(self.text)
81 finally:
82 FILE.close()
83 self.pathname = test_support.TESTFN
84 self.returned_obj = urllib.urlopen("file:%s" % self.pathname)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000085
Brett Cannon74bfd702003-04-25 09:39:47 +000086 def tearDown(self):
87 """Shut down the open object"""
88 self.returned_obj.close()
Brett Cannon19691362003-04-29 05:08:06 +000089 os.remove(test_support.TESTFN)
Jeremy Hylton7ae51bf2000-09-14 16:59:07 +000090
Brett Cannon74bfd702003-04-25 09:39:47 +000091 def test_interface(self):
92 # Make sure object returned by urlopen() has the specified methods
93 for attr in ("read", "readline", "readlines", "fileno",
Georg Brandl9b0d46d2008-01-20 11:43:03 +000094 "close", "info", "geturl", "getcode", "__iter__"):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000095 self.assertTrue(hasattr(self.returned_obj, attr),
Brett Cannon74bfd702003-04-25 09:39:47 +000096 "object returned by urlopen() lacks %s attribute" %
97 attr)
Skip Montanaroe78b92a2001-01-20 20:22:30 +000098
Brett Cannon74bfd702003-04-25 09:39:47 +000099 def test_read(self):
100 self.assertEqual(self.text, self.returned_obj.read())
Skip Montanaro080c9972001-01-28 21:12:22 +0000101
Brett Cannon74bfd702003-04-25 09:39:47 +0000102 def test_readline(self):
103 self.assertEqual(self.text, self.returned_obj.readline())
104 self.assertEqual('', self.returned_obj.readline(),
105 "calling readline() after exhausting the file did not"
106 " return an empty string")
Skip Montanaro080c9972001-01-28 21:12:22 +0000107
Brett Cannon74bfd702003-04-25 09:39:47 +0000108 def test_readlines(self):
109 lines_list = self.returned_obj.readlines()
110 self.assertEqual(len(lines_list), 1,
111 "readlines() returned the wrong number of lines")
112 self.assertEqual(lines_list[0], self.text,
113 "readlines() returned improper text")
Skip Montanaro080c9972001-01-28 21:12:22 +0000114
Brett Cannon74bfd702003-04-25 09:39:47 +0000115 def test_fileno(self):
116 file_num = self.returned_obj.fileno()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000117 self.assertIsInstance(file_num, int, "fileno() did not return an int")
Brett Cannon74bfd702003-04-25 09:39:47 +0000118 self.assertEqual(os.read(file_num, len(self.text)), self.text,
119 "Reading on the file descriptor returned by fileno() "
120 "did not return the expected text")
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000121
Brett Cannon74bfd702003-04-25 09:39:47 +0000122 def test_close(self):
123 # Test close() by calling it hear and then having it be called again
124 # by the tearDown() method for the test
125 self.returned_obj.close()
Skip Montanaro080c9972001-01-28 21:12:22 +0000126
Brett Cannon74bfd702003-04-25 09:39:47 +0000127 def test_info(self):
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000128 self.assertIsInstance(self.returned_obj.info(), mimetools.Message)
Skip Montanaroe78b92a2001-01-20 20:22:30 +0000129
Brett Cannon74bfd702003-04-25 09:39:47 +0000130 def test_geturl(self):
131 self.assertEqual(self.returned_obj.geturl(), self.pathname)
Skip Montanaro080c9972001-01-28 21:12:22 +0000132
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000133 def test_getcode(self):
134 self.assertEqual(self.returned_obj.getcode(), None)
135
Brett Cannon74bfd702003-04-25 09:39:47 +0000136 def test_iter(self):
137 # Test iterator
138 # Don't need to count number of iterations since test would fail the
139 # instant it returned anything beyond the first line from the
140 # comparison
141 for line in self.returned_obj.__iter__():
142 self.assertEqual(line, self.text)
Skip Montanaro080c9972001-01-28 21:12:22 +0000143
Senthil Kumaran58c60622012-01-21 11:43:02 +0800144 def test_relativelocalfile(self):
145 self.assertRaises(ValueError,urllib.urlopen,'./' + self.pathname)
146
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000147class ProxyTests(unittest.TestCase):
148
149 def setUp(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000150 # Records changes to env vars
151 self.env = test_support.EnvironmentVarGuard()
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000152 # Delete all proxy related env vars
Senthil Kumaran7a2ee0b2010-01-08 19:20:25 +0000153 for k in os.environ.keys():
Walter Dörwald4b965f62009-04-26 20:51:44 +0000154 if 'proxy' in k.lower():
Senthil Kumarandc61ec32009-10-01 01:50:13 +0000155 self.env.unset(k)
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000156
157 def tearDown(self):
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000158 # Restore all proxy related env vars
Walter Dörwald4b965f62009-04-26 20:51:44 +0000159 self.env.__exit__()
160 del self.env
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000161
162 def test_getproxies_environment_keep_no_proxies(self):
Walter Dörwald4b965f62009-04-26 20:51:44 +0000163 self.env.set('NO_PROXY', 'localhost')
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000164 proxies = urllib.getproxies_environment()
165 # getproxies_environment use lowered case truncated (no '_proxy') keys
Ezio Melotti2623a372010-11-21 13:34:58 +0000166 self.assertEqual('localhost', proxies['no'])
Senthil Kumaranb5bd4c82011-08-06 12:24:33 +0800167 # List of no_proxies with space.
Senthil Kumaranb31c87b2016-04-25 09:17:54 -0700168 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com:1234')
Senthil Kumaranb5bd4c82011-08-06 12:24:33 +0800169 self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com'))
Senthil Kumaranb31c87b2016-04-25 09:17:54 -0700170 self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com:8888'))
171 self.assertTrue(urllib.proxy_bypass_environment('newdomain.com:1234'))
172
Martin Panter064ee4d2016-04-30 01:03:40 +0000173 def test_proxy_bypass_environment_host_match(self):
174 bypass = urllib.proxy_bypass_environment
175 self.env.set('NO_PROXY',
176 'localhost, anotherdomain.com, newdomain.com:1234')
177 self.assertTrue(bypass('localhost'))
178 self.assertTrue(bypass('LocalHost')) # MixedCase
179 self.assertTrue(bypass('LOCALHOST')) # UPPERCASE
180 self.assertTrue(bypass('newdomain.com:1234'))
181 self.assertTrue(bypass('anotherdomain.com:8888'))
182 self.assertTrue(bypass('www.newdomain.com:1234'))
183 self.assertFalse(bypass('prelocalhost'))
184 self.assertFalse(bypass('newdomain.com')) # no port
185 self.assertFalse(bypass('newdomain.com:1235')) # wrong port
Senthil Kumaranb31c87b2016-04-25 09:17:54 -0700186
187class ProxyTests_withOrderedEnv(unittest.TestCase):
188
189 def setUp(self):
190 # We need to test conditions, where variable order _is_ significant
191 self._saved_env = os.environ
192 # Monkey patch os.environ, start with empty fake environment
193 os.environ = collections.OrderedDict()
194
195 def tearDown(self):
196 os.environ = self._saved_env
197
198 def test_getproxies_environment_prefer_lowercase(self):
199 # Test lowercase preference with removal
200 os.environ['no_proxy'] = ''
201 os.environ['No_Proxy'] = 'localhost'
202 self.assertFalse(urllib.proxy_bypass_environment('localhost'))
203 self.assertFalse(urllib.proxy_bypass_environment('arbitrary'))
204 os.environ['http_proxy'] = ''
205 os.environ['HTTP_PROXY'] = 'http://somewhere:3128'
206 proxies = urllib.getproxies_environment()
207 self.assertEqual({}, proxies)
208 # Test lowercase preference of proxy bypass and correct matching including ports
209 os.environ['no_proxy'] = 'localhost, noproxy.com, my.proxy:1234'
210 os.environ['No_Proxy'] = 'xyz.com'
211 self.assertTrue(urllib.proxy_bypass_environment('localhost'))
212 self.assertTrue(urllib.proxy_bypass_environment('noproxy.com:5678'))
213 self.assertTrue(urllib.proxy_bypass_environment('my.proxy:1234'))
214 self.assertFalse(urllib.proxy_bypass_environment('my.proxy'))
215 self.assertFalse(urllib.proxy_bypass_environment('arbitrary'))
216 # Test lowercase preference with replacement
217 os.environ['http_proxy'] = 'http://somewhere:3128'
218 os.environ['Http_Proxy'] = 'http://somewhereelse:3128'
219 proxies = urllib.getproxies_environment()
220 self.assertEqual('http://somewhere:3128', proxies['http'])
Benjamin Peterson2c7470d2008-09-21 21:27:51 +0000221
222
Senthil Kumaran87e58552011-11-01 02:44:45 +0800223class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000224 """Test urlopen() opening a fake http connection."""
225
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000226 def test_read(self):
227 self.fakehttp('Hello!')
228 try:
229 fp = urllib.urlopen("http://python.org/")
230 self.assertEqual(fp.readline(), 'Hello!')
231 self.assertEqual(fp.readline(), '')
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000232 self.assertEqual(fp.geturl(), 'http://python.org/')
233 self.assertEqual(fp.getcode(), 200)
Hye-Shik Chang39aef792004-06-05 13:30:56 +0000234 finally:
235 self.unfakehttp()
236
Senthil Kumaran49c44082011-04-13 07:31:45 +0800237 def test_url_fragment(self):
238 # Issue #11703: geturl() omits fragments in the original URL.
239 url = 'http://docs.python.org/library/urllib.html#OK'
240 self.fakehttp('Hello!')
241 try:
242 fp = urllib.urlopen(url)
243 self.assertEqual(fp.geturl(), url)
244 finally:
245 self.unfakehttp()
246
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000247 def test_read_bogus(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000248 # urlopen() should raise IOError for many error codes.
Kurt B. Kaiser0f7c25d2008-01-02 04:11:28 +0000249 self.fakehttp('''HTTP/1.1 401 Authentication Required
250Date: Wed, 02 Jan 2008 03:03:54 GMT
251Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
252Connection: close
253Content-Type: text/html; charset=iso-8859-1
254''')
255 try:
256 self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
257 finally:
258 self.unfakehttp()
259
guido@google.comf1509302011-03-28 13:47:01 -0700260 def test_invalid_redirect(self):
261 # urlopen() should raise IOError for many error codes.
262 self.fakehttp("""HTTP/1.1 302 Found
263Date: Wed, 02 Jan 2008 03:03:54 GMT
264Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
265Location: file:README
266Connection: close
267Content-Type: text/html; charset=iso-8859-1
268""")
269 try:
Martin Panterade40972016-02-04 06:01:35 +0000270 msg = "Redirection to url 'file:"
271 with self.assertRaisesRegexp(IOError, msg):
272 urllib.urlopen("http://python.org/")
guido@google.comf1509302011-03-28 13:47:01 -0700273 finally:
274 self.unfakehttp()
275
Martin Panterade40972016-02-04 06:01:35 +0000276 def test_redirect_limit_independent(self):
277 # Ticket #12923: make sure independent requests each use their
278 # own retry limit.
279 for i in range(urllib.FancyURLopener().maxtries):
280 self.fakehttp(b'''HTTP/1.1 302 Found
281Location: file://guidocomputer.athome.com:/python/license
282Connection: close
283''')
284 try:
285 self.assertRaises(IOError, urllib.urlopen,
286 "http://something")
287 finally:
288 self.unfakehttp()
289
Georg Brandlf66b6032007-03-14 08:27:52 +0000290 def test_empty_socket(self):
Kurt B. Kaiser0a112322008-01-02 05:23:38 +0000291 # urlopen() raises IOError if the underlying socket does not send any
292 # data. (#1680230)
Georg Brandlf66b6032007-03-14 08:27:52 +0000293 self.fakehttp('')
294 try:
295 self.assertRaises(IOError, urllib.urlopen, 'http://something')
296 finally:
297 self.unfakehttp()
298
Senthil Kumaranf8d370e2012-10-27 03:48:40 -0700299 def test_missing_localfile(self):
300 self.assertRaises(IOError, urllib.urlopen,
301 'file://localhost/a/missing/file.py')
302 fd, tmp_file = tempfile.mkstemp()
303 tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
Senthil Kumarana085f002013-06-01 07:59:10 -0700304 self.assertTrue(os.path.exists(tmp_file))
Senthil Kumaranf8d370e2012-10-27 03:48:40 -0700305 try:
Senthil Kumaranf8d370e2012-10-27 03:48:40 -0700306 fp = urllib.urlopen(tmp_fileurl)
Senthil Kumarana085f002013-06-01 07:59:10 -0700307 fp.close()
Senthil Kumaranf8d370e2012-10-27 03:48:40 -0700308 finally:
309 os.close(fd)
Senthil Kumarana085f002013-06-01 07:59:10 -0700310 os.unlink(tmp_file)
Senthil Kumaranf8d370e2012-10-27 03:48:40 -0700311
312 self.assertFalse(os.path.exists(tmp_file))
313 self.assertRaises(IOError, urllib.urlopen, tmp_fileurl)
314
315 def test_ftp_nonexisting(self):
316 self.assertRaises(IOError, urllib.urlopen,
317 'ftp://localhost/not/existing/file.py')
318
319
Senthil Kumaranbcd833f2012-01-11 00:09:24 +0800320 def test_userpass_inurl(self):
321 self.fakehttp('Hello!')
322 try:
323 fakehttp_wrapper = httplib.HTTP._connection_class
324 fp = urllib.urlopen("http://user:pass@python.org/")
325 authorization = ("Authorization: Basic %s\r\n" %
326 b64encode('user:pass'))
327 # The authorization header must be in place
328 self.assertIn(authorization, fakehttp_wrapper.buf)
329 self.assertEqual(fp.readline(), "Hello!")
330 self.assertEqual(fp.readline(), "")
331 self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
332 self.assertEqual(fp.getcode(), 200)
333 finally:
334 self.unfakehttp()
335
336 def test_userpass_with_spaces_inurl(self):
337 self.fakehttp('Hello!')
338 try:
339 url = "http://a b:c d@python.org/"
340 fakehttp_wrapper = httplib.HTTP._connection_class
341 authorization = ("Authorization: Basic %s\r\n" %
342 b64encode('a b:c d'))
343 fp = urllib.urlopen(url)
344 # The authorization header must be in place
345 self.assertIn(authorization, fakehttp_wrapper.buf)
346 self.assertEqual(fp.readline(), "Hello!")
347 self.assertEqual(fp.readline(), "")
348 # the spaces are quoted in URL so no match
349 self.assertNotEqual(fp.geturl(), url)
350 self.assertEqual(fp.getcode(), 200)
351 finally:
352 self.unfakehttp()
353
354
Brett Cannon19691362003-04-29 05:08:06 +0000355class urlretrieve_FileTests(unittest.TestCase):
Brett Cannon74bfd702003-04-25 09:39:47 +0000356 """Test urllib.urlretrieve() on local files"""
Skip Montanaro080c9972001-01-28 21:12:22 +0000357
Brett Cannon19691362003-04-29 05:08:06 +0000358 def setUp(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000359 # Create a list of temporary files. Each item in the list is a file
360 # name (absolute path or relative to the current working directory).
361 # All files in this list will be deleted in the tearDown method. Note,
362 # this only helps to makes sure temporary files get deleted, but it
363 # does nothing about trying to close files that may still be open. It
364 # is the responsibility of the developer to properly close files even
365 # when exceptional conditions occur.
366 self.tempFiles = []
367
Brett Cannon19691362003-04-29 05:08:06 +0000368 # Create a temporary file.
Georg Brandl5a650a22005-08-26 08:51:34 +0000369 self.registerFileForCleanUp(test_support.TESTFN)
Brett Cannon19691362003-04-29 05:08:06 +0000370 self.text = 'testing urllib.urlretrieve'
Georg Brandl5a650a22005-08-26 08:51:34 +0000371 try:
372 FILE = file(test_support.TESTFN, 'wb')
373 FILE.write(self.text)
374 FILE.close()
375 finally:
376 try: FILE.close()
377 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000378
379 def tearDown(self):
Georg Brandl5a650a22005-08-26 08:51:34 +0000380 # Delete the temporary files.
381 for each in self.tempFiles:
382 try: os.remove(each)
383 except: pass
384
385 def constructLocalFileUrl(self, filePath):
386 return "file://%s" % urllib.pathname2url(os.path.abspath(filePath))
387
388 def createNewTempFile(self, data=""):
389 """Creates a new temporary file containing the specified data,
390 registers the file for deletion during the test fixture tear down, and
391 returns the absolute path of the file."""
392
393 newFd, newFilePath = tempfile.mkstemp()
394 try:
395 self.registerFileForCleanUp(newFilePath)
396 newFile = os.fdopen(newFd, "wb")
397 newFile.write(data)
398 newFile.close()
399 finally:
400 try: newFile.close()
401 except: pass
402 return newFilePath
403
404 def registerFileForCleanUp(self, fileName):
405 self.tempFiles.append(fileName)
Brett Cannon19691362003-04-29 05:08:06 +0000406
407 def test_basic(self):
408 # Make sure that a local file just gets its own location returned and
409 # a headers value is returned.
410 result = urllib.urlretrieve("file:%s" % test_support.TESTFN)
411 self.assertEqual(result[0], test_support.TESTFN)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000412 self.assertIsInstance(result[1], mimetools.Message,
413 "did not get a mimetools.Message instance as "
414 "second returned value")
Brett Cannon19691362003-04-29 05:08:06 +0000415
416 def test_copy(self):
417 # Test that setting the filename argument works.
418 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000419 self.registerFileForCleanUp(second_temp)
420 result = urllib.urlretrieve(self.constructLocalFileUrl(
421 test_support.TESTFN), second_temp)
Brett Cannon19691362003-04-29 05:08:06 +0000422 self.assertEqual(second_temp, result[0])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000423 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
Brett Cannon19691362003-04-29 05:08:06 +0000424 "made")
425 FILE = file(second_temp, 'rb')
426 try:
427 text = FILE.read()
Brett Cannon19691362003-04-29 05:08:06 +0000428 FILE.close()
Georg Brandl5a650a22005-08-26 08:51:34 +0000429 finally:
430 try: FILE.close()
431 except: pass
Brett Cannon19691362003-04-29 05:08:06 +0000432 self.assertEqual(self.text, text)
433
434 def test_reporthook(self):
435 # Make sure that the reporthook works.
436 def hooktester(count, block_size, total_size, count_holder=[0]):
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000437 self.assertIsInstance(count, int)
438 self.assertIsInstance(block_size, int)
439 self.assertIsInstance(total_size, int)
Brett Cannon19691362003-04-29 05:08:06 +0000440 self.assertEqual(count, count_holder[0])
441 count_holder[0] = count_holder[0] + 1
442 second_temp = "%s.2" % test_support.TESTFN
Georg Brandl5a650a22005-08-26 08:51:34 +0000443 self.registerFileForCleanUp(second_temp)
444 urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN),
445 second_temp, hooktester)
446
447 def test_reporthook_0_bytes(self):
448 # Test on zero length file. Should call reporthook only 1 time.
449 report = []
450 def hooktester(count, block_size, total_size, _report=report):
451 _report.append((count, block_size, total_size))
452 srcFileName = self.createNewTempFile()
453 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
454 test_support.TESTFN, hooktester)
455 self.assertEqual(len(report), 1)
456 self.assertEqual(report[0][2], 0)
457
458 def test_reporthook_5_bytes(self):
459 # Test on 5 byte file. Should call reporthook only 2 times (once when
460 # the "network connection" is established and once when the block is
461 # read). Since the block size is 8192 bytes, only one block read is
462 # required to read the entire file.
463 report = []
464 def hooktester(count, block_size, total_size, _report=report):
465 _report.append((count, block_size, total_size))
466 srcFileName = self.createNewTempFile("x" * 5)
467 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
468 test_support.TESTFN, hooktester)
469 self.assertEqual(len(report), 2)
470 self.assertEqual(report[0][1], 8192)
471 self.assertEqual(report[0][2], 5)
472
473 def test_reporthook_8193_bytes(self):
474 # Test on 8193 byte file. Should call reporthook only 3 times (once
475 # when the "network connection" is established, once for the next 8192
476 # bytes, and once for the last byte).
477 report = []
478 def hooktester(count, block_size, total_size, _report=report):
479 _report.append((count, block_size, total_size))
480 srcFileName = self.createNewTempFile("x" * 8193)
481 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName),
482 test_support.TESTFN, hooktester)
483 self.assertEqual(len(report), 3)
484 self.assertEqual(report[0][1], 8192)
485 self.assertEqual(report[0][2], 8193)
Skip Montanaro080c9972001-01-28 21:12:22 +0000486
Senthil Kumaran87e58552011-11-01 02:44:45 +0800487
488class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
489 """Test urllib.urlretrieve() using fake http connections"""
490
491 def test_short_content_raises_ContentTooShortError(self):
492 self.fakehttp('''HTTP/1.1 200 OK
493Date: Wed, 02 Jan 2008 03:03:54 GMT
494Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
495Connection: close
496Content-Length: 100
497Content-Type: text/html; charset=iso-8859-1
498
499FF
500''')
501
502 def _reporthook(par1, par2, par3):
503 pass
504
505 try:
506 self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve,
507 'http://example.com', reporthook=_reporthook)
508 finally:
509 self.unfakehttp()
510
511 def test_short_content_raises_ContentTooShortError_without_reporthook(self):
512 self.fakehttp('''HTTP/1.1 200 OK
513Date: Wed, 02 Jan 2008 03:03:54 GMT
514Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
515Connection: close
516Content-Length: 100
517Content-Type: text/html; charset=iso-8859-1
518
519FF
520''')
521 try:
522 self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve, 'http://example.com/')
523 finally:
524 self.unfakehttp()
525
Brett Cannon74bfd702003-04-25 09:39:47 +0000526class QuotingTests(unittest.TestCase):
527 """Tests for urllib.quote() and urllib.quote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000528
Brett Cannon74bfd702003-04-25 09:39:47 +0000529 According to RFC 2396 ("Uniform Resource Identifiers), to escape a
530 character you write it as '%' + <2 character US-ASCII hex value>. The Python
531 code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly.
532 Case does not matter on the hex letters.
533
534 The various character sets specified are:
Tim Petersc2659cf2003-05-12 20:19:37 +0000535
Brett Cannon74bfd702003-04-25 09:39:47 +0000536 Reserved characters : ";/?:@&=+$,"
537 Have special meaning in URIs and must be escaped if not being used for
538 their special meaning
539 Data characters : letters, digits, and "-_.!~*'()"
540 Unreserved and do not need to be escaped; can be, though, if desired
541 Control characters : 0x00 - 0x1F, 0x7F
542 Have no use in URIs so must be escaped
543 space : 0x20
544 Must be escaped
545 Delimiters : '<>#%"'
546 Must be escaped
547 Unwise : "{}|\^[]`"
548 Must be escaped
Tim Petersc2659cf2003-05-12 20:19:37 +0000549
Brett Cannon74bfd702003-04-25 09:39:47 +0000550 """
551
552 def test_never_quote(self):
553 # Make sure quote() does not quote letters, digits, and "_,.-"
554 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
555 "abcdefghijklmnopqrstuvwxyz",
556 "0123456789",
557 "_.-"])
558 result = urllib.quote(do_not_quote)
559 self.assertEqual(do_not_quote, result,
560 "using quote(): %s != %s" % (do_not_quote, result))
561 result = urllib.quote_plus(do_not_quote)
562 self.assertEqual(do_not_quote, result,
563 "using quote_plus(): %s != %s" % (do_not_quote, result))
564
565 def test_default_safe(self):
566 # Test '/' is default value for 'safe' parameter
567 self.assertEqual(urllib.quote.func_defaults[0], '/')
568
569 def test_safe(self):
570 # Test setting 'safe' parameter does what it should do
571 quote_by_default = "<>"
572 result = urllib.quote(quote_by_default, safe=quote_by_default)
573 self.assertEqual(quote_by_default, result,
574 "using quote(): %s != %s" % (quote_by_default, result))
575 result = urllib.quote_plus(quote_by_default, safe=quote_by_default)
576 self.assertEqual(quote_by_default, result,
577 "using quote_plus(): %s != %s" %
578 (quote_by_default, result))
579
580 def test_default_quoting(self):
581 # Make sure all characters that should be quoted are by default sans
582 # space (separate test for that).
583 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
584 should_quote.append('<>#%"{}|\^[]`')
585 should_quote.append(chr(127)) # For 0x7F
586 should_quote = ''.join(should_quote)
587 for char in should_quote:
588 result = urllib.quote(char)
589 self.assertEqual(hexescape(char), result,
590 "using quote(): %s should be escaped to %s, not %s" %
591 (char, hexescape(char), result))
592 result = urllib.quote_plus(char)
593 self.assertEqual(hexescape(char), result,
594 "using quote_plus(): "
Tim Petersc2659cf2003-05-12 20:19:37 +0000595 "%s should be escapes to %s, not %s" %
Brett Cannon74bfd702003-04-25 09:39:47 +0000596 (char, hexescape(char), result))
597 del should_quote
598 partial_quote = "ab[]cd"
599 expected = "ab%5B%5Dcd"
600 result = urllib.quote(partial_quote)
601 self.assertEqual(expected, result,
602 "using quote(): %s != %s" % (expected, result))
Senthil Kumaran0d4c34c2011-09-13 06:42:21 +0800603 result = urllib.quote_plus(partial_quote)
Brett Cannon74bfd702003-04-25 09:39:47 +0000604 self.assertEqual(expected, result,
605 "using quote_plus(): %s != %s" % (expected, result))
Senthil Kumaranc7743aa2010-07-19 17:35:50 +0000606 self.assertRaises(TypeError, urllib.quote, None)
Brett Cannon74bfd702003-04-25 09:39:47 +0000607
608 def test_quoting_space(self):
609 # Make sure quote() and quote_plus() handle spaces as specified in
610 # their unique way
611 result = urllib.quote(' ')
612 self.assertEqual(result, hexescape(' '),
613 "using quote(): %s != %s" % (result, hexescape(' ')))
614 result = urllib.quote_plus(' ')
615 self.assertEqual(result, '+',
616 "using quote_plus(): %s != +" % result)
617 given = "a b cd e f"
618 expect = given.replace(' ', hexescape(' '))
619 result = urllib.quote(given)
620 self.assertEqual(expect, result,
621 "using quote(): %s != %s" % (expect, result))
622 expect = given.replace(' ', '+')
623 result = urllib.quote_plus(given)
624 self.assertEqual(expect, result,
625 "using quote_plus(): %s != %s" % (expect, result))
626
Raymond Hettinger2bdec7b2005-09-10 14:30:09 +0000627 def test_quoting_plus(self):
628 self.assertEqual(urllib.quote_plus('alpha+beta gamma'),
629 'alpha%2Bbeta+gamma')
630 self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'),
631 'alpha+beta+gamma')
632
Brett Cannon74bfd702003-04-25 09:39:47 +0000633class UnquotingTests(unittest.TestCase):
634 """Tests for unquote() and unquote_plus()
Tim Petersc2659cf2003-05-12 20:19:37 +0000635
Brett Cannon74bfd702003-04-25 09:39:47 +0000636 See the doc string for quoting_Tests for details on quoting and such.
637
638 """
639
640 def test_unquoting(self):
641 # Make sure unquoting of all ASCII values works
642 escape_list = []
643 for num in range(128):
644 given = hexescape(chr(num))
645 expect = chr(num)
646 result = urllib.unquote(given)
647 self.assertEqual(expect, result,
648 "using unquote(): %s != %s" % (expect, result))
649 result = urllib.unquote_plus(given)
650 self.assertEqual(expect, result,
651 "using unquote_plus(): %s != %s" %
652 (expect, result))
653 escape_list.append(given)
654 escape_string = ''.join(escape_list)
655 del escape_list
656 result = urllib.unquote(escape_string)
657 self.assertEqual(result.count('%'), 1,
658 "using quote(): not all characters escaped; %s" %
659 result)
660 result = urllib.unquote(escape_string)
661 self.assertEqual(result.count('%'), 1,
662 "using unquote(): not all characters escaped: "
663 "%s" % result)
664
Senthil Kumaranf3e9b2a2010-03-18 12:14:15 +0000665 def test_unquoting_badpercent(self):
666 # Test unquoting on bad percent-escapes
667 given = '%xab'
668 expect = given
669 result = urllib.unquote(given)
670 self.assertEqual(expect, result, "using unquote(): %r != %r"
671 % (expect, result))
672 given = '%x'
673 expect = given
674 result = urllib.unquote(given)
675 self.assertEqual(expect, result, "using unquote(): %r != %r"
676 % (expect, result))
677 given = '%'
678 expect = given
679 result = urllib.unquote(given)
680 self.assertEqual(expect, result, "using unquote(): %r != %r"
681 % (expect, result))
682
683 def test_unquoting_mixed_case(self):
684 # Test unquoting on mixed-case hex digits in the percent-escapes
685 given = '%Ab%eA'
686 expect = '\xab\xea'
687 result = urllib.unquote(given)
688 self.assertEqual(expect, result, "using unquote(): %r != %r"
689 % (expect, result))
690
Brett Cannon74bfd702003-04-25 09:39:47 +0000691 def test_unquoting_parts(self):
692 # Make sure unquoting works when have non-quoted characters
693 # interspersed
694 given = 'ab%sd' % hexescape('c')
695 expect = "abcd"
696 result = urllib.unquote(given)
697 self.assertEqual(expect, result,
698 "using quote(): %s != %s" % (expect, result))
699 result = urllib.unquote_plus(given)
700 self.assertEqual(expect, result,
701 "using unquote_plus(): %s != %s" % (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000702
Brett Cannon74bfd702003-04-25 09:39:47 +0000703 def test_unquoting_plus(self):
704 # Test difference between unquote() and unquote_plus()
705 given = "are+there+spaces..."
706 expect = given
707 result = urllib.unquote(given)
708 self.assertEqual(expect, result,
709 "using unquote(): %s != %s" % (expect, result))
710 expect = given.replace('+', ' ')
711 result = urllib.unquote_plus(given)
712 self.assertEqual(expect, result,
713 "using unquote_plus(): %s != %s" % (expect, result))
714
Raymond Hettinger4b0f20d2005-10-15 16:41:53 +0000715 def test_unquote_with_unicode(self):
716 r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc')
717 self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc')
718
Brett Cannon74bfd702003-04-25 09:39:47 +0000719class urlencode_Tests(unittest.TestCase):
720 """Tests for urlencode()"""
721
722 def help_inputtype(self, given, test_type):
723 """Helper method for testing different input types.
Tim Petersc2659cf2003-05-12 20:19:37 +0000724
Brett Cannon74bfd702003-04-25 09:39:47 +0000725 'given' must lead to only the pairs:
726 * 1st, 1
727 * 2nd, 2
728 * 3rd, 3
Tim Petersc2659cf2003-05-12 20:19:37 +0000729
Brett Cannon74bfd702003-04-25 09:39:47 +0000730 Test cannot assume anything about order. Docs make no guarantee and
731 have possible dictionary input.
Tim Petersc2659cf2003-05-12 20:19:37 +0000732
Brett Cannon74bfd702003-04-25 09:39:47 +0000733 """
734 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
735 result = urllib.urlencode(given)
736 for expected in expect_somewhere:
Ezio Melottiaa980582010-01-23 23:04:36 +0000737 self.assertIn(expected, result,
Brett Cannon74bfd702003-04-25 09:39:47 +0000738 "testing %s: %s not found in %s" %
739 (test_type, expected, result))
740 self.assertEqual(result.count('&'), 2,
741 "testing %s: expected 2 '&'s; got %s" %
742 (test_type, result.count('&')))
743 amp_location = result.index('&')
744 on_amp_left = result[amp_location - 1]
745 on_amp_right = result[amp_location + 1]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000746 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
Brett Cannon74bfd702003-04-25 09:39:47 +0000747 "testing %s: '&' not located in proper place in %s" %
748 (test_type, result))
749 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
750 "testing %s: "
751 "unexpected number of characters: %s != %s" %
752 (test_type, len(result), (5 * 3) + 2))
753
754 def test_using_mapping(self):
755 # Test passing in a mapping object as an argument.
756 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
757 "using dict as input type")
758
759 def test_using_sequence(self):
760 # Test passing in a sequence of two-item sequences as an argument.
761 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
762 "using sequence of two-item tuples as input")
763
764 def test_quoting(self):
765 # Make sure keys and values are quoted using quote_plus()
766 given = {"&":"="}
767 expect = "%s=%s" % (hexescape('&'), hexescape('='))
768 result = urllib.urlencode(given)
769 self.assertEqual(expect, result)
770 given = {"key name":"A bunch of pluses"}
771 expect = "key+name=A+bunch+of+pluses"
772 result = urllib.urlencode(given)
773 self.assertEqual(expect, result)
774
775 def test_doseq(self):
776 # Test that passing True for 'doseq' parameter works correctly
777 given = {'sequence':['1', '2', '3']}
778 expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3']))
779 result = urllib.urlencode(given)
780 self.assertEqual(expect, result)
781 result = urllib.urlencode(given, True)
782 for value in given["sequence"]:
783 expect = "sequence=%s" % value
Ezio Melottiaa980582010-01-23 23:04:36 +0000784 self.assertIn(expect, result)
Brett Cannon74bfd702003-04-25 09:39:47 +0000785 self.assertEqual(result.count('&'), 2,
786 "Expected 2 '&'s, got %s" % result.count('&'))
787
788class Pathname_Tests(unittest.TestCase):
789 """Test pathname2url() and url2pathname()"""
790
791 def test_basic(self):
792 # Make sure simple tests pass
793 expected_path = os.path.join("parts", "of", "a", "path")
794 expected_url = "parts/of/a/path"
795 result = urllib.pathname2url(expected_path)
796 self.assertEqual(expected_url, result,
797 "pathname2url() failed; %s != %s" %
798 (result, expected_url))
799 result = urllib.url2pathname(expected_url)
800 self.assertEqual(expected_path, result,
801 "url2pathame() failed; %s != %s" %
802 (result, expected_path))
803
804 def test_quoting(self):
805 # Test automatic quoting and unquoting works for pathnam2url() and
806 # url2pathname() respectively
807 given = os.path.join("needs", "quot=ing", "here")
808 expect = "needs/%s/here" % urllib.quote("quot=ing")
809 result = urllib.pathname2url(given)
810 self.assertEqual(expect, result,
811 "pathname2url() failed; %s != %s" %
812 (expect, result))
813 expect = given
814 result = urllib.url2pathname(result)
815 self.assertEqual(expect, result,
816 "url2pathname() failed; %s != %s" %
817 (expect, result))
818 given = os.path.join("make sure", "using_quote")
819 expect = "%s/using_quote" % urllib.quote("make sure")
820 result = urllib.pathname2url(given)
821 self.assertEqual(expect, result,
822 "pathname2url() failed; %s != %s" %
823 (expect, result))
824 given = "make+sure/using_unquote"
825 expect = os.path.join("make+sure", "using_unquote")
826 result = urllib.url2pathname(given)
827 self.assertEqual(expect, result,
828 "url2pathname() failed; %s != %s" %
829 (expect, result))
Tim Petersc2659cf2003-05-12 20:19:37 +0000830
Senthil Kumarana99b7612011-04-14 12:54:35 +0800831 @unittest.skipUnless(sys.platform == 'win32',
832 'test specific to the nturl2path library')
833 def test_ntpath(self):
834 given = ('/C:/', '///C:/', '/C|//')
835 expect = 'C:\\'
836 for url in given:
837 result = urllib.url2pathname(url)
838 self.assertEqual(expect, result,
839 'nturl2path.url2pathname() failed; %s != %s' %
840 (expect, result))
841 given = '///C|/path'
842 expect = 'C:\\path'
843 result = urllib.url2pathname(given)
844 self.assertEqual(expect, result,
845 'nturl2path.url2pathname() failed; %s != %s' %
846 (expect, result))
847
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000848class Utility_Tests(unittest.TestCase):
849 """Testcase to test the various utility functions in the urllib."""
Serhiy Storchakaf0b630b2015-03-02 16:31:57 +0200850 # In Python 3 this test class is moved to test_urlparse.
851
852 def test_splittype(self):
853 splittype = urllib.splittype
854 self.assertEqual(splittype('type:opaquestring'), ('type', 'opaquestring'))
855 self.assertEqual(splittype('opaquestring'), (None, 'opaquestring'))
856 self.assertEqual(splittype(':opaquestring'), (None, ':opaquestring'))
857 self.assertEqual(splittype('type:'), ('type', ''))
858 self.assertEqual(splittype('type:opaque:string'), ('type', 'opaque:string'))
859
860 def test_splithost(self):
861 splithost = urllib.splithost
862 self.assertEqual(splithost('//www.example.org:80/foo/bar/baz.html'),
863 ('www.example.org:80', '/foo/bar/baz.html'))
864 self.assertEqual(splithost('//www.example.org:80'),
865 ('www.example.org:80', ''))
866 self.assertEqual(splithost('/foo/bar/baz.html'),
867 (None, '/foo/bar/baz.html'))
868
869 def test_splituser(self):
870 splituser = urllib.splituser
871 self.assertEqual(splituser('User:Pass@www.python.org:080'),
872 ('User:Pass', 'www.python.org:080'))
873 self.assertEqual(splituser('@www.python.org:080'),
874 ('', 'www.python.org:080'))
875 self.assertEqual(splituser('www.python.org:080'),
876 (None, 'www.python.org:080'))
877 self.assertEqual(splituser('User:Pass@'),
878 ('User:Pass', ''))
879 self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'),
880 ('User@example.com:Pass', 'www.python.org:080'))
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000881
882 def test_splitpasswd(self):
Serhiy Storchakaf0b630b2015-03-02 16:31:57 +0200883 # Some of the password examples are not sensible, but it is added to
884 # confirming to RFC2617 and addressing issue4675.
885 splitpasswd = urllib.splitpasswd
886 self.assertEqual(splitpasswd('user:ab'), ('user', 'ab'))
887 self.assertEqual(splitpasswd('user:a\nb'), ('user', 'a\nb'))
888 self.assertEqual(splitpasswd('user:a\tb'), ('user', 'a\tb'))
889 self.assertEqual(splitpasswd('user:a\rb'), ('user', 'a\rb'))
890 self.assertEqual(splitpasswd('user:a\fb'), ('user', 'a\fb'))
891 self.assertEqual(splitpasswd('user:a\vb'), ('user', 'a\vb'))
892 self.assertEqual(splitpasswd('user:a:b'), ('user', 'a:b'))
893 self.assertEqual(splitpasswd('user:a b'), ('user', 'a b'))
894 self.assertEqual(splitpasswd('user 2:ab'), ('user 2', 'ab'))
895 self.assertEqual(splitpasswd('user+1:a+b'), ('user+1', 'a+b'))
896 self.assertEqual(splitpasswd('user:'), ('user', ''))
897 self.assertEqual(splitpasswd('user'), ('user', None))
898 self.assertEqual(splitpasswd(':ab'), ('', 'ab'))
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000899
Serhiy Storchaka326b5ab2014-01-18 18:30:09 +0200900 def test_splitport(self):
901 splitport = urllib.splitport
902 self.assertEqual(splitport('parrot:88'), ('parrot', '88'))
903 self.assertEqual(splitport('parrot'), ('parrot', None))
904 self.assertEqual(splitport('parrot:'), ('parrot', None))
905 self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None))
906 self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None))
Serhiy Storchakaf0b630b2015-03-02 16:31:57 +0200907 self.assertEqual(splitport('[::1]:88'), ('[::1]', '88'))
908 self.assertEqual(splitport('[::1]'), ('[::1]', None))
909 self.assertEqual(splitport(':88'), ('', '88'))
Serhiy Storchaka326b5ab2014-01-18 18:30:09 +0200910
911 def test_splitnport(self):
912 splitnport = urllib.splitnport
913 self.assertEqual(splitnport('parrot:88'), ('parrot', 88))
914 self.assertEqual(splitnport('parrot'), ('parrot', -1))
915 self.assertEqual(splitnport('parrot', 55), ('parrot', 55))
916 self.assertEqual(splitnport('parrot:'), ('parrot', -1))
917 self.assertEqual(splitnport('parrot:', 55), ('parrot', 55))
918 self.assertEqual(splitnport('127.0.0.1'), ('127.0.0.1', -1))
919 self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55))
920 self.assertEqual(splitnport('parrot:cheese'), ('parrot', None))
921 self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None))
922
Serhiy Storchakaf0b630b2015-03-02 16:31:57 +0200923 def test_splitquery(self):
924 # Normal cases are exercised by other tests; ensure that we also
925 # catch cases with no port specified (testcase ensuring coverage)
926 splitquery = urllib.splitquery
927 self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
928 ('http://python.org/fake', 'foo=bar'))
929 self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
930 ('http://python.org/fake?foo=bar', ''))
931 self.assertEqual(splitquery('http://python.org/fake'),
932 ('http://python.org/fake', None))
933 self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar'))
934
935 def test_splittag(self):
936 splittag = urllib.splittag
937 self.assertEqual(splittag('http://example.com?foo=bar#baz'),
938 ('http://example.com?foo=bar', 'baz'))
939 self.assertEqual(splittag('http://example.com?foo=bar#'),
940 ('http://example.com?foo=bar', ''))
941 self.assertEqual(splittag('#baz'), ('', 'baz'))
942 self.assertEqual(splittag('http://example.com?foo=bar'),
943 ('http://example.com?foo=bar', None))
944 self.assertEqual(splittag('http://example.com?foo=bar#baz#boo'),
945 ('http://example.com?foo=bar#baz', 'boo'))
946
947 def test_splitattr(self):
948 splitattr = urllib.splitattr
949 self.assertEqual(splitattr('/path;attr1=value1;attr2=value2'),
950 ('/path', ['attr1=value1', 'attr2=value2']))
951 self.assertEqual(splitattr('/path;'), ('/path', ['']))
952 self.assertEqual(splitattr(';attr1=value1;attr2=value2'),
953 ('', ['attr1=value1', 'attr2=value2']))
954 self.assertEqual(splitattr('/path'), ('/path', []))
955
956 def test_splitvalue(self):
957 # Normal cases are exercised by other tests; test pathological cases
958 # with no key/value pairs. (testcase ensuring coverage)
959 splitvalue = urllib.splitvalue
960 self.assertEqual(splitvalue('foo=bar'), ('foo', 'bar'))
961 self.assertEqual(splitvalue('foo='), ('foo', ''))
962 self.assertEqual(splitvalue('=bar'), ('', 'bar'))
963 self.assertEqual(splitvalue('foobar'), ('foobar', None))
964 self.assertEqual(splitvalue('foo=bar=baz'), ('foo', 'bar=baz'))
965
966 def test_toBytes(self):
967 result = urllib.toBytes(u'http://www.python.org')
968 self.assertEqual(result, 'http://www.python.org')
969 self.assertRaises(UnicodeError, urllib.toBytes,
970 test_support.u(r'http://www.python.org/medi\u00e6val'))
971
972 def test_unwrap(self):
973 url = urllib.unwrap('<URL:type://host/path>')
974 self.assertEqual(url, 'type://host/path')
975
Senthil Kumaran5e95e762009-03-30 21:51:50 +0000976
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000977class URLopener_Tests(unittest.TestCase):
978 """Testcase to test the open method of URLopener class."""
979
980 def test_quoted_open(self):
981 class DummyURLopener(urllib.URLopener):
982 def open_spam(self, url):
983 return url
984
985 self.assertEqual(DummyURLopener().open(
986 'spam://example/ /'),'//example/%20/')
987
Senthil Kumaran18d5a692010-02-20 22:05:34 +0000988 # test the safe characters are not quoted by urlopen
989 self.assertEqual(DummyURLopener().open(
990 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
991 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
992
Senthil Kumaran7c2867f2009-04-21 03:24:19 +0000993
Facundo Batistad9880d02007-05-25 04:20:22 +0000994# Just commented them out.
995# Can't really tell why keep failing in windows and sparc.
Ezio Melottic2077b02011-03-16 12:34:31 +0200996# Everywhere else they work ok, but on those machines, sometimes
Facundo Batistad9880d02007-05-25 04:20:22 +0000997# fail in one of the tests, sometimes in other. I have a linux, and
998# the tests go ok.
Ezio Melotti419e23c2013-08-17 16:56:09 +0300999# If anybody has one of the problematic environments, please help!
Facundo Batistad9880d02007-05-25 04:20:22 +00001000# . Facundo
1001#
1002# def server(evt):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001003# import socket, time
Facundo Batistad9880d02007-05-25 04:20:22 +00001004# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1005# serv.settimeout(3)
1006# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1007# serv.bind(("", 9093))
1008# serv.listen(5)
1009# try:
1010# conn, addr = serv.accept()
1011# conn.send("1 Hola mundo\n")
1012# cantdata = 0
1013# while cantdata < 13:
1014# data = conn.recv(13-cantdata)
1015# cantdata += len(data)
1016# time.sleep(.3)
1017# conn.send("2 No more lines\n")
1018# conn.close()
1019# except socket.timeout:
1020# pass
1021# finally:
1022# serv.close()
1023# evt.set()
1024#
1025# class FTPWrapperTests(unittest.TestCase):
1026#
1027# def setUp(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001028# import ftplib, time, threading
Facundo Batistad9880d02007-05-25 04:20:22 +00001029# ftplib.FTP.port = 9093
1030# self.evt = threading.Event()
1031# threading.Thread(target=server, args=(self.evt,)).start()
1032# time.sleep(.1)
1033#
1034# def tearDown(self):
1035# self.evt.wait()
1036#
1037# def testBasic(self):
1038# # connects
1039# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001040# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +00001041#
1042# def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001043# # global default timeout is ignored
1044# import socket
Serhiy Storchaka528bed82014-02-08 14:49:55 +02001045# self.assertIsNone(socket.getdefaulttimeout())
Facundo Batistad9880d02007-05-25 04:20:22 +00001046# socket.setdefaulttimeout(30)
1047# try:
1048# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1049# finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001050# socket.setdefaulttimeout(None)
Facundo Batistad9880d02007-05-25 04:20:22 +00001051# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001052# ftp.close()
Facundo Batistad9880d02007-05-25 04:20:22 +00001053#
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001054# def testTimeoutDefault(self):
1055# # global default timeout is used
1056# import socket
Serhiy Storchaka528bed82014-02-08 14:49:55 +02001057# self.assertIsNone(socket.getdefaulttimeout())
Facundo Batista4f1b1ed2008-05-29 16:39:26 +00001058# socket.setdefaulttimeout(30)
1059# try:
1060# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1061# finally:
1062# socket.setdefaulttimeout(None)
1063# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1064# ftp.close()
1065#
1066# def testTimeoutValue(self):
1067# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1068# timeout=30)
1069# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1070# ftp.close()
Facundo Batista711a54e2007-05-24 17:50:54 +00001071
Skip Montanaro080c9972001-01-28 21:12:22 +00001072
1073
Brett Cannon74bfd702003-04-25 09:39:47 +00001074def test_main():
Brett Cannon8bb8fa52008-07-02 01:57:08 +00001075 import warnings
Brett Cannon672237d2008-09-09 00:49:16 +00001076 with warnings.catch_warnings():
Brett Cannon8bb8fa52008-07-02 01:57:08 +00001077 warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
1078 DeprecationWarning)
1079 test_support.run_unittest(
1080 urlopen_FileTests,
1081 urlopen_HttpTests,
1082 urlretrieve_FileTests,
Senthil Kumaran87e58552011-11-01 02:44:45 +08001083 urlretrieve_HttpTests,
Benjamin Peterson2c7470d2008-09-21 21:27:51 +00001084 ProxyTests,
Brett Cannon8bb8fa52008-07-02 01:57:08 +00001085 QuotingTests,
1086 UnquotingTests,
1087 urlencode_Tests,
1088 Pathname_Tests,
Senthil Kumaran5e95e762009-03-30 21:51:50 +00001089 Utility_Tests,
Senthil Kumaran7c2867f2009-04-21 03:24:19 +00001090 URLopener_Tests,
Senthil Kumaranb31c87b2016-04-25 09:17:54 -07001091 ProxyTests,
1092 ProxyTests_withOrderedEnv,
Brett Cannon8bb8fa52008-07-02 01:57:08 +00001093 #FTPWrapperTests,
1094 )
Brett Cannon74bfd702003-04-25 09:39:47 +00001095
1096
1097
1098if __name__ == '__main__':
1099 test_main()