Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 3 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 4 | import contextlib |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 5 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 6 | import urllib.request |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 7 | import os |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 8 | import email.message |
Senthil Kumaran | f6c456d | 2010-05-01 08:29:18 +0000 | [diff] [blame] | 9 | import time |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 10 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 11 | |
Senthil Kumaran | cfdd016 | 2014-04-14 21:31:41 -0400 | [diff] [blame] | 12 | support.requires('network') |
| 13 | |
Senthil Kumaran | 1bd7d29 | 2017-05-15 23:08:07 -0700 | [diff] [blame] | 14 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 15 | class URLTimeoutTest(unittest.TestCase): |
Antoine Pitrou | d9faa20 | 2011-03-26 18:38:06 +0100 | [diff] [blame] | 16 | # XXX this test doesn't seem to test anything useful. |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 17 | |
Senthil Kumaran | bd8f145 | 2010-12-15 04:02:45 +0000 | [diff] [blame] | 18 | TIMEOUT = 30.0 |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 19 | |
| 20 | def setUp(self): |
| 21 | socket.setdefaulttimeout(self.TIMEOUT) |
| 22 | |
| 23 | def tearDown(self): |
| 24 | socket.setdefaulttimeout(None) |
| 25 | |
| 26 | def testURLread(self): |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 27 | with support.transient_internet("www.example.com"): |
| 28 | f = urllib.request.urlopen("http://www.example.com/") |
Senthil Kumaran | 1bd7d29 | 2017-05-15 23:08:07 -0700 | [diff] [blame] | 29 | f.read() |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 30 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 31 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 32 | class urlopenNetworkTests(unittest.TestCase): |
Senthil Kumaran | efbd4ea | 2017-04-01 23:47:35 -0700 | [diff] [blame] | 33 | """Tests urllib.request.urlopen using the network. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 34 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 35 | These tests are not exhaustive. Assuming that testing using files does a |
| 36 | good job overall of some of the basic interface features. There are no |
| 37 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 38 | for transparent redirection have been written. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 39 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 40 | setUp is not used for always constructing a connection to |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 41 | http://www.pythontest.net/ since there a few tests that don't use that address |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 42 | and making a connection is expensive enough to warrant minimizing unneeded |
| 43 | connections. |
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 | """ |
| 46 | |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 47 | url = 'http://www.pythontest.net/' |
| 48 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 49 | @contextlib.contextmanager |
Senthil Kumaran | ee2538b | 2010-10-17 10:52:12 +0000 | [diff] [blame] | 50 | def urlopen(self, *args, **kwargs): |
| 51 | resource = args[0] |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 52 | with support.transient_internet(resource): |
| 53 | r = urllib.request.urlopen(*args, **kwargs) |
| 54 | try: |
| 55 | yield r |
| 56 | finally: |
| 57 | r.close() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 58 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 59 | def test_basic(self): |
| 60 | # Simple test expected to pass. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 61 | with self.urlopen(self.url) as open_url: |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 62 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 63 | "info", "geturl"): |
| 64 | self.assertTrue(hasattr(open_url, attr), "object returned from " |
| 65 | "urlopen lacks the %s attribute" % attr) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +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 | |
| 68 | def test_readlines(self): |
| 69 | # Test both readline and readlines. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 70 | with self.urlopen(self.url) as open_url: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 71 | self.assertIsInstance(open_url.readline(), bytes, |
| 72 | "readline did not return a string") |
| 73 | self.assertIsInstance(open_url.readlines(), list, |
| 74 | "readlines did not return a list") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 75 | |
| 76 | def test_info(self): |
| 77 | # Test 'info'. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 78 | with self.urlopen(self.url) as open_url: |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 79 | info_obj = open_url.info() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 80 | self.assertIsInstance(info_obj, email.message.Message, |
| 81 | "object returned by 'info' is not an " |
| 82 | "instance of email.message.Message") |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 83 | self.assertEqual(info_obj.get_content_subtype(), "html") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 84 | |
| 85 | def test_geturl(self): |
| 86 | # Make sure same URL as opened is returned by geturl. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 87 | with self.urlopen(self.url) as open_url: |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 88 | gotten_url = open_url.geturl() |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 89 | self.assertEqual(gotten_url, self.url) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 90 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 91 | def test_getcode(self): |
| 92 | # test getcode() with the fancy opener to get 404 error codes |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 93 | URL = self.url + "XXXinvalidXXX" |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 94 | with support.transient_internet(URL): |
R David Murray | 130a566 | 2014-06-11 17:09:43 -0400 | [diff] [blame] | 95 | with self.assertWarns(DeprecationWarning): |
| 96 | open_url = urllib.request.FancyURLopener().open(URL) |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 97 | try: |
| 98 | code = open_url.getcode() |
| 99 | finally: |
| 100 | open_url.close() |
| 101 | self.assertEqual(code, 404) |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 102 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 103 | def test_bad_address(self): |
| 104 | # Make sure proper exception is raised when connecting to a bogus |
| 105 | # address. |
Martin Panter | a7f9933 | 2015-12-16 04:36:20 +0000 | [diff] [blame] | 106 | |
| 107 | # Given that both VeriSign and various ISPs have in |
| 108 | # the past or are presently hijacking various invalid |
| 109 | # domain name requests in an attempt to boost traffic |
| 110 | # to their own sites, finding a domain name to use |
| 111 | # for this test is difficult. RFC2606 leads one to |
| 112 | # believe that '.invalid' should work, but experience |
| 113 | # seemed to indicate otherwise. Single character |
| 114 | # TLDs are likely to remain invalid, so this seems to |
| 115 | # be the best choice. The trailing '.' prevents a |
| 116 | # related problem: The normal DNS resolver appends |
| 117 | # the domain names from the search path if there is |
| 118 | # no '.' the end and, and if one of those domains |
| 119 | # implements a '*' rule a result is returned. |
| 120 | # However, none of this will prevent the test from |
| 121 | # failing if the ISP hijacks all invalid domain |
| 122 | # requests. The real solution would be to be able to |
| 123 | # parameterize the framework with a mock resolver. |
| 124 | bogus_domain = "sadflkjsasf.i.nvali.d." |
Antoine Pitrou | 72fff04 | 2011-07-08 19:19:57 +0200 | [diff] [blame] | 125 | try: |
| 126 | socket.gethostbyname(bogus_domain) |
Antoine Pitrou | 6b5a38c | 2013-05-25 13:08:13 +0200 | [diff] [blame] | 127 | except OSError: |
| 128 | # socket.gaierror is too narrow, since getaddrinfo() may also |
| 129 | # fail with EAI_SYSTEM and ETIMEDOUT (seen on Ubuntu 13.04), |
| 130 | # i.e. Python's TimeoutError. |
Antoine Pitrou | 72fff04 | 2011-07-08 19:19:57 +0200 | [diff] [blame] | 131 | pass |
| 132 | else: |
| 133 | # This happens with some overzealous DNS providers such as OpenDNS |
| 134 | self.skipTest("%r should not resolve for test to work" % bogus_domain) |
Brett Cannon | b463c48 | 2013-01-11 11:17:53 -0500 | [diff] [blame] | 135 | failure_explanation = ('opening an invalid URL did not raise OSError; ' |
| 136 | 'can be caused by a broken DNS server ' |
| 137 | '(e.g. returns 404 or hijacks page)') |
| 138 | with self.assertRaises(OSError, msg=failure_explanation): |
Martin Panter | a7f9933 | 2015-12-16 04:36:20 +0000 | [diff] [blame] | 139 | urllib.request.urlopen("http://{}/".format(bogus_domain)) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 141 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 142 | class urlretrieveNetworkTests(unittest.TestCase): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 143 | """Tests urllib.request.urlretrieve using the network.""" |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 145 | @contextlib.contextmanager |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 146 | def urlretrieve(self, *args, **kwargs): |
Senthil Kumaran | ee2538b | 2010-10-17 10:52:12 +0000 | [diff] [blame] | 147 | resource = args[0] |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 148 | with support.transient_internet(resource): |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 149 | file_location, info = urllib.request.urlretrieve(*args, **kwargs) |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 150 | try: |
| 151 | yield file_location, info |
| 152 | finally: |
| 153 | support.unlink(file_location) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 154 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 155 | def test_basic(self): |
| 156 | # Test basic functionality. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 157 | with self.urlretrieve(self.logo) as (file_location, info): |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 158 | self.assertTrue(os.path.exists(file_location), "file location returned by" |
| 159 | " urlretrieve is not a valid path") |
Benjamin Peterson | a96ed63 | 2014-02-19 23:06:41 -0500 | [diff] [blame] | 160 | with open(file_location, 'rb') as f: |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 161 | self.assertTrue(f.read(), "reading from the file location returned" |
| 162 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 163 | |
| 164 | def test_specified_path(self): |
| 165 | # Make sure that specifying the location of the file to write to works. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 166 | with self.urlretrieve(self.logo, |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 167 | support.TESTFN) as (file_location, info): |
| 168 | self.assertEqual(file_location, support.TESTFN) |
| 169 | self.assertTrue(os.path.exists(file_location)) |
Benjamin Peterson | a96ed63 | 2014-02-19 23:06:41 -0500 | [diff] [blame] | 170 | with open(file_location, 'rb') as f: |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 171 | self.assertTrue(f.read(), "reading from temporary file failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 172 | |
| 173 | def test_header(self): |
| 174 | # Make sure header returned as 2nd value from urlretrieve is good. |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 175 | with self.urlretrieve(self.logo) as (file_location, info): |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 176 | self.assertIsInstance(info, email.message.Message, |
| 177 | "info is not an instance of email.message.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 178 | |
Berker Peksag | a40b0ef | 2016-05-07 16:37:09 +0300 | [diff] [blame] | 179 | logo = "http://www.pythontest.net/" |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 180 | |
Senthil Kumaran | f6c456d | 2010-05-01 08:29:18 +0000 | [diff] [blame] | 181 | def test_data_header(self): |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 182 | with self.urlretrieve(self.logo) as (file_location, fileheaders): |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 183 | datevalue = fileheaders.get('Date') |
| 184 | dateformat = '%a, %d %b %Y %H:%M:%S GMT' |
| 185 | try: |
| 186 | time.strptime(datevalue, dateformat) |
| 187 | except ValueError: |
Berker Peksag | ad1fd34 | 2016-05-07 16:58:41 +0300 | [diff] [blame] | 188 | self.fail('Date value not in %r format' % dateformat) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 189 | |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 190 | def test_reporthook(self): |
| 191 | records = [] |
Senthil Kumaran | 1bd7d29 | 2017-05-15 23:08:07 -0700 | [diff] [blame] | 192 | |
Gregory P. Smith | 6b0bdab | 2012-11-10 13:43:44 -0800 | [diff] [blame] | 193 | def recording_reporthook(blocks, block_size, total_size): |
| 194 | records.append((blocks, block_size, total_size)) |
| 195 | |
| 196 | with self.urlretrieve(self.logo, reporthook=recording_reporthook) as ( |
| 197 | file_location, fileheaders): |
| 198 | expected_size = int(fileheaders['Content-Length']) |
| 199 | |
| 200 | records_repr = repr(records) # For use in error messages. |
| 201 | self.assertGreater(len(records), 1, msg="There should always be two " |
| 202 | "calls; the first one before the transfer starts.") |
| 203 | self.assertEqual(records[0][0], 0) |
| 204 | self.assertGreater(records[0][1], 0, |
| 205 | msg="block size can't be 0 in %s" % records_repr) |
| 206 | self.assertEqual(records[0][2], expected_size) |
| 207 | self.assertEqual(records[-1][2], expected_size) |
| 208 | |
| 209 | block_sizes = {block_size for _, block_size, _ in records} |
| 210 | self.assertEqual({records[0][1]}, block_sizes, |
| 211 | msg="block sizes in %s must be equal" % records_repr) |
| 212 | self.assertGreaterEqual(records[-1][0]*records[0][1], expected_size, |
| 213 | msg="number of blocks * block size must be" |
| 214 | " >= total size in %s" % records_repr) |
| 215 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 216 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 217 | if __name__ == "__main__": |
Senthil Kumaran | cfdd016 | 2014-04-14 21:31:41 -0400 | [diff] [blame] | 218 | unittest.main() |