Victor Stinner | 311110a | 2020-06-11 18:26:23 +0200 | [diff] [blame] | 1 | import errno |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 2 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 4 | from test.support import os_helper |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 5 | from test.support import socket_helper |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | from test.test_urllib2 import sanepathname2url |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 7 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 8 | import os |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 9 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 10 | import urllib.error |
| 11 | import urllib.request |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 12 | import sys |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 13 | |
doko@ubuntu.com | e575148 | 2013-12-26 17:37:11 +0100 | [diff] [blame] | 14 | support.requires("network") |
| 15 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 16 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 17 | def _retry_thrice(func, exc, *args, **kwargs): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 18 | for i in range(3): |
| 19 | try: |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 20 | return func(*args, **kwargs) |
| 21 | except exc as e: |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 22 | last_exc = e |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 23 | continue |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 24 | raise last_exc |
| 25 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 26 | def _wrap_with_retry_thrice(func, exc): |
| 27 | def wrapped(*args, **kwargs): |
| 28 | return _retry_thrice(func, exc, *args, **kwargs) |
| 29 | return wrapped |
| 30 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 31 | # bpo-35411: FTP tests of test_urllib2net randomly fail |
| 32 | # with "425 Security: Bad IP connecting" on Travis CI |
| 33 | skip_ftp_test_on_travis = unittest.skipIf('TRAVIS' in os.environ, |
| 34 | 'bpo-35411: skip FTP test ' |
| 35 | 'on Travis CI') |
| 36 | |
| 37 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 38 | # Connecting to remote hosts is flaky. Make it more robust by retrying |
| 39 | # the connection several times. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 40 | _urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen, |
| 41 | urllib.error.URLError) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 42 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 43 | |
Victor Stinner | 311110a | 2020-06-11 18:26:23 +0200 | [diff] [blame] | 44 | class TransientResource(object): |
| 45 | |
| 46 | """Raise ResourceDenied if an exception is raised while the context manager |
| 47 | is in effect that matches the specified exception and attributes.""" |
| 48 | |
| 49 | def __init__(self, exc, **kwargs): |
| 50 | self.exc = exc |
| 51 | self.attrs = kwargs |
| 52 | |
| 53 | def __enter__(self): |
| 54 | return self |
| 55 | |
| 56 | def __exit__(self, type_=None, value=None, traceback=None): |
| 57 | """If type_ is a subclass of self.exc and value has attributes matching |
| 58 | self.attrs, raise ResourceDenied. Otherwise let the exception |
| 59 | propagate (if any).""" |
| 60 | if type_ is not None and issubclass(self.exc, type_): |
| 61 | for attr, attr_value in self.attrs.items(): |
| 62 | if not hasattr(value, attr): |
| 63 | break |
| 64 | if getattr(value, attr) != attr_value: |
| 65 | break |
| 66 | else: |
| 67 | raise ResourceDenied("an optional resource is not available") |
| 68 | |
| 69 | # Context managers that raise ResourceDenied when various issues |
| 70 | # with the Internet connection manifest themselves as exceptions. |
| 71 | # XXX deprecate these and use transient_internet() instead |
| 72 | time_out = TransientResource(OSError, errno=errno.ETIMEDOUT) |
| 73 | socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET) |
| 74 | ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET) |
| 75 | |
| 76 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 77 | class AuthTests(unittest.TestCase): |
| 78 | """Tests urllib2 authentication features.""" |
| 79 | |
| 80 | ## Disabled at the moment since there is no page under python.org which |
| 81 | ## could be used to HTTP authentication. |
| 82 | # |
| 83 | # def test_basic_auth(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 84 | # import http.client |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 85 | # |
| 86 | # test_url = "http://www.python.org/test/test_urllib2/basic_auth" |
| 87 | # test_hostport = "www.python.org" |
| 88 | # test_realm = 'Test Realm' |
| 89 | # test_user = 'test.test_urllib2net' |
| 90 | # test_password = 'blah' |
| 91 | # |
| 92 | # # failure |
| 93 | # try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 94 | # _urlopen_with_retry(test_url) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | # except urllib2.HTTPError, exc: |
| 96 | # self.assertEqual(exc.code, 401) |
| 97 | # else: |
| 98 | # self.fail("urlopen() should have failed with 401") |
| 99 | # |
| 100 | # # success |
| 101 | # auth_handler = urllib2.HTTPBasicAuthHandler() |
| 102 | # auth_handler.add_password(test_realm, test_hostport, |
| 103 | # test_user, test_password) |
| 104 | # opener = urllib2.build_opener(auth_handler) |
| 105 | # f = opener.open('http://localhost/') |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 106 | # response = _urlopen_with_retry("http://www.python.org/") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 107 | # |
| 108 | # # The 'userinfo' URL component is deprecated by RFC 3986 for security |
| 109 | # # reasons, let's not implement it! (it's already implemented for proxy |
| 110 | # # specification strings (that is, URLs or authorities specifying a |
| 111 | # # proxy), so we must keep that) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 112 | # self.assertRaises(http.client.InvalidURL, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 113 | # urllib2.urlopen, "http://evil:thing@example.com") |
| 114 | |
| 115 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 116 | class CloseSocketTest(unittest.TestCase): |
| 117 | |
| 118 | def test_close(self): |
Victor Stinner | 7cb9204 | 2019-07-02 14:50:19 +0200 | [diff] [blame] | 119 | # clear _opener global variable |
| 120 | self.addCleanup(urllib.request.urlcleanup) |
| 121 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 122 | # calling .close() on urllib2's response objects should close the |
| 123 | # underlying socket |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 124 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 125 | with socket_helper.transient_internet(url): |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 126 | response = _urlopen_with_retry(url) |
| 127 | sock = response.fp |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 128 | self.assertFalse(sock.closed) |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 129 | response.close() |
| 130 | self.assertTrue(sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 131 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 132 | class OtherNetworkTests(unittest.TestCase): |
| 133 | def setUp(self): |
| 134 | if 0: # for debugging |
| 135 | import logging |
| 136 | logger = logging.getLogger("test_urllib2net") |
| 137 | logger.addHandler(logging.StreamHandler()) |
| 138 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 139 | # XXX The rest of these tests aren't very good -- they don't check much. |
| 140 | # They do sometimes catch some major disasters, though. |
| 141 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 142 | @skip_ftp_test_on_travis |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 143 | def test_ftp(self): |
| 144 | urls = [ |
Ammar Askar | d81bea6 | 2017-07-18 19:27:24 -0700 | [diff] [blame] | 145 | 'ftp://www.pythontest.net/README', |
| 146 | ('ftp://www.pythontest.net/non-existent-file', |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 147 | None, urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 148 | ] |
| 149 | self._test_urls(urls, self._extra_handlers()) |
| 150 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 151 | def test_file(self): |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 152 | TESTFN = os_helper.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 153 | f = open(TESTFN, 'w') |
| 154 | try: |
| 155 | f.write('hi there\n') |
| 156 | f.close() |
| 157 | urls = [ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 158 | 'file:' + sanepathname2url(os.path.abspath(TESTFN)), |
| 159 | ('file:///nonsensename/etc/passwd', None, |
| 160 | urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 161 | ] |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 162 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 163 | finally: |
| 164 | os.remove(TESTFN) |
| 165 | |
Senthil Kumaran | 3800ea9 | 2012-01-21 11:52:48 +0800 | [diff] [blame] | 166 | self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file') |
| 167 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 168 | # XXX Following test depends on machine configurations that are internal |
| 169 | # to CNRI. Need to set up a public server with the right authentication |
| 170 | # configuration for test purposes. |
| 171 | |
| 172 | ## def test_cnri(self): |
| 173 | ## if socket.gethostname() == 'bitdiddle': |
| 174 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 175 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 176 | ## localhost = 'localhost' |
| 177 | ## else: |
| 178 | ## localhost = None |
| 179 | ## if localhost is not None: |
| 180 | ## urls = [ |
| 181 | ## 'file://%s/etc/passwd' % localhost, |
| 182 | ## 'http://%s/simple/' % localhost, |
| 183 | ## 'http://%s/digest/' % localhost, |
| 184 | ## 'http://%s/not/found.h' % localhost, |
| 185 | ## ] |
| 186 | |
| 187 | ## bauth = HTTPBasicAuthHandler() |
| 188 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 189 | ## 'password') |
| 190 | ## dauth = HTTPDigestAuthHandler() |
| 191 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 192 | ## 'password') |
| 193 | |
| 194 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 195 | |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 196 | def test_urlwithfrag(self): |
Benjamin Peterson | 258f3f0 | 2014-11-05 11:27:14 -0500 | [diff] [blame] | 197 | urlwith_frag = "http://www.pythontest.net/index.html#frag" |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 198 | with socket_helper.transient_internet(urlwith_frag): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 199 | req = urllib.request.Request(urlwith_frag) |
| 200 | res = urllib.request.urlopen(req) |
| 201 | self.assertEqual(res.geturl(), |
Benjamin Peterson | 258f3f0 | 2014-11-05 11:27:14 -0500 | [diff] [blame] | 202 | "http://www.pythontest.net/index.html#frag") |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 203 | |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 204 | def test_redirect_url_withfrag(self): |
Benjamin Peterson | b811a97 | 2014-11-05 13:10:08 -0500 | [diff] [blame] | 205 | redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/" |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 206 | with socket_helper.transient_internet(redirect_url_with_frag): |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 207 | req = urllib.request.Request(redirect_url_with_frag) |
| 208 | res = urllib.request.urlopen(req) |
| 209 | self.assertEqual(res.geturl(), |
Benjamin Peterson | b811a97 | 2014-11-05 13:10:08 -0500 | [diff] [blame] | 210 | "http://www.pythontest.net/elsewhere/#frag") |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 211 | |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 212 | def test_custom_headers(self): |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 213 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 214 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 215 | opener = urllib.request.build_opener() |
| 216 | request = urllib.request.Request(url) |
| 217 | self.assertFalse(request.header_items()) |
| 218 | opener.open(request) |
| 219 | self.assertTrue(request.header_items()) |
| 220 | self.assertTrue(request.has_header('User-agent')) |
| 221 | request.add_header('User-Agent','Test-Agent') |
| 222 | opener.open(request) |
| 223 | self.assertEqual(request.get_header('User-agent'),'Test-Agent') |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 224 | |
INADA Naoki | 36d56ea | 2018-04-18 00:31:29 +0900 | [diff] [blame] | 225 | @unittest.skip('XXX: http://www.imdb.com is gone') |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 226 | def test_sites_no_connection_close(self): |
| 227 | # Some sites do not send Connection: close header. |
| 228 | # Verify that those work properly. (#issue12576) |
| 229 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 230 | URL = 'http://www.imdb.com' # mangles Connection:close |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 231 | |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 232 | with socket_helper.transient_internet(URL): |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 233 | try: |
| 234 | with urllib.request.urlopen(URL) as res: |
| 235 | pass |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 236 | except ValueError: |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 237 | self.fail("urlopen failed for site not sending \ |
| 238 | Connection:close") |
| 239 | else: |
| 240 | self.assertTrue(res) |
| 241 | |
| 242 | req = urllib.request.urlopen(URL) |
| 243 | res = req.read() |
| 244 | self.assertTrue(res) |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 245 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 246 | def _test_urls(self, urls, handlers, retry=True): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 247 | import time |
| 248 | import logging |
| 249 | debug = logging.getLogger("test_urllib2").debug |
| 250 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 251 | urlopen = urllib.request.build_opener(*handlers).open |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 252 | if retry: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 253 | urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 254 | |
| 255 | for url in urls: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 256 | with self.subTest(url=url): |
| 257 | if isinstance(url, tuple): |
| 258 | url, req, expected_err = url |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 259 | else: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 260 | req = expected_err = None |
| 261 | |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 262 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 263 | try: |
Victor Stinner | 1d0f9b3 | 2019-12-10 22:09:23 +0100 | [diff] [blame] | 264 | f = urlopen(url, req, support.INTERNET_TIMEOUT) |
Berker Peksag | 8b63d3a | 2014-10-25 05:42:30 +0300 | [diff] [blame] | 265 | # urllib.error.URLError is a subclass of OSError |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 266 | except OSError as err: |
| 267 | if expected_err: |
| 268 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 269 | (expected_err, url, req, type(err), err)) |
| 270 | self.assertIsInstance(err, expected_err, msg) |
| 271 | else: |
| 272 | raise |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 273 | else: |
| 274 | try: |
Victor Stinner | 311110a | 2020-06-11 18:26:23 +0200 | [diff] [blame] | 275 | with time_out, \ |
| 276 | socket_peer_reset, \ |
| 277 | ioerror_peer_reset: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 278 | buf = f.read() |
| 279 | debug("read %d bytes" % len(buf)) |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 280 | except TimeoutError: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 281 | print("<timeout: %s>" % url, file=sys.stderr) |
| 282 | f.close() |
| 283 | time.sleep(0.1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 284 | |
| 285 | def _extra_handlers(self): |
| 286 | handlers = [] |
| 287 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 288 | cfh = urllib.request.CacheFTPHandler() |
Nadeem Vawda | 08f5f7a | 2011-07-23 14:03:00 +0200 | [diff] [blame] | 289 | self.addCleanup(cfh.clear_cache) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 290 | cfh.setTimeout(1) |
| 291 | handlers.append(cfh) |
| 292 | |
| 293 | return handlers |
| 294 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 296 | class TimeoutTest(unittest.TestCase): |
Victor Stinner | 7cb9204 | 2019-07-02 14:50:19 +0200 | [diff] [blame] | 297 | def setUp(self): |
| 298 | # clear _opener global variable |
| 299 | self.addCleanup(urllib.request.urlcleanup) |
| 300 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 301 | def test_http_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 302 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 303 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 304 | with socket_helper.transient_internet(url, timeout=None): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 305 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 306 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 307 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 308 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 309 | def test_http_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 310 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 311 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 312 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 313 | socket.setdefaulttimeout(60) |
| 314 | try: |
| 315 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 316 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 317 | finally: |
| 318 | socket.setdefaulttimeout(None) |
| 319 | self.assertEqual(u.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 320 | |
| 321 | def test_http_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 322 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 323 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 324 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 325 | socket.setdefaulttimeout(60) |
| 326 | try: |
| 327 | u = _urlopen_with_retry(url, timeout=None) |
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 | finally: |
| 330 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 331 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 332 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 333 | def test_http_timeout(self): |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 334 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 335 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 336 | u = _urlopen_with_retry(url, timeout=120) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 337 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 338 | self.assertEqual(u.fp.raw._sock.gettimeout(), 120) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 339 | |
Ammar Askar | d81bea6 | 2017-07-18 19:27:24 -0700 | [diff] [blame] | 340 | FTP_HOST = 'ftp://www.pythontest.net/' |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 341 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 342 | @skip_ftp_test_on_travis |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 343 | def test_ftp_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 344 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 345 | with socket_helper.transient_internet(self.FTP_HOST, timeout=None): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 346 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 347 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 348 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 349 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 350 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 351 | def test_ftp_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 352 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 353 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 354 | socket.setdefaulttimeout(60) |
| 355 | try: |
| 356 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 357 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 358 | finally: |
| 359 | socket.setdefaulttimeout(None) |
| 360 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 361 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 362 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 363 | def test_ftp_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 364 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 365 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 366 | socket.setdefaulttimeout(60) |
| 367 | try: |
| 368 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 369 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 370 | finally: |
| 371 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 372 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 373 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 374 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 375 | def test_ftp_timeout(self): |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 376 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 377 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 378 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 379 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 380 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 381 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 382 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 383 | unittest.main() |