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