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 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 6 | import contextlib |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 7 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 8 | import urllib.request |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 9 | import sys |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 10 | import os |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 11 | import email.message |
Senthil Kumaran | f6c456d | 2010-05-01 08:29:18 +0000 | [diff] [blame] | 12 | import time |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 13 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [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): |
Senthil Kumaran | ee2538b | 2010-10-17 10:52:12 +0000 | [diff] [blame] | 27 | with support.transient_internet("www.python.org"): |
| 28 | f = urllib.request.urlopen("http://www.python.org/") |
Antoine Pitrou | d9faa20 | 2011-03-26 18:38:06 +0100 | [diff] [blame] | 29 | x = 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): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 33 | """Tests urllib.reqest.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 |
| 41 | http://www.python.org/ since there a few tests that don't use that address |
| 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 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 47 | @contextlib.contextmanager |
Senthil Kumaran | ee2538b | 2010-10-17 10:52:12 +0000 | [diff] [blame] | 48 | def urlopen(self, *args, **kwargs): |
| 49 | resource = args[0] |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 50 | with support.transient_internet(resource): |
| 51 | r = urllib.request.urlopen(*args, **kwargs) |
| 52 | try: |
| 53 | yield r |
| 54 | finally: |
| 55 | r.close() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 56 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 57 | def test_basic(self): |
| 58 | # Simple test expected to pass. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 59 | with self.urlopen("http://www.python.org/") as open_url: |
| 60 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 61 | "info", "geturl"): |
| 62 | self.assertTrue(hasattr(open_url, attr), "object returned from " |
| 63 | "urlopen lacks the %s attribute" % attr) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 64 | self.assertTrue(open_url.read(), "calling 'read' failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 65 | |
| 66 | def test_readlines(self): |
| 67 | # Test both readline and readlines. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 68 | with self.urlopen("http://www.python.org/") as open_url: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 69 | self.assertIsInstance(open_url.readline(), bytes, |
| 70 | "readline did not return a string") |
| 71 | self.assertIsInstance(open_url.readlines(), list, |
| 72 | "readlines did not return a list") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 73 | |
| 74 | def test_info(self): |
| 75 | # Test 'info'. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 76 | with self.urlopen("http://www.python.org/") as open_url: |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 77 | info_obj = open_url.info() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 78 | self.assertIsInstance(info_obj, email.message.Message, |
| 79 | "object returned by 'info' is not an " |
| 80 | "instance of email.message.Message") |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 81 | self.assertEqual(info_obj.get_content_subtype(), "html") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 82 | |
| 83 | def test_geturl(self): |
| 84 | # Make sure same URL as opened is returned by geturl. |
| 85 | URL = "http://www.python.org/" |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 86 | with self.urlopen(URL) as open_url: |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 87 | gotten_url = open_url.geturl() |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 88 | self.assertEqual(gotten_url, URL) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 89 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 90 | def test_getcode(self): |
| 91 | # test getcode() with the fancy opener to get 404 error codes |
| 92 | URL = "http://www.python.org/XXXinvalidXXX" |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 93 | with support.transient_internet(URL): |
| 94 | open_url = urllib.request.FancyURLopener().open(URL) |
| 95 | try: |
| 96 | code = open_url.getcode() |
| 97 | finally: |
| 98 | open_url.close() |
| 99 | self.assertEqual(code, 404) |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 100 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 101 | def test_fileno(self): |
Amaury Forgeot d'Arc | bdbddf8 | 2008-08-01 00:06:49 +0000 | [diff] [blame] | 102 | if sys.platform in ('win32',): |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 103 | # On Windows, socket handles are not file descriptors; this |
| 104 | # test can't pass on Windows. |
| 105 | return |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 106 | # Make sure fd returned by fileno is valid. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 107 | with self.urlopen("http://www.python.org/", timeout=None) as open_url: |
| 108 | fd = open_url.fileno() |
| 109 | with os.fdopen(fd, encoding='utf-8') as f: |
| 110 | self.assertTrue(f.read(), "reading from file created using fd " |
| 111 | "returned by fileno failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 112 | |
| 113 | def test_bad_address(self): |
| 114 | # Make sure proper exception is raised when connecting to a bogus |
| 115 | # address. |
Antoine Pitrou | 72fff04 | 2011-07-08 19:19:57 +0200 | [diff] [blame] | 116 | bogus_domain = "sadflkjsasf.i.nvali.d" |
| 117 | try: |
| 118 | socket.gethostbyname(bogus_domain) |
| 119 | except socket.gaierror: |
| 120 | pass |
| 121 | else: |
| 122 | # This happens with some overzealous DNS providers such as OpenDNS |
| 123 | self.skipTest("%r should not resolve for test to work" % bogus_domain) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 124 | self.assertRaises(IOError, |
Tim Peters | 0aab002 | 2003-09-20 22:16:26 +0000 | [diff] [blame] | 125 | # SF patch 809915: In Sep 2003, VeriSign started |
| 126 | # highjacking invalid .com and .net addresses to |
| 127 | # boost traffic to their own site. This test |
| 128 | # started failing then. One hopes the .invalid |
| 129 | # domain will be spared to serve its defined |
| 130 | # purpose. |
| 131 | # urllib.urlopen, "http://www.sadflkjsasadf.com/") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 132 | urllib.request.urlopen, |
Antoine Pitrou | 8fd33d3 | 2008-12-15 13:08:55 +0000 | [diff] [blame] | 133 | "http://sadflkjsasf.i.nvali.d/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 135 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 136 | class urlretrieveNetworkTests(unittest.TestCase): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 137 | """Tests urllib.request.urlretrieve using the network.""" |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 139 | @contextlib.contextmanager |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 140 | def urlretrieve(self, *args): |
Senthil Kumaran | ee2538b | 2010-10-17 10:52:12 +0000 | [diff] [blame] | 141 | resource = args[0] |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 142 | with support.transient_internet(resource): |
| 143 | file_location, info = urllib.request.urlretrieve(*args) |
| 144 | try: |
| 145 | yield file_location, info |
| 146 | finally: |
| 147 | support.unlink(file_location) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 148 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 149 | def test_basic(self): |
| 150 | # Test basic functionality. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 151 | with self.urlretrieve("http://www.python.org/") as (file_location, info): |
| 152 | self.assertTrue(os.path.exists(file_location), "file location returned by" |
| 153 | " urlretrieve is not a valid path") |
| 154 | with open(file_location, encoding='utf-8') as f: |
| 155 | self.assertTrue(f.read(), "reading from the file location returned" |
| 156 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 157 | |
| 158 | def test_specified_path(self): |
| 159 | # Make sure that specifying the location of the file to write to works. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 160 | with self.urlretrieve("http://www.python.org/", |
| 161 | support.TESTFN) as (file_location, info): |
| 162 | self.assertEqual(file_location, support.TESTFN) |
| 163 | self.assertTrue(os.path.exists(file_location)) |
| 164 | with open(file_location, encoding='utf-8') as f: |
| 165 | self.assertTrue(f.read(), "reading from temporary file failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 166 | |
| 167 | def test_header(self): |
| 168 | # Make sure header returned as 2nd value from urlretrieve is good. |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 169 | with self.urlretrieve("http://www.python.org/") as (file_location, info): |
| 170 | self.assertIsInstance(info, email.message.Message, |
| 171 | "info is not an instance of email.message.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 172 | |
Senthil Kumaran | f6c456d | 2010-05-01 08:29:18 +0000 | [diff] [blame] | 173 | def test_data_header(self): |
| 174 | logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png" |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 175 | with self.urlretrieve(logo) as (file_location, fileheaders): |
| 176 | datevalue = fileheaders.get('Date') |
| 177 | dateformat = '%a, %d %b %Y %H:%M:%S GMT' |
| 178 | try: |
| 179 | time.strptime(datevalue, dateformat) |
| 180 | except ValueError: |
| 181 | self.fail('Date value not in %r format', dateformat) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 182 | |
| 183 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 184 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 185 | support.requires('network') |
| 186 | support.run_unittest(URLTimeoutTest, |
Antoine Pitrou | a98d26a | 2011-05-22 17:35:17 +0200 | [diff] [blame] | 187 | urlopenNetworkTests, |
| 188 | urlretrieveNetworkTests) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 189 | |
| 190 | if __name__ == "__main__": |
| 191 | test_main() |