Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | |
| 4 | import socket |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 5 | import urllib |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 6 | import sys |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 7 | import os |
Senthil Kumaran | 1b7f9e5 | 2010-05-01 08:01:56 +0000 | [diff] [blame] | 8 | import time |
| 9 | |
Ezio Melotti | a2d4653 | 2010-01-30 07:22:54 +0000 | [diff] [blame] | 10 | mimetools = test_support.import_module("mimetools", deprecated=True) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 11 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 12 | |
| 13 | def _open_with_retry(func, host, *args, **kwargs): |
| 14 | # Connecting to remote hosts is flaky. Make it more robust |
| 15 | # by retrying the connection several times. |
| 16 | for i in range(3): |
| 17 | try: |
| 18 | return func(host, *args, **kwargs) |
| 19 | except IOError, last_exc: |
| 20 | continue |
| 21 | except: |
| 22 | raise |
| 23 | raise last_exc |
| 24 | |
| 25 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 26 | class URLTimeoutTest(unittest.TestCase): |
| 27 | |
| 28 | TIMEOUT = 10.0 |
| 29 | |
| 30 | def setUp(self): |
| 31 | socket.setdefaulttimeout(self.TIMEOUT) |
| 32 | |
| 33 | def tearDown(self): |
| 34 | socket.setdefaulttimeout(None) |
| 35 | |
| 36 | def testURLread(self): |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 37 | f = _open_with_retry(urllib.urlopen, "http://www.python.org/") |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 38 | x = f.read() |
| 39 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 40 | class urlopenNetworkTests(unittest.TestCase): |
| 41 | """Tests urllib.urlopen using the network. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 42 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 43 | These tests are not exhaustive. Assuming that testing using files does a |
| 44 | good job overall of some of the basic interface features. There are no |
| 45 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 46 | for transparent redirection have been written. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 47 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 48 | setUp is not used for always constructing a connection to |
| 49 | http://www.python.org/ since there a few tests that don't use that address |
| 50 | and making a connection is expensive enough to warrant minimizing unneeded |
| 51 | connections. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 52 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 53 | """ |
| 54 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 55 | def urlopen(self, *args): |
| 56 | return _open_with_retry(urllib.urlopen, *args) |
| 57 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 58 | def test_basic(self): |
| 59 | # Simple test expected to pass. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 60 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 61 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 62 | "info", "geturl"): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 63 | self.assertTrue(hasattr(open_url, attr), "object returned from " |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 64 | "urlopen lacks the %s attribute" % attr) |
| 65 | try: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 66 | self.assertTrue(open_url.read(), "calling 'read' failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 67 | finally: |
| 68 | open_url.close() |
| 69 | |
| 70 | def test_readlines(self): |
| 71 | # Test both readline and readlines. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 72 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 73 | try: |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 74 | self.assertIsInstance(open_url.readline(), basestring, |
| 75 | "readline did not return a string") |
| 76 | self.assertIsInstance(open_url.readlines(), list, |
| 77 | "readlines did not return a list") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 78 | finally: |
| 79 | open_url.close() |
| 80 | |
| 81 | def test_info(self): |
| 82 | # Test 'info'. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 83 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 84 | try: |
| 85 | info_obj = open_url.info() |
| 86 | finally: |
| 87 | open_url.close() |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 88 | self.assertIsInstance(info_obj, mimetools.Message, |
| 89 | "object returned by 'info' is not an " |
| 90 | "instance of mimetools.Message") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 91 | self.assertEqual(info_obj.getsubtype(), "html") |
| 92 | |
| 93 | def test_geturl(self): |
| 94 | # Make sure same URL as opened is returned by geturl. |
| 95 | URL = "http://www.python.org/" |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 96 | open_url = self.urlopen(URL) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 97 | try: |
| 98 | gotten_url = open_url.geturl() |
| 99 | finally: |
| 100 | open_url.close() |
| 101 | self.assertEqual(gotten_url, URL) |
| 102 | |
Georg Brandl | 9b0d46d | 2008-01-20 11:43:03 +0000 | [diff] [blame] | 103 | def test_getcode(self): |
| 104 | # test getcode() with the fancy opener to get 404 error codes |
| 105 | URL = "http://www.python.org/XXXinvalidXXX" |
| 106 | open_url = urllib.FancyURLopener().open(URL) |
| 107 | try: |
| 108 | code = open_url.getcode() |
| 109 | finally: |
| 110 | open_url.close() |
| 111 | self.assertEqual(code, 404) |
| 112 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 113 | @unittest.skipIf(sys.platform in ('win32',), 'not appropriate for Windows') |
| 114 | @unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available') |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 115 | def test_fileno(self): |
| 116 | # Make sure fd returned by fileno is valid. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 117 | open_url = self.urlopen("http://www.python.org/") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 118 | fd = open_url.fileno() |
| 119 | FILE = os.fdopen(fd) |
| 120 | try: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 121 | self.assertTrue(FILE.read(), "reading from file created using fd " |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 122 | "returned by fileno failed") |
| 123 | finally: |
| 124 | FILE.close() |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 125 | |
| 126 | def test_bad_address(self): |
| 127 | # Make sure proper exception is raised when connecting to a bogus |
| 128 | # address. |
Antoine Pitrou | a4d58d2 | 2011-07-08 19:14:19 +0200 | [diff] [blame] | 129 | bogus_domain = "sadflkjsasf.i.nvali.d" |
| 130 | try: |
| 131 | socket.gethostbyname(bogus_domain) |
| 132 | except socket.gaierror: |
| 133 | pass |
| 134 | else: |
| 135 | # This happens with some overzealous DNS providers such as OpenDNS |
| 136 | self.skipTest("%r should not resolve for test to work" % bogus_domain) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 137 | self.assertRaises(IOError, |
Tim Peters | 0aab002 | 2003-09-20 22:16:26 +0000 | [diff] [blame] | 138 | # SF patch 809915: In Sep 2003, VeriSign started |
| 139 | # highjacking invalid .com and .net addresses to |
| 140 | # boost traffic to their own site. This test |
| 141 | # started failing then. One hopes the .invalid |
| 142 | # domain will be spared to serve its defined |
| 143 | # purpose. |
| 144 | # urllib.urlopen, "http://www.sadflkjsasadf.com/") |
Antoine Pitrou | cc5b64a | 2008-12-15 00:39:51 +0000 | [diff] [blame] | 145 | urllib.urlopen, "http://sadflkjsasf.i.nvali.d/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 146 | |
| 147 | class urlretrieveNetworkTests(unittest.TestCase): |
| 148 | """Tests urllib.urlretrieve using the network.""" |
| 149 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 150 | def urlretrieve(self, *args): |
| 151 | return _open_with_retry(urllib.urlretrieve, *args) |
| 152 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 153 | def test_basic(self): |
| 154 | # Test basic functionality. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 155 | file_location,info = self.urlretrieve("http://www.python.org/") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 156 | self.assertTrue(os.path.exists(file_location), "file location returned by" |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 157 | " urlretrieve is not a valid path") |
| 158 | FILE = file(file_location) |
| 159 | try: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 160 | self.assertTrue(FILE.read(), "reading from the file location returned" |
Jeremy Hylton | bd9f520 | 2003-07-17 16:31:00 +0000 | [diff] [blame] | 161 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 162 | finally: |
| 163 | FILE.close() |
| 164 | os.unlink(file_location) |
| 165 | |
| 166 | def test_specified_path(self): |
| 167 | # Make sure that specifying the location of the file to write to works. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 168 | file_location,info = self.urlretrieve("http://www.python.org/", |
| 169 | test_support.TESTFN) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 170 | self.assertEqual(file_location, test_support.TESTFN) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 171 | self.assertTrue(os.path.exists(file_location)) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 172 | FILE = file(file_location) |
| 173 | try: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 174 | self.assertTrue(FILE.read(), "reading from temporary file failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 175 | finally: |
| 176 | FILE.close() |
| 177 | os.unlink(file_location) |
| 178 | |
| 179 | def test_header(self): |
| 180 | # Make sure header returned as 2nd value from urlretrieve is good. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 181 | file_location, header = self.urlretrieve("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 182 | os.unlink(file_location) |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 183 | self.assertIsInstance(header, mimetools.Message, |
| 184 | "header is not an instance of mimetools.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 185 | |
Senthil Kumaran | 1b7f9e5 | 2010-05-01 08:01:56 +0000 | [diff] [blame] | 186 | def test_data_header(self): |
| 187 | logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png" |
| 188 | file_location, fileheaders = self.urlretrieve(logo) |
| 189 | os.unlink(file_location) |
| 190 | datevalue = fileheaders.getheader('Date') |
| 191 | dateformat = '%a, %d %b %Y %H:%M:%S GMT' |
| 192 | try: |
| 193 | time.strptime(datevalue, dateformat) |
| 194 | except ValueError: |
| 195 | self.fail('Date value not in %r format', dateformat) |
| 196 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 197 | |
| 198 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 199 | def test_main(): |
| 200 | test_support.requires('network') |
Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 201 | with test_support.check_py3k_warnings( |
| 202 | ("urllib.urlopen.. has been removed", DeprecationWarning)): |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 203 | test_support.run_unittest(URLTimeoutTest, |
| 204 | urlopenNetworkTests, |
| 205 | urlretrieveNetworkTests) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 206 | |
| 207 | if __name__ == "__main__": |
| 208 | test_main() |