Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | from test import test_support |
| 5 | |
| 6 | import socket |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 7 | import urllib |
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 |
Ezio Melotti | 3897a44 | 2010-08-02 01:33:30 +0000 | [diff] [blame^] | 10 | mimetools = test_support.import_module("mimetools", deprecated=True) |
Senthil Kumaran | fcfd25b | 2010-05-01 08:06:38 +0000 | [diff] [blame] | 11 | import time |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 12 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +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. |
| 17 | for i in range(3): |
| 18 | try: |
| 19 | return func(host, *args, **kwargs) |
| 20 | except IOError, last_exc: |
| 21 | continue |
| 22 | except: |
| 23 | raise |
| 24 | raise last_exc |
| 25 | |
| 26 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 27 | class URLTimeoutTest(unittest.TestCase): |
| 28 | |
| 29 | TIMEOUT = 10.0 |
| 30 | |
| 31 | def setUp(self): |
| 32 | socket.setdefaulttimeout(self.TIMEOUT) |
| 33 | |
| 34 | def tearDown(self): |
| 35 | socket.setdefaulttimeout(None) |
| 36 | |
| 37 | def testURLread(self): |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 38 | f = _open_with_retry(urllib.urlopen, "http://www.python.org/") |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 39 | x = f.read() |
| 40 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 41 | class urlopenNetworkTests(unittest.TestCase): |
| 42 | """Tests urllib.urlopen using the network. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 43 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 44 | These tests are not exhaustive. Assuming that testing using files does a |
| 45 | good job overall of some of the basic interface features. There are no |
| 46 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 47 | for transparent redirection have been written. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 48 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 49 | setUp is not used for always constructing a connection to |
| 50 | http://www.python.org/ since there a few tests that don't use that address |
| 51 | and making a connection is expensive enough to warrant minimizing unneeded |
| 52 | connections. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 53 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 54 | """ |
| 55 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 56 | def urlopen(self, *args): |
| 57 | return _open_with_retry(urllib.urlopen, *args) |
| 58 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 59 | def test_basic(self): |
| 60 | # Simple test expected to pass. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 61 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 62 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 63 | "info", "geturl"): |
| 64 | self.assert_(hasattr(open_url, attr), "object returned from " |
| 65 | "urlopen lacks the %s attribute" % attr) |
| 66 | try: |
| 67 | self.assert_(open_url.read(), "calling 'read' failed") |
| 68 | finally: |
| 69 | open_url.close() |
| 70 | |
| 71 | def test_readlines(self): |
| 72 | # Test both readline and readlines. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 73 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 74 | try: |
| 75 | self.assert_(isinstance(open_url.readline(), basestring), |
| 76 | "readline did not return a string") |
| 77 | self.assert_(isinstance(open_url.readlines(), list), |
| 78 | "readlines did not return a list") |
| 79 | finally: |
| 80 | open_url.close() |
| 81 | |
| 82 | def test_info(self): |
| 83 | # Test 'info'. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 84 | open_url = self.urlopen("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 85 | try: |
| 86 | info_obj = open_url.info() |
| 87 | finally: |
| 88 | open_url.close() |
| 89 | self.assert_(isinstance(info_obj, mimetools.Message), |
| 90 | "object returned by 'info' is not an instance of " |
| 91 | "mimetools.Message") |
| 92 | self.assertEqual(info_obj.getsubtype(), "html") |
| 93 | |
| 94 | def test_geturl(self): |
| 95 | # Make sure same URL as opened is returned by geturl. |
| 96 | URL = "http://www.python.org/" |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 97 | open_url = self.urlopen(URL) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 98 | try: |
| 99 | gotten_url = open_url.geturl() |
| 100 | finally: |
| 101 | open_url.close() |
| 102 | self.assertEqual(gotten_url, URL) |
| 103 | |
Georg Brandl | 9b0d46d | 2008-01-20 11:43:03 +0000 | [diff] [blame] | 104 | def test_getcode(self): |
| 105 | # test getcode() with the fancy opener to get 404 error codes |
| 106 | URL = "http://www.python.org/XXXinvalidXXX" |
| 107 | open_url = urllib.FancyURLopener().open(URL) |
| 108 | try: |
| 109 | code = open_url.getcode() |
| 110 | finally: |
| 111 | open_url.close() |
| 112 | self.assertEqual(code, 404) |
| 113 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 114 | def test_fileno(self): |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 115 | if (sys.platform in ('win32',) or |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 116 | not hasattr(os, 'fdopen')): |
| 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. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +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() |
| 123 | FILE = os.fdopen(fd) |
| 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/") |
Antoine Pitrou | 3fced65 | 2008-12-15 17:48:36 +0000 | [diff] [blame] | 141 | urllib.urlopen, "http://sadflkjsasf.i.nvali.d/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 142 | |
| 143 | class urlretrieveNetworkTests(unittest.TestCase): |
| 144 | """Tests urllib.urlretrieve using the network.""" |
| 145 | |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 146 | def urlretrieve(self, *args): |
| 147 | return _open_with_retry(urllib.urlretrieve, *args) |
| 148 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 149 | def test_basic(self): |
| 150 | # Test basic functionality. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 151 | file_location,info = self.urlretrieve("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 152 | self.assert_(os.path.exists(file_location), "file location returned by" |
| 153 | " urlretrieve is not a valid path") |
| 154 | FILE = file(file_location) |
| 155 | try: |
Jeremy Hylton | bd9f520 | 2003-07-17 16:31:00 +0000 | [diff] [blame] | 156 | self.assert_(FILE.read(), "reading from the file location returned" |
| 157 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 158 | finally: |
| 159 | FILE.close() |
| 160 | os.unlink(file_location) |
| 161 | |
| 162 | def test_specified_path(self): |
| 163 | # 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] | 164 | file_location,info = self.urlretrieve("http://www.python.org/", |
| 165 | test_support.TESTFN) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 166 | self.assertEqual(file_location, test_support.TESTFN) |
| 167 | self.assert_(os.path.exists(file_location)) |
| 168 | FILE = file(file_location) |
| 169 | try: |
| 170 | self.assert_(FILE.read(), "reading from temporary file failed") |
| 171 | finally: |
| 172 | FILE.close() |
| 173 | os.unlink(file_location) |
| 174 | |
| 175 | def test_header(self): |
| 176 | # Make sure header returned as 2nd value from urlretrieve is good. |
Neal Norwitz | 5be3067 | 2008-01-26 05:54:48 +0000 | [diff] [blame] | 177 | file_location, header = self.urlretrieve("http://www.python.org/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 178 | os.unlink(file_location) |
| 179 | self.assert_(isinstance(header, mimetools.Message), |
| 180 | "header is not an instance of mimetools.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 181 | |
Senthil Kumaran | fcfd25b | 2010-05-01 08:06:38 +0000 | [diff] [blame] | 182 | def test_data_header(self): |
| 183 | logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png" |
| 184 | file_location, fileheaders = self.urlretrieve(logo) |
| 185 | os.unlink(file_location) |
| 186 | datevalue = fileheaders.getheader('Date') |
| 187 | dateformat = '%a, %d %b %Y %H:%M:%S GMT' |
| 188 | try: |
| 189 | time.strptime(datevalue, dateformat) |
| 190 | except ValueError: |
| 191 | self.fail('Date value not in %r format', dateformat) |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 192 | |
| 193 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 194 | def test_main(): |
| 195 | test_support.requires('network') |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 196 | from warnings import filterwarnings, catch_warnings |
| 197 | with catch_warnings(): |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 198 | filterwarnings('ignore', '.*urllib\.urlopen.*Python 3.0', |
| 199 | DeprecationWarning) |
| 200 | test_support.run_unittest(URLTimeoutTest, |
| 201 | urlopenNetworkTests, |
| 202 | urlretrieveNetworkTests) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 203 | |
| 204 | if __name__ == "__main__": |
| 205 | test_main() |