Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 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 |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 9 | import urllib.error |
| 10 | import urllib.request |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 11 | import sys |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 12 | try: |
| 13 | import ssl |
| 14 | except ImportError: |
| 15 | ssl = None |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 16 | |
| 17 | TIMEOUT = 60 # seconds |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 19 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 20 | def _retry_thrice(func, exc, *args, **kwargs): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 21 | for i in range(3): |
| 22 | try: |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 23 | return func(*args, **kwargs) |
| 24 | except exc as e: |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 25 | last_exc = e |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 26 | continue |
| 27 | except: |
| 28 | raise |
| 29 | raise last_exc |
| 30 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 31 | def _wrap_with_retry_thrice(func, exc): |
| 32 | def wrapped(*args, **kwargs): |
| 33 | return _retry_thrice(func, exc, *args, **kwargs) |
| 34 | return wrapped |
| 35 | |
| 36 | # Connecting to remote hosts is flaky. Make it more robust by retrying |
| 37 | # the connection several times. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 38 | _urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen, |
| 39 | urllib.error.URLError) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 41 | |
| 42 | class AuthTests(unittest.TestCase): |
| 43 | """Tests urllib2 authentication features.""" |
| 44 | |
| 45 | ## Disabled at the moment since there is no page under python.org which |
| 46 | ## could be used to HTTP authentication. |
| 47 | # |
| 48 | # def test_basic_auth(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 49 | # import http.client |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 50 | # |
| 51 | # test_url = "http://www.python.org/test/test_urllib2/basic_auth" |
| 52 | # test_hostport = "www.python.org" |
| 53 | # test_realm = 'Test Realm' |
| 54 | # test_user = 'test.test_urllib2net' |
| 55 | # test_password = 'blah' |
| 56 | # |
| 57 | # # failure |
| 58 | # try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 59 | # _urlopen_with_retry(test_url) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 60 | # except urllib2.HTTPError, exc: |
| 61 | # self.assertEqual(exc.code, 401) |
| 62 | # else: |
| 63 | # self.fail("urlopen() should have failed with 401") |
| 64 | # |
| 65 | # # success |
| 66 | # auth_handler = urllib2.HTTPBasicAuthHandler() |
| 67 | # auth_handler.add_password(test_realm, test_hostport, |
| 68 | # test_user, test_password) |
| 69 | # opener = urllib2.build_opener(auth_handler) |
| 70 | # f = opener.open('http://localhost/') |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 71 | # response = _urlopen_with_retry("http://www.python.org/") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | # |
| 73 | # # The 'userinfo' URL component is deprecated by RFC 3986 for security |
| 74 | # # reasons, let's not implement it! (it's already implemented for proxy |
| 75 | # # specification strings (that is, URLs or authorities specifying a |
| 76 | # # proxy), so we must keep that) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 77 | # self.assertRaises(http.client.InvalidURL, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 78 | # urllib2.urlopen, "http://evil:thing@example.com") |
| 79 | |
| 80 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 81 | class CloseSocketTest(unittest.TestCase): |
| 82 | |
| 83 | def test_close(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 84 | # calling .close() on urllib2's response objects should close the |
| 85 | # underlying socket |
| 86 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 87 | response = _urlopen_with_retry("http://www.python.org/") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 88 | sock = response.fp |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 89 | self.assertTrue(not sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 90 | response.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 91 | self.assertTrue(sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 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', |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 106 | 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file', |
Gregory P. Smith | c111d9f | 2007-09-09 23:55:55 +0000 | [diff] [blame] | 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 = [ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 120 | 'file:' + sanepathname2url(os.path.abspath(TESTFN)), |
| 121 | ('file:///nonsensename/etc/passwd', None, |
| 122 | urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | ] |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 124 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 125 | finally: |
| 126 | os.remove(TESTFN) |
| 127 | |
Senthil Kumaran | 3800ea9 | 2012-01-21 11:52:48 +0800 | [diff] [blame] | 128 | self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file') |
| 129 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 130 | # XXX Following test depends on machine configurations that are internal |
| 131 | # to CNRI. Need to set up a public server with the right authentication |
| 132 | # configuration for test purposes. |
| 133 | |
| 134 | ## def test_cnri(self): |
| 135 | ## if socket.gethostname() == 'bitdiddle': |
| 136 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 137 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 138 | ## localhost = 'localhost' |
| 139 | ## else: |
| 140 | ## localhost = None |
| 141 | ## if localhost is not None: |
| 142 | ## urls = [ |
| 143 | ## 'file://%s/etc/passwd' % localhost, |
| 144 | ## 'http://%s/simple/' % localhost, |
| 145 | ## 'http://%s/digest/' % localhost, |
| 146 | ## 'http://%s/not/found.h' % localhost, |
| 147 | ## ] |
| 148 | |
| 149 | ## bauth = HTTPBasicAuthHandler() |
| 150 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 151 | ## 'password') |
| 152 | ## dauth = HTTPDigestAuthHandler() |
| 153 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 154 | ## 'password') |
| 155 | |
| 156 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 157 | |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 158 | def test_urlwithfrag(self): |
| 159 | urlwith_frag = "http://docs.python.org/glossary.html#glossary" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 160 | with support.transient_internet(urlwith_frag): |
| 161 | req = urllib.request.Request(urlwith_frag) |
| 162 | res = urllib.request.urlopen(req) |
| 163 | self.assertEqual(res.geturl(), |
Senthil Kumaran | 2643041 | 2011-04-13 07:01:19 +0800 | [diff] [blame] | 164 | "http://docs.python.org/glossary.html#glossary") |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 165 | |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 166 | def test_custom_headers(self): |
| 167 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 168 | with support.transient_internet(url): |
| 169 | opener = urllib.request.build_opener() |
| 170 | request = urllib.request.Request(url) |
| 171 | self.assertFalse(request.header_items()) |
| 172 | opener.open(request) |
| 173 | self.assertTrue(request.header_items()) |
| 174 | self.assertTrue(request.has_header('User-agent')) |
| 175 | request.add_header('User-Agent','Test-Agent') |
| 176 | opener.open(request) |
| 177 | self.assertEqual(request.get_header('User-agent'),'Test-Agent') |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 178 | |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 179 | def test_sites_no_connection_close(self): |
| 180 | # Some sites do not send Connection: close header. |
| 181 | # Verify that those work properly. (#issue12576) |
| 182 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 183 | URL = 'http://www.imdb.com' # mangles Connection:close |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 184 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 185 | with support.transient_internet(URL): |
| 186 | try: |
| 187 | with urllib.request.urlopen(URL) as res: |
| 188 | pass |
| 189 | except ValueError as e: |
| 190 | self.fail("urlopen failed for site not sending \ |
| 191 | Connection:close") |
| 192 | else: |
| 193 | self.assertTrue(res) |
| 194 | |
| 195 | req = urllib.request.urlopen(URL) |
| 196 | res = req.read() |
| 197 | self.assertTrue(res) |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 198 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 199 | def _test_urls(self, urls, handlers, retry=True): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | import time |
| 201 | import logging |
| 202 | debug = logging.getLogger("test_urllib2").debug |
| 203 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 204 | urlopen = urllib.request.build_opener(*handlers).open |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 205 | if retry: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 206 | urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 207 | |
| 208 | for url in urls: |
| 209 | if isinstance(url, tuple): |
| 210 | url, req, expected_err = url |
| 211 | else: |
| 212 | req = expected_err = None |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 213 | |
| 214 | with support.transient_internet(url): |
| 215 | debug(url) |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 216 | try: |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 217 | f = urlopen(url, req, TIMEOUT) |
| 218 | except EnvironmentError as err: |
| 219 | debug(err) |
| 220 | if expected_err: |
| 221 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 222 | (expected_err, url, req, type(err), err)) |
| 223 | self.assertIsInstance(err, expected_err, msg) |
| 224 | except urllib.error.URLError as err: |
| 225 | if isinstance(err[0], socket.timeout): |
| 226 | print("<timeout: %s>" % url, file=sys.stderr) |
| 227 | continue |
| 228 | else: |
| 229 | raise |
| 230 | else: |
| 231 | try: |
| 232 | with support.time_out, \ |
| 233 | support.socket_peer_reset, \ |
| 234 | support.ioerror_peer_reset: |
| 235 | buf = f.read() |
| 236 | debug("read %d bytes" % len(buf)) |
| 237 | except socket.timeout: |
| 238 | print("<timeout: %s>" % url, file=sys.stderr) |
| 239 | f.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | debug("******** next url coming up...") |
| 241 | time.sleep(0.1) |
| 242 | |
| 243 | def _extra_handlers(self): |
| 244 | handlers = [] |
| 245 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 246 | cfh = urllib.request.CacheFTPHandler() |
Nadeem Vawda | 08f5f7a | 2011-07-23 14:03:00 +0200 | [diff] [blame] | 247 | self.addCleanup(cfh.clear_cache) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 248 | cfh.setTimeout(1) |
| 249 | handlers.append(cfh) |
| 250 | |
| 251 | return handlers |
| 252 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 254 | class TimeoutTest(unittest.TestCase): |
| 255 | def test_http_basic(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 256 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 257 | url = "http://www.python.org" |
| 258 | with support.transient_internet(url, timeout=None): |
| 259 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 260 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 261 | self.assertTrue(u.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 262 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 263 | def test_http_default_timeout(self): |
| 264 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 265 | url = "http://www.python.org" |
| 266 | with support.transient_internet(url): |
| 267 | socket.setdefaulttimeout(60) |
| 268 | try: |
| 269 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 270 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 271 | finally: |
| 272 | socket.setdefaulttimeout(None) |
| 273 | self.assertEqual(u.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 274 | |
| 275 | def test_http_no_timeout(self): |
| 276 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 277 | url = "http://www.python.org" |
| 278 | with support.transient_internet(url): |
| 279 | socket.setdefaulttimeout(60) |
| 280 | try: |
| 281 | u = _urlopen_with_retry(url, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 282 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 283 | finally: |
| 284 | socket.setdefaulttimeout(None) |
| 285 | self.assertTrue(u.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 286 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 287 | def test_http_timeout(self): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 288 | url = "http://www.python.org" |
| 289 | with support.transient_internet(url): |
| 290 | u = _urlopen_with_retry(url, timeout=120) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 291 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 292 | self.assertEqual(u.fp.raw._sock.gettimeout(), 120) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 293 | |
Benjamin Peterson | 87cb787 | 2010-04-11 21:59:57 +0000 | [diff] [blame] | 294 | FTP_HOST = "ftp://ftp.mirror.nl/pub/gnu/" |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 296 | def test_ftp_basic(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 297 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 298 | with support.transient_internet(self.FTP_HOST, timeout=None): |
| 299 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 300 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 301 | self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 302 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 303 | def test_ftp_default_timeout(self): |
| 304 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 305 | with support.transient_internet(self.FTP_HOST): |
| 306 | socket.setdefaulttimeout(60) |
| 307 | try: |
| 308 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 309 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 310 | finally: |
| 311 | socket.setdefaulttimeout(None) |
| 312 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 313 | |
| 314 | def test_ftp_no_timeout(self): |
| 315 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 316 | with support.transient_internet(self.FTP_HOST): |
| 317 | socket.setdefaulttimeout(60) |
| 318 | try: |
| 319 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 320 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 321 | finally: |
| 322 | socket.setdefaulttimeout(None) |
| 323 | self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 324 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 325 | def test_ftp_timeout(self): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 326 | with support.transient_internet(self.FTP_HOST): |
| 327 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 328 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 329 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 330 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 332 | @unittest.skipUnless(ssl, "requires SSL support") |
| 333 | class HTTPSTests(unittest.TestCase): |
| 334 | |
| 335 | def test_sni(self): |
Antoine Pitrou | 0eee1f5 | 2010-11-03 08:53:25 +0000 | [diff] [blame] | 336 | self.skipTest("test disabled - test server needed") |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 337 | # Checks that Server Name Indication works, if supported by the |
| 338 | # OpenSSL linked to. |
| 339 | # The ssl module itself doesn't have server-side support for SNI, |
| 340 | # so we rely on a third-party test site. |
| 341 | expect_sni = ssl.HAS_SNI |
Antoine Pitrou | 0eee1f5 | 2010-11-03 08:53:25 +0000 | [diff] [blame] | 342 | with support.transient_internet("XXX"): |
| 343 | u = urllib.request.urlopen("XXX") |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 344 | contents = u.readall() |
| 345 | if expect_sni: |
| 346 | self.assertIn(b"Great", contents) |
| 347 | self.assertNotIn(b"Unfortunately", contents) |
| 348 | else: |
| 349 | self.assertNotIn(b"Great", contents) |
| 350 | self.assertIn(b"Unfortunately", contents) |
| 351 | |
| 352 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 353 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 354 | support.requires("network") |
| 355 | support.run_unittest(AuthTests, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 356 | HTTPSTests, |
| 357 | OtherNetworkTests, |
| 358 | CloseSocketTest, |
| 359 | TimeoutTest, |
| 360 | ) |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 361 | |
| 362 | if __name__ == "__main__": |
| 363 | test_main() |