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