blob: 8b9435ac60f1748c3b88fca35385de3df4d79c5d [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00002
3import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
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
12
13TIMEOUT = 60 # seconds
Jeremy Hylton5d9c3032004-08-07 17:40:50 +000014
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
23 except:
24 raise
25 raise last_exc
26
Georg Brandlc28e1fa2008-06-10 19:20:26 +000027def _wrap_with_retry_thrice(func, exc):
28 def wrapped(*args, **kwargs):
29 return _retry_thrice(func, exc, *args, **kwargs)
30 return wrapped
31
32# Connecting to remote hosts is flaky. Make it more robust by retrying
33# the connection several times.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000034_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
35 urllib.error.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000036
Thomas Wouters477c8d52006-05-27 19:21:47 +000037
38class AuthTests(unittest.TestCase):
39 """Tests urllib2 authentication features."""
40
41## Disabled at the moment since there is no page under python.org which
42## could be used to HTTP authentication.
43#
44# def test_basic_auth(self):
Georg Brandl24420152008-05-26 16:32:26 +000045# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000046#
47# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
48# test_hostport = "www.python.org"
49# test_realm = 'Test Realm'
50# test_user = 'test.test_urllib2net'
51# test_password = 'blah'
52#
53# # failure
54# try:
Christian Heimes969fe572008-01-25 11:23:10 +000055# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000056# except urllib2.HTTPError, exc:
57# self.assertEqual(exc.code, 401)
58# else:
59# self.fail("urlopen() should have failed with 401")
60#
61# # success
62# auth_handler = urllib2.HTTPBasicAuthHandler()
63# auth_handler.add_password(test_realm, test_hostport,
64# test_user, test_password)
65# opener = urllib2.build_opener(auth_handler)
66# f = opener.open('http://localhost/')
Christian Heimes969fe572008-01-25 11:23:10 +000067# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +000068#
69# # The 'userinfo' URL component is deprecated by RFC 3986 for security
70# # reasons, let's not implement it! (it's already implemented for proxy
71# # specification strings (that is, URLs or authorities specifying a
72# # proxy), so we must keep that)
Georg Brandl24420152008-05-26 16:32:26 +000073# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +000074# urllib2.urlopen, "http://evil:thing@example.com")
75
76
Thomas Woutersb2137042007-02-01 18:02:27 +000077class CloseSocketTest(unittest.TestCase):
78
79 def test_close(self):
Thomas Woutersb2137042007-02-01 18:02:27 +000080 # calling .close() on urllib2's response objects should close the
81 # underlying socket
82
Christian Heimes969fe572008-01-25 11:23:10 +000083 response = _urlopen_with_retry("http://www.python.org/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +000084 sock = response.fp
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000085 self.assertTrue(not sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +000086 response.close()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertTrue(sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +000088
Thomas Wouters477c8d52006-05-27 19:21:47 +000089class OtherNetworkTests(unittest.TestCase):
90 def setUp(self):
91 if 0: # for debugging
92 import logging
93 logger = logging.getLogger("test_urllib2net")
94 logger.addHandler(logging.StreamHandler())
95
Thomas Wouters477c8d52006-05-27 19:21:47 +000096 # XXX The rest of these tests aren't very good -- they don't check much.
97 # They do sometimes catch some major disasters, though.
98
99 def test_ftp(self):
100 urls = [
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000101 'ftp://ftp.kernel.org/pub/linux/kernel/README',
Mark Dickinson934896d2009-02-21 20:59:32 +0000102 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file',
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000103 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
105 '/research-reports/00README-Legal-Rules-Regs',
106 ]
107 self._test_urls(urls, self._extra_handlers())
108
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000110 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000111 f = open(TESTFN, 'w')
112 try:
113 f.write('hi there\n')
114 f.close()
115 urls = [
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000116 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
117 ('file:///nonsensename/etc/passwd', None,
118 urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119 ]
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000120 self._test_urls(urls, self._extra_handlers(), retry=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121 finally:
122 os.remove(TESTFN)
123
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 # XXX Following test depends on machine configurations that are internal
125 # to CNRI. Need to set up a public server with the right authentication
126 # configuration for test purposes.
127
128## def test_cnri(self):
129## if socket.gethostname() == 'bitdiddle':
130## localhost = 'bitdiddle.cnri.reston.va.us'
131## elif socket.gethostname() == 'bitdiddle.concentric.net':
132## localhost = 'localhost'
133## else:
134## localhost = None
135## if localhost is not None:
136## urls = [
137## 'file://%s/etc/passwd' % localhost,
138## 'http://%s/simple/' % localhost,
139## 'http://%s/digest/' % localhost,
140## 'http://%s/not/found.h' % localhost,
141## ]
142
143## bauth = HTTPBasicAuthHandler()
144## bauth.add_password('basic_test_realm', localhost, 'jhylton',
145## 'password')
146## dauth = HTTPDigestAuthHandler()
147## dauth.add_password('digest_test_realm', localhost, 'jhylton',
148## 'password')
149
150## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
151
Senthil Kumarand95cc752010-08-08 11:27:53 +0000152 def test_urlwithfrag(self):
153 urlwith_frag = "http://docs.python.org/glossary.html#glossary"
154 req = urllib.request.Request(urlwith_frag)
155 res = urllib.request.urlopen(req)
156 self.assertEqual(res.geturl(),
157 "http://docs.python.org/glossary.html")
158
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000159 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000160 import time
161 import logging
162 debug = logging.getLogger("test_urllib2").debug
163
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000164 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000165 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000166 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167
168 for url in urls:
169 if isinstance(url, tuple):
170 url, req, expected_err = url
171 else:
172 req = expected_err = None
173 debug(url)
174 try:
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +0000175 f = urlopen(url, req, TIMEOUT)
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000176 except EnvironmentError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000177 debug(err)
178 if expected_err:
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000179 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
180 (expected_err, url, req, type(err), err))
Ezio Melottie9615932010-01-24 19:26:24 +0000181 self.assertIsInstance(err, expected_err, msg)
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +0000182 except urllib.error.URLError as err:
183 if isinstance(err[0], socket.timeout):
184 print("<timeout: %s>" % url, file=sys.stderr)
185 continue
186 else:
187 raise
Thomas Wouters477c8d52006-05-27 19:21:47 +0000188 else:
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +0000189 try:
190 with support.time_out, \
191 support.socket_peer_reset, \
192 support.ioerror_peer_reset:
193 buf = f.read()
194 debug("read %d bytes" % len(buf))
195 except socket.timeout:
196 print("<timeout: %s>" % url, file=sys.stderr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000197 f.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 debug("******** next url coming up...")
199 time.sleep(0.1)
200
201 def _extra_handlers(self):
202 handlers = []
203
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000204 cfh = urllib.request.CacheFTPHandler()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205 cfh.setTimeout(1)
206 handlers.append(cfh)
207
208 return handlers
209
Christian Heimesbbe741d2008-03-28 10:53:29 +0000210
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000211class TimeoutTest(unittest.TestCase):
212 def test_http_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000213 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000214 u = _urlopen_with_retry("http://www.python.org")
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000215 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000216
Georg Brandlf78e02b2008-06-10 17:40:04 +0000217 def test_http_default_timeout(self):
218 self.assertTrue(socket.getdefaulttimeout() is None)
219 socket.setdefaulttimeout(60)
220 try:
221 u = _urlopen_with_retry("http://www.python.org")
222 finally:
223 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000224 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000225
226 def test_http_no_timeout(self):
227 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000228 socket.setdefaulttimeout(60)
229 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000230 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000231 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000232 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000233 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000234
Georg Brandlf78e02b2008-06-10 17:40:04 +0000235 def test_http_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000236 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000237 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000238
Benjamin Peterson87cb7872010-04-11 21:59:57 +0000239 FTP_HOST = "ftp://ftp.mirror.nl/pub/gnu/"
Christian Heimes969fe572008-01-25 11:23:10 +0000240
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000241 def test_ftp_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000242 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000243 u = _urlopen_with_retry(self.FTP_HOST)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000244 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000245
Georg Brandlf78e02b2008-06-10 17:40:04 +0000246 def test_ftp_default_timeout(self):
247 self.assertTrue(socket.getdefaulttimeout() is None)
248 socket.setdefaulttimeout(60)
249 try:
250 u = _urlopen_with_retry(self.FTP_HOST)
251 finally:
252 socket.setdefaulttimeout(None)
253 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
254
255 def test_ftp_no_timeout(self):
256 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000257 socket.setdefaulttimeout(60)
258 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000259 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000260 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000261 socket.setdefaulttimeout(None)
Neal Norwitz2f142582008-01-26 19:49:41 +0000262 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000263
Georg Brandlf78e02b2008-06-10 17:40:04 +0000264 def test_ftp_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000265 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000266 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000267
Thomas Wouters477c8d52006-05-27 19:21:47 +0000268
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000269def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000270 support.requires("network")
271 support.run_unittest(AuthTests,
Thomas Woutersb2137042007-02-01 18:02:27 +0000272 OtherNetworkTests,
273 CloseSocketTest,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000274 TimeoutTest,
Thomas Woutersb2137042007-02-01 18:02:27 +0000275 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000276
277if __name__ == "__main__":
278 test_main()