Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +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 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5 | from test.test_urllib2 import sanepathname2url |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 6 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 7 | import os |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 8 | import socket |
| 9 | import sys |
| 10 | import urllib.error |
| 11 | import urllib.request |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 12 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 13 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 14 | def _retry_thrice(func, exc, *args, **kwargs): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 15 | for i in range(3): |
| 16 | try: |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 17 | return func(*args, **kwargs) |
| 18 | except exc as e: |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 19 | last_exc = e |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 20 | continue |
| 21 | except: |
| 22 | raise |
| 23 | raise last_exc |
| 24 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 25 | def _wrap_with_retry_thrice(func, exc): |
| 26 | def wrapped(*args, **kwargs): |
| 27 | return _retry_thrice(func, exc, *args, **kwargs) |
| 28 | return wrapped |
| 29 | |
| 30 | # Connecting to remote hosts is flaky. Make it more robust by retrying |
| 31 | # the connection several times. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 32 | _urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen, |
| 33 | urllib.error.URLError) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 35 | |
| 36 | class AuthTests(unittest.TestCase): |
| 37 | """Tests urllib2 authentication features.""" |
| 38 | |
| 39 | ## Disabled at the moment since there is no page under python.org which |
| 40 | ## could be used to HTTP authentication. |
| 41 | # |
| 42 | # def test_basic_auth(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 43 | # import http.client |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 44 | # |
| 45 | # test_url = "http://www.python.org/test/test_urllib2/basic_auth" |
| 46 | # test_hostport = "www.python.org" |
| 47 | # test_realm = 'Test Realm' |
| 48 | # test_user = 'test.test_urllib2net' |
| 49 | # test_password = 'blah' |
| 50 | # |
| 51 | # # failure |
| 52 | # try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 53 | # _urlopen_with_retry(test_url) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 54 | # except urllib2.HTTPError, exc: |
| 55 | # self.assertEqual(exc.code, 401) |
| 56 | # else: |
| 57 | # self.fail("urlopen() should have failed with 401") |
| 58 | # |
| 59 | # # success |
| 60 | # auth_handler = urllib2.HTTPBasicAuthHandler() |
| 61 | # auth_handler.add_password(test_realm, test_hostport, |
| 62 | # test_user, test_password) |
| 63 | # opener = urllib2.build_opener(auth_handler) |
| 64 | # f = opener.open('http://localhost/') |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 65 | # response = _urlopen_with_retry("http://www.python.org/") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 66 | # |
| 67 | # # The 'userinfo' URL component is deprecated by RFC 3986 for security |
| 68 | # # reasons, let's not implement it! (it's already implemented for proxy |
| 69 | # # specification strings (that is, URLs or authorities specifying a |
| 70 | # # proxy), so we must keep that) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 71 | # self.assertRaises(http.client.InvalidURL, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | # urllib2.urlopen, "http://evil:thing@example.com") |
| 73 | |
| 74 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 75 | class CloseSocketTest(unittest.TestCase): |
| 76 | |
| 77 | def test_close(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 78 | import socket, http.client, gc |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 79 | |
| 80 | # calling .close() on urllib2's response objects should close the |
| 81 | # underlying socket |
| 82 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 83 | response = _urlopen_with_retry("http://www.python.org/") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 84 | sock = response.fp |
| 85 | self.assert_(not sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 86 | response.close() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 87 | self.assert_(sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 89 | class OtherNetworkTests(unittest.TestCase): |
| 90 | def setUp(self): |
| 91 | if 0: # for debugging |
| 92 | import logging |
| 93 | logger = logging.getLogger("test_urllib2net") |
| 94 | logger.addHandler(logging.StreamHandler()) |
| 95 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 96 | # XXX The rest of these tests aren't very good -- they don't check much. |
| 97 | # They do sometimes catch some major disasters, though. |
| 98 | |
| 99 | def test_ftp(self): |
| 100 | urls = [ |
Gregory P. Smith | c111d9f | 2007-09-09 23:55:55 +0000 | [diff] [blame] | 101 | 'ftp://ftp.kernel.org/pub/linux/kernel/README', |
| 102 | 'ftp://ftp.kernel.org/pub/linux/kernel/non-existant-file', |
| 103 | #'ftp://ftp.kernel.org/pub/leenox/kernel/test', |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 104 | 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC' |
| 105 | '/research-reports/00README-Legal-Rules-Regs', |
| 106 | ] |
| 107 | self._test_urls(urls, self._extra_handlers()) |
| 108 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 109 | def test_file(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 110 | TESTFN = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 111 | f = open(TESTFN, 'w') |
| 112 | try: |
| 113 | f.write('hi there\n') |
| 114 | f.close() |
| 115 | urls = [ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 116 | 'file:' + sanepathname2url(os.path.abspath(TESTFN)), |
| 117 | ('file:///nonsensename/etc/passwd', None, |
| 118 | urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 119 | ] |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 120 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 121 | finally: |
| 122 | os.remove(TESTFN) |
| 123 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 124 | # XXX Following test depends on machine configurations that are internal |
| 125 | # to CNRI. Need to set up a public server with the right authentication |
| 126 | # configuration for test purposes. |
| 127 | |
| 128 | ## def test_cnri(self): |
| 129 | ## if socket.gethostname() == 'bitdiddle': |
| 130 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 131 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 132 | ## localhost = 'localhost' |
| 133 | ## else: |
| 134 | ## localhost = None |
| 135 | ## if localhost is not None: |
| 136 | ## urls = [ |
| 137 | ## 'file://%s/etc/passwd' % localhost, |
| 138 | ## 'http://%s/simple/' % localhost, |
| 139 | ## 'http://%s/digest/' % localhost, |
| 140 | ## 'http://%s/not/found.h' % localhost, |
| 141 | ## ] |
| 142 | |
| 143 | ## bauth = HTTPBasicAuthHandler() |
| 144 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 145 | ## 'password') |
| 146 | ## dauth = HTTPDigestAuthHandler() |
| 147 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 148 | ## 'password') |
| 149 | |
| 150 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 151 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 152 | def _test_urls(self, urls, handlers, retry=True): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 153 | import socket |
| 154 | import time |
| 155 | import logging |
| 156 | debug = logging.getLogger("test_urllib2").debug |
| 157 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 158 | urlopen = urllib.request.build_opener(*handlers).open |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 159 | if retry: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 160 | urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 161 | |
| 162 | for url in urls: |
| 163 | if isinstance(url, tuple): |
| 164 | url, req, expected_err = url |
| 165 | else: |
| 166 | req = expected_err = None |
| 167 | debug(url) |
| 168 | try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 169 | f = urlopen(url, req) |
Gregory P. Smith | c111d9f | 2007-09-09 23:55:55 +0000 | [diff] [blame] | 170 | except EnvironmentError as err: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 171 | debug(err) |
| 172 | if expected_err: |
Gregory P. Smith | c111d9f | 2007-09-09 23:55:55 +0000 | [diff] [blame] | 173 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 174 | (expected_err, url, req, type(err), err)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 175 | self.assert_(isinstance(err, expected_err), msg) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 176 | else: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 177 | with support.transient_internet(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 178 | buf = f.read() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 179 | f.close() |
| 180 | debug("read %d bytes" % len(buf)) |
| 181 | debug("******** next url coming up...") |
| 182 | time.sleep(0.1) |
| 183 | |
| 184 | def _extra_handlers(self): |
| 185 | handlers = [] |
| 186 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 187 | cfh = urllib.request.CacheFTPHandler() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 188 | cfh.setTimeout(1) |
| 189 | handlers.append(cfh) |
| 190 | |
| 191 | return handlers |
| 192 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 194 | class TimeoutTest(unittest.TestCase): |
| 195 | def test_http_basic(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 196 | self.assertTrue(socket.getdefaulttimeout() is None) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 197 | u = _urlopen_with_retry("http://www.python.org") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 198 | self.assertTrue(u.fp._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 199 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 200 | def test_http_default_timeout(self): |
| 201 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 202 | socket.setdefaulttimeout(60) |
| 203 | try: |
| 204 | u = _urlopen_with_retry("http://www.python.org") |
| 205 | finally: |
| 206 | socket.setdefaulttimeout(None) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 207 | self.assertEqual(u.fp._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 208 | |
| 209 | def test_http_no_timeout(self): |
| 210 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 211 | socket.setdefaulttimeout(60) |
| 212 | try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 213 | u = _urlopen_with_retry("http://www.python.org", timeout=None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 214 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 215 | socket.setdefaulttimeout(None) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 216 | self.assertTrue(u.fp._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 217 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 218 | def test_http_timeout(self): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 219 | u = _urlopen_with_retry("http://www.python.org", timeout=120) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 220 | self.assertEqual(u.fp._sock.gettimeout(), 120) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 221 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 222 | FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/" |
| 223 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 224 | def test_ftp_basic(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 225 | self.assertTrue(socket.getdefaulttimeout() is None) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 226 | u = _urlopen_with_retry(self.FTP_HOST) |
Jeremy Hylton | cf2f419 | 2007-08-03 20:31:38 +0000 | [diff] [blame] | 227 | self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 228 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 229 | def test_ftp_default_timeout(self): |
| 230 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 231 | socket.setdefaulttimeout(60) |
| 232 | try: |
| 233 | u = _urlopen_with_retry(self.FTP_HOST) |
| 234 | finally: |
| 235 | socket.setdefaulttimeout(None) |
| 236 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
| 237 | |
| 238 | def test_ftp_no_timeout(self): |
| 239 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 240 | socket.setdefaulttimeout(60) |
| 241 | try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 242 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 243 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 244 | socket.setdefaulttimeout(None) |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 245 | self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 246 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 247 | def test_ftp_timeout(self): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 248 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
Jeremy Hylton | cf2f419 | 2007-08-03 20:31:38 +0000 | [diff] [blame] | 249 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 250 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 251 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 252 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 253 | support.requires("network") |
| 254 | support.run_unittest(AuthTests, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 255 | OtherNetworkTests, |
| 256 | CloseSocketTest, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 257 | TimeoutTest, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 258 | ) |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 259 | |
| 260 | if __name__ == "__main__": |
| 261 | test_main() |