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