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