blob: cb74685715d35b987073a6d5425317171ce41963 [file] [log] [blame]
Victor Stinner311110a2020-06-11 18:26:23 +02001import errno
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00002import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +03004from test.support import socket_helper
Thomas Wouters477c8d52006-05-27 19:21:47 +00005from test.test_urllib2 import sanepathname2url
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00006
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00007import os
Jeremy Hylton1afc1692008-06-18 20:49:58 +00008import socket
Jeremy Hylton1afc1692008-06-18 20:49:58 +00009import urllib.error
10import urllib.request
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +000011import sys
Berker Peksagb77983d2014-10-10 14:34:16 +030012
doko@ubuntu.come5751482013-12-26 17:37:11 +010013support.requires("network")
14
Christian Heimes969fe572008-01-25 11:23:10 +000015
Georg Brandlc28e1fa2008-06-10 19:20:26 +000016def _retry_thrice(func, exc, *args, **kwargs):
Christian Heimes969fe572008-01-25 11:23:10 +000017 for i in range(3):
18 try:
Georg Brandlc28e1fa2008-06-10 19:20:26 +000019 return func(*args, **kwargs)
20 except exc as e:
Neal Norwitz2f142582008-01-26 19:49:41 +000021 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000022 continue
Christian Heimes969fe572008-01-25 11:23:10 +000023 raise last_exc
24
Georg Brandlc28e1fa2008-06-10 19:20:26 +000025def _wrap_with_retry_thrice(func, exc):
26 def wrapped(*args, **kwargs):
27 return _retry_thrice(func, exc, *args, **kwargs)
28 return wrapped
29
Victor Stinnerc11b3b12018-12-05 01:58:31 +010030# bpo-35411: FTP tests of test_urllib2net randomly fail
31# with "425 Security: Bad IP connecting" on Travis CI
32skip_ftp_test_on_travis = unittest.skipIf('TRAVIS' in os.environ,
33 'bpo-35411: skip FTP test '
34 'on Travis CI')
35
36
Georg Brandlc28e1fa2008-06-10 19:20:26 +000037# Connecting to remote hosts is flaky. Make it more robust by retrying
38# the connection several times.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000039_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
40 urllib.error.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000041
Thomas Wouters477c8d52006-05-27 19:21:47 +000042
Victor Stinner311110a2020-06-11 18:26:23 +020043class 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
71time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
72socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
73ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
74
75
Thomas Wouters477c8d52006-05-27 19:21:47 +000076class 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 Brandl24420152008-05-26 16:32:26 +000083# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000084#
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 Heimes969fe572008-01-25 11:23:10 +000093# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000094# 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 Heimes969fe572008-01-25 11:23:10 +0000105# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106#
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 Brandl24420152008-05-26 16:32:26 +0000111# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112# urllib2.urlopen, "http://evil:thing@example.com")
113
114
Thomas Woutersb2137042007-02-01 18:02:27 +0000115class CloseSocketTest(unittest.TestCase):
116
117 def test_close(self):
Victor Stinner7cb92042019-07-02 14:50:19 +0200118 # clear _opener global variable
119 self.addCleanup(urllib.request.urlcleanup)
120
Thomas Woutersb2137042007-02-01 18:02:27 +0000121 # calling .close() on urllib2's response objects should close the
122 # underlying socket
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100123 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300124 with socket_helper.transient_internet(url):
Nadeem Vawda61baebd2012-01-25 08:02:05 +0200125 response = _urlopen_with_retry(url)
126 sock = response.fp
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200127 self.assertFalse(sock.closed)
Nadeem Vawda61baebd2012-01-25 08:02:05 +0200128 response.close()
129 self.assertTrue(sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +0000130
Thomas Wouters477c8d52006-05-27 19:21:47 +0000131class 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 Wouters477c8d52006-05-27 19:21:47 +0000138 # 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 Stinnerc11b3b12018-12-05 01:58:31 +0100141 @skip_ftp_test_on_travis
Thomas Wouters477c8d52006-05-27 19:21:47 +0000142 def test_ftp(self):
143 urls = [
Ammar Askard81bea62017-07-18 19:27:24 -0700144 'ftp://www.pythontest.net/README',
145 ('ftp://www.pythontest.net/non-existent-file',
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200146 None, urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 ]
148 self._test_urls(urls, self._extra_handlers())
149
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000151 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152 f = open(TESTFN, 'w')
153 try:
154 f.write('hi there\n')
155 f.close()
156 urls = [
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000157 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
158 ('file:///nonsensename/etc/passwd', None,
159 urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000160 ]
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000161 self._test_urls(urls, self._extra_handlers(), retry=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000162 finally:
163 os.remove(TESTFN)
164
Senthil Kumaran3800ea92012-01-21 11:52:48 +0800165 self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file')
166
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167 # 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 Kumarand95cc752010-08-08 11:27:53 +0000195 def test_urlwithfrag(self):
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500196 urlwith_frag = "http://www.pythontest.net/index.html#frag"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300197 with socket_helper.transient_internet(urlwith_frag):
Georg Brandl5be365f2010-10-28 14:55:02 +0000198 req = urllib.request.Request(urlwith_frag)
199 res = urllib.request.urlopen(req)
200 self.assertEqual(res.geturl(),
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500201 "http://www.pythontest.net/index.html#frag")
Senthil Kumarand95cc752010-08-08 11:27:53 +0000202
Senthil Kumaran83070752013-05-24 09:14:12 -0700203 def test_redirect_url_withfrag(self):
Benjamin Petersonb811a972014-11-05 13:10:08 -0500204 redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300205 with socket_helper.transient_internet(redirect_url_with_frag):
Senthil Kumaran83070752013-05-24 09:14:12 -0700206 req = urllib.request.Request(redirect_url_with_frag)
207 res = urllib.request.urlopen(req)
208 self.assertEqual(res.geturl(),
Benjamin Petersonb811a972014-11-05 13:10:08 -0500209 "http://www.pythontest.net/elsewhere/#frag")
Senthil Kumaran83070752013-05-24 09:14:12 -0700210
Senthil Kumaran42ef4b12010-09-27 01:26:03 +0000211 def test_custom_headers(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100212 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300213 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000214 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 Kumaran42ef4b12010-09-27 01:26:03 +0000223
INADA Naoki36d56ea2018-04-18 00:31:29 +0900224 @unittest.skip('XXX: http://www.imdb.com is gone')
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800225 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 Kumarane324c572011-07-31 11:45:14 +0800229 URL = 'http://www.imdb.com' # mangles Connection:close
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800230
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300231 with socket_helper.transient_internet(URL):
Senthil Kumarane324c572011-07-31 11:45:14 +0800232 try:
233 with urllib.request.urlopen(URL) as res:
234 pass
Pablo Galindo293dd232019-11-19 21:34:03 +0000235 except ValueError:
Senthil Kumarane324c572011-07-31 11:45:14 +0800236 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 Kumaran1299a8f2011-07-27 08:05:58 +0800244
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000245 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 import time
247 import logging
248 debug = logging.getLogger("test_urllib2").debug
249
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000250 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000251 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000252 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253
254 for url in urls:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200255 with self.subTest(url=url):
256 if isinstance(url, tuple):
257 url, req, expected_err = url
Georg Brandl5be365f2010-10-28 14:55:02 +0000258 else:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200259 req = expected_err = None
260
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300261 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000262 try:
Victor Stinner1d0f9b32019-12-10 22:09:23 +0100263 f = urlopen(url, req, support.INTERNET_TIMEOUT)
Berker Peksag8b63d3a2014-10-25 05:42:30 +0300264 # urllib.error.URLError is a subclass of OSError
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200265 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 Pitroubc2c4c92014-09-17 00:39:21 +0200272 else:
273 try:
Victor Stinner311110a2020-06-11 18:26:23 +0200274 with time_out, \
275 socket_peer_reset, \
276 ioerror_peer_reset:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200277 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 Wouters477c8d52006-05-27 19:21:47 +0000283
284 def _extra_handlers(self):
285 handlers = []
286
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000287 cfh = urllib.request.CacheFTPHandler()
Nadeem Vawda08f5f7a2011-07-23 14:03:00 +0200288 self.addCleanup(cfh.clear_cache)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289 cfh.setTimeout(1)
290 handlers.append(cfh)
291
292 return handlers
293
Christian Heimesbbe741d2008-03-28 10:53:29 +0000294
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000295class TimeoutTest(unittest.TestCase):
Victor Stinner7cb92042019-07-02 14:50:19 +0200296 def setUp(self):
297 # clear _opener global variable
298 self.addCleanup(urllib.request.urlcleanup)
299
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000300 def test_http_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200301 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100302 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300303 with socket_helper.transient_internet(url, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000304 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200305 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200306 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000307
Georg Brandlf78e02b2008-06-10 17:40:04 +0000308 def test_http_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200309 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100310 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300311 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000312 socket.setdefaulttimeout(60)
313 try:
314 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200315 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000316 finally:
317 socket.setdefaulttimeout(None)
318 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000319
320 def test_http_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200321 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100322 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300323 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000324 socket.setdefaulttimeout(60)
325 try:
326 u = _urlopen_with_retry(url, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200327 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000328 finally:
329 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200330 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000331
Georg Brandlf78e02b2008-06-10 17:40:04 +0000332 def test_http_timeout(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100333 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300334 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000335 u = _urlopen_with_retry(url, timeout=120)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200336 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000337 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000338
Ammar Askard81bea62017-07-18 19:27:24 -0700339 FTP_HOST = 'ftp://www.pythontest.net/'
Christian Heimes969fe572008-01-25 11:23:10 +0000340
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100341 @skip_ftp_test_on_travis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000342 def test_ftp_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200343 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300344 with socket_helper.transient_internet(self.FTP_HOST, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000345 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200346 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200347 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000348
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100349 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000350 def test_ftp_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200351 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300352 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000353 socket.setdefaulttimeout(60)
354 try:
355 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200356 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000357 finally:
358 socket.setdefaulttimeout(None)
359 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000360
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100361 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000362 def test_ftp_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200363 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300364 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000365 socket.setdefaulttimeout(60)
366 try:
367 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200368 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000369 finally:
370 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200371 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000372
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100373 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000374 def test_ftp_timeout(self):
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300375 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000376 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200377 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000378 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000379
Thomas Wouters477c8d52006-05-27 19:21:47 +0000380
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000381if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400382 unittest.main()