Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 3 | from test.support import socket_helper |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4 | from test.test_urllib2 import sanepathname2url |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 6 | import os |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 7 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 8 | import urllib.error |
| 9 | import urllib.request |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 10 | import sys |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 11 | |
doko@ubuntu.com | e575148 | 2013-12-26 17:37:11 +0100 | [diff] [blame] | 12 | support.requires("network") |
| 13 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 14 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 15 | def _retry_thrice(func, exc, *args, **kwargs): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 16 | for i in range(3): |
| 17 | try: |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 18 | return func(*args, **kwargs) |
| 19 | except exc as e: |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 20 | last_exc = e |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 21 | continue |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 22 | raise last_exc |
| 23 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 24 | def _wrap_with_retry_thrice(func, exc): |
| 25 | def wrapped(*args, **kwargs): |
| 26 | return _retry_thrice(func, exc, *args, **kwargs) |
| 27 | return wrapped |
| 28 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 29 | # bpo-35411: FTP tests of test_urllib2net randomly fail |
| 30 | # with "425 Security: Bad IP connecting" on Travis CI |
| 31 | skip_ftp_test_on_travis = unittest.skipIf('TRAVIS' in os.environ, |
| 32 | 'bpo-35411: skip FTP test ' |
| 33 | 'on Travis CI') |
| 34 | |
| 35 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 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): |
Victor Stinner | 7cb9204 | 2019-07-02 14:50:19 +0200 | [diff] [blame] | 84 | # clear _opener global variable |
| 85 | self.addCleanup(urllib.request.urlcleanup) |
| 86 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 87 | # calling .close() on urllib2's response objects should close the |
| 88 | # underlying socket |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 89 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 90 | with socket_helper.transient_internet(url): |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 91 | response = _urlopen_with_retry(url) |
| 92 | sock = response.fp |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 93 | self.assertFalse(sock.closed) |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 94 | response.close() |
| 95 | self.assertTrue(sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 96 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 97 | class OtherNetworkTests(unittest.TestCase): |
| 98 | def setUp(self): |
| 99 | if 0: # for debugging |
| 100 | import logging |
| 101 | logger = logging.getLogger("test_urllib2net") |
| 102 | logger.addHandler(logging.StreamHandler()) |
| 103 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 104 | # XXX The rest of these tests aren't very good -- they don't check much. |
| 105 | # They do sometimes catch some major disasters, though. |
| 106 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 107 | @skip_ftp_test_on_travis |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 108 | def test_ftp(self): |
| 109 | urls = [ |
Ammar Askar | d81bea6 | 2017-07-18 19:27:24 -0700 | [diff] [blame] | 110 | 'ftp://www.pythontest.net/README', |
| 111 | ('ftp://www.pythontest.net/non-existent-file', |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 112 | None, urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 113 | ] |
| 114 | self._test_urls(urls, self._extra_handlers()) |
| 115 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 116 | def test_file(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 117 | TESTFN = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 118 | f = open(TESTFN, 'w') |
| 119 | try: |
| 120 | f.write('hi there\n') |
| 121 | f.close() |
| 122 | urls = [ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 123 | 'file:' + sanepathname2url(os.path.abspath(TESTFN)), |
| 124 | ('file:///nonsensename/etc/passwd', None, |
| 125 | urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | ] |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 127 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | finally: |
| 129 | os.remove(TESTFN) |
| 130 | |
Senthil Kumaran | 3800ea9 | 2012-01-21 11:52:48 +0800 | [diff] [blame] | 131 | self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file') |
| 132 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 133 | # XXX Following test depends on machine configurations that are internal |
| 134 | # to CNRI. Need to set up a public server with the right authentication |
| 135 | # configuration for test purposes. |
| 136 | |
| 137 | ## def test_cnri(self): |
| 138 | ## if socket.gethostname() == 'bitdiddle': |
| 139 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 140 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 141 | ## localhost = 'localhost' |
| 142 | ## else: |
| 143 | ## localhost = None |
| 144 | ## if localhost is not None: |
| 145 | ## urls = [ |
| 146 | ## 'file://%s/etc/passwd' % localhost, |
| 147 | ## 'http://%s/simple/' % localhost, |
| 148 | ## 'http://%s/digest/' % localhost, |
| 149 | ## 'http://%s/not/found.h' % localhost, |
| 150 | ## ] |
| 151 | |
| 152 | ## bauth = HTTPBasicAuthHandler() |
| 153 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 154 | ## 'password') |
| 155 | ## dauth = HTTPDigestAuthHandler() |
| 156 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 157 | ## 'password') |
| 158 | |
| 159 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 160 | |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 161 | def test_urlwithfrag(self): |
Benjamin Peterson | 258f3f0 | 2014-11-05 11:27:14 -0500 | [diff] [blame] | 162 | urlwith_frag = "http://www.pythontest.net/index.html#frag" |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 163 | with socket_helper.transient_internet(urlwith_frag): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 164 | req = urllib.request.Request(urlwith_frag) |
| 165 | res = urllib.request.urlopen(req) |
| 166 | self.assertEqual(res.geturl(), |
Benjamin Peterson | 258f3f0 | 2014-11-05 11:27:14 -0500 | [diff] [blame] | 167 | "http://www.pythontest.net/index.html#frag") |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 168 | |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 169 | def test_redirect_url_withfrag(self): |
Benjamin Peterson | b811a97 | 2014-11-05 13:10:08 -0500 | [diff] [blame] | 170 | redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/" |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 171 | with socket_helper.transient_internet(redirect_url_with_frag): |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 172 | req = urllib.request.Request(redirect_url_with_frag) |
| 173 | res = urllib.request.urlopen(req) |
| 174 | self.assertEqual(res.geturl(), |
Benjamin Peterson | b811a97 | 2014-11-05 13:10:08 -0500 | [diff] [blame] | 175 | "http://www.pythontest.net/elsewhere/#frag") |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 176 | |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 177 | def test_custom_headers(self): |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 178 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 179 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 180 | opener = urllib.request.build_opener() |
| 181 | request = urllib.request.Request(url) |
| 182 | self.assertFalse(request.header_items()) |
| 183 | opener.open(request) |
| 184 | self.assertTrue(request.header_items()) |
| 185 | self.assertTrue(request.has_header('User-agent')) |
| 186 | request.add_header('User-Agent','Test-Agent') |
| 187 | opener.open(request) |
| 188 | self.assertEqual(request.get_header('User-agent'),'Test-Agent') |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 189 | |
INADA Naoki | 36d56ea | 2018-04-18 00:31:29 +0900 | [diff] [blame] | 190 | @unittest.skip('XXX: http://www.imdb.com is gone') |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 191 | def test_sites_no_connection_close(self): |
| 192 | # Some sites do not send Connection: close header. |
| 193 | # Verify that those work properly. (#issue12576) |
| 194 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 195 | URL = 'http://www.imdb.com' # mangles Connection:close |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 196 | |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 197 | with socket_helper.transient_internet(URL): |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 198 | try: |
| 199 | with urllib.request.urlopen(URL) as res: |
| 200 | pass |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 201 | except ValueError: |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 202 | self.fail("urlopen failed for site not sending \ |
| 203 | Connection:close") |
| 204 | else: |
| 205 | self.assertTrue(res) |
| 206 | |
| 207 | req = urllib.request.urlopen(URL) |
| 208 | res = req.read() |
| 209 | self.assertTrue(res) |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 210 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 211 | def _test_urls(self, urls, handlers, retry=True): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 212 | import time |
| 213 | import logging |
| 214 | debug = logging.getLogger("test_urllib2").debug |
| 215 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 216 | urlopen = urllib.request.build_opener(*handlers).open |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 217 | if retry: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 218 | urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 219 | |
| 220 | for url in urls: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 221 | with self.subTest(url=url): |
| 222 | if isinstance(url, tuple): |
| 223 | url, req, expected_err = url |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 224 | else: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 225 | req = expected_err = None |
| 226 | |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 227 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 228 | try: |
Victor Stinner | 1d0f9b3 | 2019-12-10 22:09:23 +0100 | [diff] [blame] | 229 | f = urlopen(url, req, support.INTERNET_TIMEOUT) |
Berker Peksag | 8b63d3a | 2014-10-25 05:42:30 +0300 | [diff] [blame] | 230 | # urllib.error.URLError is a subclass of OSError |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 231 | except OSError as err: |
| 232 | if expected_err: |
| 233 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 234 | (expected_err, url, req, type(err), err)) |
| 235 | self.assertIsInstance(err, expected_err, msg) |
| 236 | else: |
| 237 | raise |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 238 | else: |
| 239 | try: |
| 240 | with support.time_out, \ |
| 241 | support.socket_peer_reset, \ |
| 242 | support.ioerror_peer_reset: |
| 243 | buf = f.read() |
| 244 | debug("read %d bytes" % len(buf)) |
| 245 | except socket.timeout: |
| 246 | print("<timeout: %s>" % url, file=sys.stderr) |
| 247 | f.close() |
| 248 | time.sleep(0.1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 249 | |
| 250 | def _extra_handlers(self): |
| 251 | handlers = [] |
| 252 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 253 | cfh = urllib.request.CacheFTPHandler() |
Nadeem Vawda | 08f5f7a | 2011-07-23 14:03:00 +0200 | [diff] [blame] | 254 | self.addCleanup(cfh.clear_cache) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 255 | cfh.setTimeout(1) |
| 256 | handlers.append(cfh) |
| 257 | |
| 258 | return handlers |
| 259 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 261 | class TimeoutTest(unittest.TestCase): |
Victor Stinner | 7cb9204 | 2019-07-02 14:50:19 +0200 | [diff] [blame] | 262 | def setUp(self): |
| 263 | # clear _opener global variable |
| 264 | self.addCleanup(urllib.request.urlcleanup) |
| 265 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 266 | def test_http_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 267 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 268 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 269 | with socket_helper.transient_internet(url, timeout=None): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 270 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 271 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 272 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 273 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 274 | def test_http_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 275 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 276 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 277 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 278 | socket.setdefaulttimeout(60) |
| 279 | try: |
| 280 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 281 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 282 | finally: |
| 283 | socket.setdefaulttimeout(None) |
| 284 | self.assertEqual(u.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 285 | |
| 286 | def test_http_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 287 | self.assertIsNone(socket.getdefaulttimeout()) |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 288 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 289 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 290 | socket.setdefaulttimeout(60) |
| 291 | try: |
| 292 | u = _urlopen_with_retry(url, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 293 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 294 | finally: |
| 295 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 296 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 297 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 298 | def test_http_timeout(self): |
Stéphane Wirtel | a40681d | 2019-02-22 14:45:36 +0100 | [diff] [blame] | 299 | url = support.TEST_HTTP_URL |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 300 | with socket_helper.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 301 | u = _urlopen_with_retry(url, timeout=120) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 302 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 303 | self.assertEqual(u.fp.raw._sock.gettimeout(), 120) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 304 | |
Ammar Askar | d81bea6 | 2017-07-18 19:27:24 -0700 | [diff] [blame] | 305 | FTP_HOST = 'ftp://www.pythontest.net/' |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 306 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 307 | @skip_ftp_test_on_travis |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 308 | def test_ftp_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 309 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 310 | with socket_helper.transient_internet(self.FTP_HOST, timeout=None): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 311 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 312 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 313 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 314 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 315 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 316 | def test_ftp_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 317 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 318 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 319 | socket.setdefaulttimeout(60) |
| 320 | try: |
| 321 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 322 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 323 | finally: |
| 324 | socket.setdefaulttimeout(None) |
| 325 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 326 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 327 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 328 | def test_ftp_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 329 | self.assertIsNone(socket.getdefaulttimeout()) |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 330 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 331 | socket.setdefaulttimeout(60) |
| 332 | try: |
| 333 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 334 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 335 | finally: |
| 336 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 337 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 338 | |
Victor Stinner | c11b3b1 | 2018-12-05 01:58:31 +0100 | [diff] [blame] | 339 | @skip_ftp_test_on_travis |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 340 | def test_ftp_timeout(self): |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame^] | 341 | with socket_helper.transient_internet(self.FTP_HOST): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 342 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 343 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 344 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 345 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 346 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 347 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 348 | unittest.main() |