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