Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 5 | |
| 6 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 7 | import urllib.request |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 8 | import sys |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 9 | import os |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 10 | import email.message |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 11 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +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. |
Christian Heimes | 061ce7f | 2008-01-27 15:45:24 +0000 | [diff] [blame] | 16 | last_exc = None |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 17 | for i in range(3): |
| 18 | try: |
| 19 | return func(host, *args, **kwargs) |
Christian Heimes | 061ce7f | 2008-01-27 15:45:24 +0000 | [diff] [blame] | 20 | except IOError as err: |
| 21 | last_exc = err |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 22 | continue |
| 23 | except: |
| 24 | raise |
| 25 | raise last_exc |
| 26 | |
| 27 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 28 | class URLTimeoutTest(unittest.TestCase): |
| 29 | |
| 30 | TIMEOUT = 10.0 |
| 31 | |
| 32 | def setUp(self): |
| 33 | socket.setdefaulttimeout(self.TIMEOUT) |
| 34 | |
| 35 | def tearDown(self): |
| 36 | socket.setdefaulttimeout(None) |
| 37 | |
| 38 | def testURLread(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 39 | f = _open_with_retry(urllib.request.urlopen, "http://www.python.org/") |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 40 | x = f.read() |
| 41 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 42 | class urlopenNetworkTests(unittest.TestCase): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 43 | """Tests urllib.reqest.urlopen using the network. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 44 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 45 | These tests are not exhaustive. Assuming that testing using files does a |
| 46 | good job overall of some of the basic interface features. There are no |
| 47 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 48 | for transparent redirection have been written. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 49 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 50 | setUp is not used for always constructing a connection to |
| 51 | http://www.python.org/ since there a few tests that don't use that address |
| 52 | and making a connection is expensive enough to warrant minimizing unneeded |
| 53 | connections. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 54 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 55 | """ |
| 56 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 57 | def urlopen(self, *args): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 58 | return _open_with_retry(urllib.request.urlopen, *args) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 59 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 60 | def test_basic(self): |
| 61 | # Simple test expected to pass. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 62 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 63 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 64 | "info", "geturl"): |
| 65 | self.assert_(hasattr(open_url, attr), "object returned from " |
| 66 | "urlopen lacks the %s attribute" % attr) |
| 67 | try: |
| 68 | self.assert_(open_url.read(), "calling 'read' failed") |
| 69 | finally: |
| 70 | open_url.close() |
| 71 | |
| 72 | def test_readlines(self): |
| 73 | # Test both readline and readlines. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 74 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 75 | try: |
Jeremy Hylton | 3e18615 | 2007-08-04 03:46:11 +0000 | [diff] [blame] | 76 | self.assert_(isinstance(open_url.readline(), bytes), |
| 77 | "readline did not return bytes") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 78 | self.assert_(isinstance(open_url.readlines(), list), |
| 79 | "readlines did not return a list") |
| 80 | finally: |
| 81 | open_url.close() |
| 82 | |
| 83 | def test_info(self): |
| 84 | # Test 'info'. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 85 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 86 | try: |
| 87 | info_obj = open_url.info() |
| 88 | finally: |
| 89 | open_url.close() |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 90 | self.assert_(isinstance(info_obj, email.message.Message), |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 91 | "object returned by 'info' is not an instance of " |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 92 | "email.message.Message") |
| 93 | self.assertEqual(info_obj.get_content_subtype(), "html") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 94 | |
| 95 | def test_geturl(self): |
| 96 | # Make sure same URL as opened is returned by geturl. |
| 97 | URL = "http://www.python.org/" |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 98 | open_url = self.urlopen(URL) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 99 | try: |
| 100 | gotten_url = open_url.geturl() |
| 101 | finally: |
| 102 | open_url.close() |
| 103 | self.assertEqual(gotten_url, URL) |
| 104 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 105 | def test_getcode(self): |
| 106 | # test getcode() with the fancy opener to get 404 error codes |
| 107 | URL = "http://www.python.org/XXXinvalidXXX" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 108 | open_url = urllib.request.FancyURLopener().open(URL) |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 109 | try: |
| 110 | code = open_url.getcode() |
| 111 | finally: |
| 112 | open_url.close() |
| 113 | self.assertEqual(code, 404) |
| 114 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 115 | def test_fileno(self): |
Amaury Forgeot d'Arc | bdbddf8 | 2008-08-01 00:06:49 +0000 | [diff] [blame] | 116 | if sys.platform in ('win32',): |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 117 | # On Windows, socket handles are not file descriptors; this |
| 118 | # test can't pass on Windows. |
| 119 | return |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 120 | # Make sure fd returned by fileno is valid. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 121 | open_url = self.urlopen("http://www.python.org/") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 122 | fd = open_url.fileno() |
Amaury Forgeot d'Arc | bdbddf8 | 2008-08-01 00:06:49 +0000 | [diff] [blame] | 123 | FILE = os.fdopen(fd, encoding='utf-8') |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 124 | try: |
| 125 | self.assert_(FILE.read(), "reading from file created using fd " |
| 126 | "returned by fileno failed") |
| 127 | finally: |
| 128 | FILE.close() |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 129 | |
| 130 | def test_bad_address(self): |
| 131 | # Make sure proper exception is raised when connecting to a bogus |
| 132 | # address. |
| 133 | self.assertRaises(IOError, |
Tim Peters | 0aab002 | 2003-09-20 22:16:26 +0000 | [diff] [blame] | 134 | # SF patch 809915: In Sep 2003, VeriSign started |
| 135 | # highjacking invalid .com and .net addresses to |
| 136 | # boost traffic to their own site. This test |
| 137 | # started failing then. One hopes the .invalid |
| 138 | # domain will be spared to serve its defined |
| 139 | # purpose. |
| 140 | # urllib.urlopen, "http://www.sadflkjsasadf.com/") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 141 | urllib.request.urlopen, |
| 142 | "http://www.python.invalid./") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 143 | |
| 144 | class urlretrieveNetworkTests(unittest.TestCase): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 145 | """Tests urllib.request.urlretrieve using the network.""" |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 146 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 147 | def urlretrieve(self, *args): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 148 | return _open_with_retry(urllib.request.urlretrieve, *args) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 149 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 150 | def test_basic(self): |
| 151 | # Test basic functionality. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 152 | file_location,info = self.urlretrieve("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 153 | self.assert_(os.path.exists(file_location), "file location returned by" |
| 154 | " urlretrieve is not a valid path") |
Amaury Forgeot d'Arc | bdbddf8 | 2008-08-01 00:06:49 +0000 | [diff] [blame] | 155 | FILE = open(file_location, encoding='utf-8') |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 156 | try: |
Jeremy Hylton | bd9f520 | 2003-07-17 16:31:00 +0000 | [diff] [blame] | 157 | self.assert_(FILE.read(), "reading from the file location returned" |
| 158 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 159 | finally: |
| 160 | FILE.close() |
| 161 | os.unlink(file_location) |
| 162 | |
| 163 | def test_specified_path(self): |
| 164 | # Make sure that specifying the location of the file to write to works. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 165 | file_location,info = self.urlretrieve("http://www.python.org/", |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 166 | support.TESTFN) |
| 167 | self.assertEqual(file_location, support.TESTFN) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 168 | self.assert_(os.path.exists(file_location)) |
Amaury Forgeot d'Arc | bdbddf8 | 2008-08-01 00:06:49 +0000 | [diff] [blame] | 169 | FILE = open(file_location, encoding='utf-8') |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 170 | try: |
| 171 | self.assert_(FILE.read(), "reading from temporary file failed") |
| 172 | finally: |
| 173 | FILE.close() |
| 174 | os.unlink(file_location) |
| 175 | |
| 176 | def test_header(self): |
| 177 | # Make sure header returned as 2nd value from urlretrieve is good. |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 178 | file_location, header = self.urlretrieve("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 179 | os.unlink(file_location) |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 180 | self.assert_(isinstance(header, email.message.Message), |
| 181 | "header is not an instance of email.message.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 182 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 183 | |
| 184 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 185 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 186 | support.requires('network') |
| 187 | support.run_unittest(URLTimeoutTest, |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 188 | urlopenNetworkTests, |
| 189 | urlretrieveNetworkTests) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 190 | |
| 191 | if __name__ == "__main__": |
| 192 | test_main() |