+++ /dev/null
-#!/usr/bin/env python\r
-\r
-import unittest\r
-from test import test_support\r
-\r
-import socket\r
-import urllib\r
-import sys\r
-import os\r
-import time\r
-\r
-mimetools = test_support.import_module("mimetools", deprecated=True)\r
-\r
-\r
-def _open_with_retry(func, host, *args, **kwargs):\r
- # Connecting to remote hosts is flaky. Make it more robust\r
- # by retrying the connection several times.\r
- for i in range(3):\r
- try:\r
- return func(host, *args, **kwargs)\r
- except IOError, last_exc:\r
- continue\r
- except:\r
- raise\r
- raise last_exc\r
-\r
-\r
-class URLTimeoutTest(unittest.TestCase):\r
-\r
- TIMEOUT = 10.0\r
-\r
- def setUp(self):\r
- socket.setdefaulttimeout(self.TIMEOUT)\r
-\r
- def tearDown(self):\r
- socket.setdefaulttimeout(None)\r
-\r
- def testURLread(self):\r
- f = _open_with_retry(urllib.urlopen, "http://www.python.org/")\r
- x = f.read()\r
-\r
-class urlopenNetworkTests(unittest.TestCase):\r
- """Tests urllib.urlopen using the network.\r
-\r
- These tests are not exhaustive. Assuming that testing using files does a\r
- good job overall of some of the basic interface features. There are no\r
- tests exercising the optional 'data' and 'proxies' arguments. No tests\r
- for transparent redirection have been written.\r
-\r
- setUp is not used for always constructing a connection to\r
- http://www.python.org/ since there a few tests that don't use that address\r
- and making a connection is expensive enough to warrant minimizing unneeded\r
- connections.\r
-\r
- """\r
-\r
- def urlopen(self, *args):\r
- return _open_with_retry(urllib.urlopen, *args)\r
-\r
- def test_basic(self):\r
- # Simple test expected to pass.\r
- open_url = self.urlopen("http://www.python.org/")\r
- for attr in ("read", "readline", "readlines", "fileno", "close",\r
- "info", "geturl"):\r
- self.assertTrue(hasattr(open_url, attr), "object returned from "\r
- "urlopen lacks the %s attribute" % attr)\r
- try:\r
- self.assertTrue(open_url.read(), "calling 'read' failed")\r
- finally:\r
- open_url.close()\r
-\r
- def test_readlines(self):\r
- # Test both readline and readlines.\r
- open_url = self.urlopen("http://www.python.org/")\r
- try:\r
- self.assertIsInstance(open_url.readline(), basestring,\r
- "readline did not return a string")\r
- self.assertIsInstance(open_url.readlines(), list,\r
- "readlines did not return a list")\r
- finally:\r
- open_url.close()\r
-\r
- def test_info(self):\r
- # Test 'info'.\r
- open_url = self.urlopen("http://www.python.org/")\r
- try:\r
- info_obj = open_url.info()\r
- finally:\r
- open_url.close()\r
- self.assertIsInstance(info_obj, mimetools.Message,\r
- "object returned by 'info' is not an "\r
- "instance of mimetools.Message")\r
- self.assertEqual(info_obj.getsubtype(), "html")\r
-\r
- def test_geturl(self):\r
- # Make sure same URL as opened is returned by geturl.\r
- URL = "http://www.python.org/"\r
- open_url = self.urlopen(URL)\r
- try:\r
- gotten_url = open_url.geturl()\r
- finally:\r
- open_url.close()\r
- self.assertEqual(gotten_url, URL)\r
-\r
- def test_getcode(self):\r
- # test getcode() with the fancy opener to get 404 error codes\r
- URL = "http://www.python.org/XXXinvalidXXX"\r
- open_url = urllib.FancyURLopener().open(URL)\r
- try:\r
- code = open_url.getcode()\r
- finally:\r
- open_url.close()\r
- self.assertEqual(code, 404)\r
-\r
- def test_fileno(self):\r
- if (sys.platform in ('win32',) or\r
- not hasattr(os, 'fdopen')):\r
- # On Windows, socket handles are not file descriptors; this\r
- # test can't pass on Windows.\r
- return\r
- # Make sure fd returned by fileno is valid.\r
- open_url = self.urlopen("http://www.python.org/")\r
- fd = open_url.fileno()\r
- FILE = os.fdopen(fd)\r
- try:\r
- self.assertTrue(FILE.read(), "reading from file created using fd "\r
- "returned by fileno failed")\r
- finally:\r
- FILE.close()\r
-\r
- def test_bad_address(self):\r
- # Make sure proper exception is raised when connecting to a bogus\r
- # address.\r
- self.assertRaises(IOError,\r
- # SF patch 809915: In Sep 2003, VeriSign started\r
- # highjacking invalid .com and .net addresses to\r
- # boost traffic to their own site. This test\r
- # started failing then. One hopes the .invalid\r
- # domain will be spared to serve its defined\r
- # purpose.\r
- # urllib.urlopen, "http://www.sadflkjsasadf.com/")\r
- urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")\r
-\r
-class urlretrieveNetworkTests(unittest.TestCase):\r
- """Tests urllib.urlretrieve using the network."""\r
-\r
- def urlretrieve(self, *args):\r
- return _open_with_retry(urllib.urlretrieve, *args)\r
-\r
- def test_basic(self):\r
- # Test basic functionality.\r
- file_location,info = self.urlretrieve("http://www.python.org/")\r
- self.assertTrue(os.path.exists(file_location), "file location returned by"\r
- " urlretrieve is not a valid path")\r
- FILE = file(file_location)\r
- try:\r
- self.assertTrue(FILE.read(), "reading from the file location returned"\r
- " by urlretrieve failed")\r
- finally:\r
- FILE.close()\r
- os.unlink(file_location)\r
-\r
- def test_specified_path(self):\r
- # Make sure that specifying the location of the file to write to works.\r
- file_location,info = self.urlretrieve("http://www.python.org/",\r
- test_support.TESTFN)\r
- self.assertEqual(file_location, test_support.TESTFN)\r
- self.assertTrue(os.path.exists(file_location))\r
- FILE = file(file_location)\r
- try:\r
- self.assertTrue(FILE.read(), "reading from temporary file failed")\r
- finally:\r
- FILE.close()\r
- os.unlink(file_location)\r
-\r
- def test_header(self):\r
- # Make sure header returned as 2nd value from urlretrieve is good.\r
- file_location, header = self.urlretrieve("http://www.python.org/")\r
- os.unlink(file_location)\r
- self.assertIsInstance(header, mimetools.Message,\r
- "header is not an instance of mimetools.Message")\r
-\r
- def test_data_header(self):\r
- logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"\r
- file_location, fileheaders = self.urlretrieve(logo)\r
- os.unlink(file_location)\r
- datevalue = fileheaders.getheader('Date')\r
- dateformat = '%a, %d %b %Y %H:%M:%S GMT'\r
- try:\r
- time.strptime(datevalue, dateformat)\r
- except ValueError:\r
- self.fail('Date value not in %r format', dateformat)\r
-\r
-\r
-\r
-def test_main():\r
- test_support.requires('network')\r
- with test_support.check_py3k_warnings(\r
- ("urllib.urlopen.. has been removed", DeprecationWarning)):\r
- test_support.run_unittest(URLTimeoutTest,\r
- urlopenNetworkTests,\r
- urlretrieveNetworkTests)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r