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