blob: ba4c500e8ec3ef6cc2274d3fd9b78ff3d30a0ed7 [file] [log] [blame]
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +03003from test.support import socket_helper
Thomas Wouters477c8d52006-05-27 19:21:47 +00004from test.test_urllib2 import sanepathname2url
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00005
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00006import os
Jeremy Hylton1afc1692008-06-18 20:49:58 +00007import socket
Jeremy Hylton1afc1692008-06-18 20:49:58 +00008import urllib.error
9import urllib.request
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +000010import sys
Berker Peksagb77983d2014-10-10 14:34:16 +030011
doko@ubuntu.come5751482013-12-26 17:37:11 +010012support.requires("network")
13
Christian Heimes969fe572008-01-25 11:23:10 +000014
Georg Brandlc28e1fa2008-06-10 19:20:26 +000015def _retry_thrice(func, exc, *args, **kwargs):
Christian Heimes969fe572008-01-25 11:23:10 +000016 for i in range(3):
17 try:
Georg Brandlc28e1fa2008-06-10 19:20:26 +000018 return func(*args, **kwargs)
19 except exc as e:
Neal Norwitz2f142582008-01-26 19:49:41 +000020 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000021 continue
Christian Heimes969fe572008-01-25 11:23:10 +000022 raise last_exc
23
Georg Brandlc28e1fa2008-06-10 19:20:26 +000024def _wrap_with_retry_thrice(func, exc):
25 def wrapped(*args, **kwargs):
26 return _retry_thrice(func, exc, *args, **kwargs)
27 return wrapped
28
Victor Stinnerc11b3b12018-12-05 01:58:31 +010029# bpo-35411: FTP tests of test_urllib2net randomly fail
30# with "425 Security: Bad IP connecting" on Travis CI
31skip_ftp_test_on_travis = unittest.skipIf('TRAVIS' in os.environ,
32 'bpo-35411: skip FTP test '
33 'on Travis CI')
34
35
Georg Brandlc28e1fa2008-06-10 19:20:26 +000036# Connecting to remote hosts is flaky. Make it more robust by retrying
37# the connection several times.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000038_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
39 urllib.error.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000040
Thomas Wouters477c8d52006-05-27 19:21:47 +000041
42class 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 Brandl24420152008-05-26 16:32:26 +000049# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000050#
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 Heimes969fe572008-01-25 11:23:10 +000059# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000060# 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 Heimes969fe572008-01-25 11:23:10 +000071# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +000072#
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 Brandl24420152008-05-26 16:32:26 +000077# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +000078# urllib2.urlopen, "http://evil:thing@example.com")
79
80
Thomas Woutersb2137042007-02-01 18:02:27 +000081class CloseSocketTest(unittest.TestCase):
82
83 def test_close(self):
Victor Stinner7cb92042019-07-02 14:50:19 +020084 # clear _opener global variable
85 self.addCleanup(urllib.request.urlcleanup)
86
Thomas Woutersb2137042007-02-01 18:02:27 +000087 # calling .close() on urllib2's response objects should close the
88 # underlying socket
Stéphane Wirtela40681d2019-02-22 14:45:36 +010089 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +030090 with socket_helper.transient_internet(url):
Nadeem Vawda61baebd2012-01-25 08:02:05 +020091 response = _urlopen_with_retry(url)
92 sock = response.fp
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +020093 self.assertFalse(sock.closed)
Nadeem Vawda61baebd2012-01-25 08:02:05 +020094 response.close()
95 self.assertTrue(sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +000096
Thomas Wouters477c8d52006-05-27 19:21:47 +000097class 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 Wouters477c8d52006-05-27 19:21:47 +0000104 # 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 Stinnerc11b3b12018-12-05 01:58:31 +0100107 @skip_ftp_test_on_travis
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108 def test_ftp(self):
109 urls = [
Ammar Askard81bea62017-07-18 19:27:24 -0700110 'ftp://www.pythontest.net/README',
111 ('ftp://www.pythontest.net/non-existent-file',
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200112 None, urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113 ]
114 self._test_urls(urls, self._extra_handlers())
115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000117 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118 f = open(TESTFN, 'w')
119 try:
120 f.write('hi there\n')
121 f.close()
122 urls = [
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000123 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
124 ('file:///nonsensename/etc/passwd', None,
125 urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126 ]
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000127 self._test_urls(urls, self._extra_handlers(), retry=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128 finally:
129 os.remove(TESTFN)
130
Senthil Kumaran3800ea92012-01-21 11:52:48 +0800131 self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file')
132
Thomas Wouters477c8d52006-05-27 19:21:47 +0000133 # 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 Kumarand95cc752010-08-08 11:27:53 +0000161 def test_urlwithfrag(self):
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500162 urlwith_frag = "http://www.pythontest.net/index.html#frag"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300163 with socket_helper.transient_internet(urlwith_frag):
Georg Brandl5be365f2010-10-28 14:55:02 +0000164 req = urllib.request.Request(urlwith_frag)
165 res = urllib.request.urlopen(req)
166 self.assertEqual(res.geturl(),
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500167 "http://www.pythontest.net/index.html#frag")
Senthil Kumarand95cc752010-08-08 11:27:53 +0000168
Senthil Kumaran83070752013-05-24 09:14:12 -0700169 def test_redirect_url_withfrag(self):
Benjamin Petersonb811a972014-11-05 13:10:08 -0500170 redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300171 with socket_helper.transient_internet(redirect_url_with_frag):
Senthil Kumaran83070752013-05-24 09:14:12 -0700172 req = urllib.request.Request(redirect_url_with_frag)
173 res = urllib.request.urlopen(req)
174 self.assertEqual(res.geturl(),
Benjamin Petersonb811a972014-11-05 13:10:08 -0500175 "http://www.pythontest.net/elsewhere/#frag")
Senthil Kumaran83070752013-05-24 09:14:12 -0700176
Senthil Kumaran42ef4b12010-09-27 01:26:03 +0000177 def test_custom_headers(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100178 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300179 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000180 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 Kumaran42ef4b12010-09-27 01:26:03 +0000189
INADA Naoki36d56ea2018-04-18 00:31:29 +0900190 @unittest.skip('XXX: http://www.imdb.com is gone')
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800191 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 Kumarane324c572011-07-31 11:45:14 +0800195 URL = 'http://www.imdb.com' # mangles Connection:close
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800196
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300197 with socket_helper.transient_internet(URL):
Senthil Kumarane324c572011-07-31 11:45:14 +0800198 try:
199 with urllib.request.urlopen(URL) as res:
200 pass
Pablo Galindo293dd232019-11-19 21:34:03 +0000201 except ValueError:
Senthil Kumarane324c572011-07-31 11:45:14 +0800202 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 Kumaran1299a8f2011-07-27 08:05:58 +0800210
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000211 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000212 import time
213 import logging
214 debug = logging.getLogger("test_urllib2").debug
215
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000216 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000217 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000218 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000219
220 for url in urls:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200221 with self.subTest(url=url):
222 if isinstance(url, tuple):
223 url, req, expected_err = url
Georg Brandl5be365f2010-10-28 14:55:02 +0000224 else:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200225 req = expected_err = None
226
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300227 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000228 try:
Victor Stinner1d0f9b32019-12-10 22:09:23 +0100229 f = urlopen(url, req, support.INTERNET_TIMEOUT)
Berker Peksag8b63d3a2014-10-25 05:42:30 +0300230 # urllib.error.URLError is a subclass of OSError
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200231 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 Pitroubc2c4c92014-09-17 00:39:21 +0200238 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 Wouters477c8d52006-05-27 19:21:47 +0000249
250 def _extra_handlers(self):
251 handlers = []
252
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000253 cfh = urllib.request.CacheFTPHandler()
Nadeem Vawda08f5f7a2011-07-23 14:03:00 +0200254 self.addCleanup(cfh.clear_cache)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 cfh.setTimeout(1)
256 handlers.append(cfh)
257
258 return handlers
259
Christian Heimesbbe741d2008-03-28 10:53:29 +0000260
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000261class TimeoutTest(unittest.TestCase):
Victor Stinner7cb92042019-07-02 14:50:19 +0200262 def setUp(self):
263 # clear _opener global variable
264 self.addCleanup(urllib.request.urlcleanup)
265
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000266 def test_http_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200267 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100268 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300269 with socket_helper.transient_internet(url, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000270 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200271 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200272 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000273
Georg Brandlf78e02b2008-06-10 17:40:04 +0000274 def test_http_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200275 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100276 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300277 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000278 socket.setdefaulttimeout(60)
279 try:
280 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200281 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000282 finally:
283 socket.setdefaulttimeout(None)
284 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000285
286 def test_http_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200287 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100288 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300289 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000290 socket.setdefaulttimeout(60)
291 try:
292 u = _urlopen_with_retry(url, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200293 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000294 finally:
295 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200296 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000297
Georg Brandlf78e02b2008-06-10 17:40:04 +0000298 def test_http_timeout(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100299 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300300 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000301 u = _urlopen_with_retry(url, timeout=120)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200302 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000303 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000304
Ammar Askard81bea62017-07-18 19:27:24 -0700305 FTP_HOST = 'ftp://www.pythontest.net/'
Christian Heimes969fe572008-01-25 11:23:10 +0000306
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100307 @skip_ftp_test_on_travis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000308 def test_ftp_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200309 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300310 with socket_helper.transient_internet(self.FTP_HOST, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000311 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200312 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200313 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000314
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100315 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000316 def test_ftp_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200317 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300318 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000319 socket.setdefaulttimeout(60)
320 try:
321 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200322 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000323 finally:
324 socket.setdefaulttimeout(None)
325 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000326
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100327 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000328 def test_ftp_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200329 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300330 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000331 socket.setdefaulttimeout(60)
332 try:
333 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200334 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000335 finally:
336 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200337 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000338
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100339 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000340 def test_ftp_timeout(self):
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300341 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000342 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200343 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000344 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000345
Thomas Wouters477c8d52006-05-27 19:21:47 +0000346
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000347if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400348 unittest.main()