blob: 0f109dc22acf1440d23a972d760b5ff6a5d658b3 [file] [log] [blame]
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00001#!/usr/bin/env python
2
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
9import sys
10import urllib.error
11import urllib.request
Senthil Kumarandcedfb12010-04-20 10:40:21 +000012import sys
13
14TIMEOUT = 60 # seconds
Jeremy Hylton5d9c3032004-08-07 17:40:50 +000015
Christian Heimes969fe572008-01-25 11:23:10 +000016
Georg Brandlc28e1fa2008-06-10 19:20:26 +000017def _retry_thrice(func, exc, *args, **kwargs):
Christian Heimes969fe572008-01-25 11:23:10 +000018 for i in range(3):
19 try:
Georg Brandlc28e1fa2008-06-10 19:20:26 +000020 return func(*args, **kwargs)
21 except exc as e:
Neal Norwitz2f142582008-01-26 19:49:41 +000022 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000023 continue
24 except:
25 raise
26 raise last_exc
27
Georg Brandlc28e1fa2008-06-10 19:20:26 +000028def _wrap_with_retry_thrice(func, exc):
29 def wrapped(*args, **kwargs):
30 return _retry_thrice(func, exc, *args, **kwargs)
31 return wrapped
32
33# Connecting to remote hosts is flaky. Make it more robust by retrying
34# the connection several times.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000035_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
36 urllib.error.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000037
Thomas Wouters477c8d52006-05-27 19:21:47 +000038
39class AuthTests(unittest.TestCase):
40 """Tests urllib2 authentication features."""
41
42## Disabled at the moment since there is no page under python.org which
43## could be used to HTTP authentication.
44#
45# def test_basic_auth(self):
Georg Brandl24420152008-05-26 16:32:26 +000046# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000047#
48# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
49# test_hostport = "www.python.org"
50# test_realm = 'Test Realm'
51# test_user = 'test.test_urllib2net'
52# test_password = 'blah'
53#
54# # failure
55# try:
Christian Heimes969fe572008-01-25 11:23:10 +000056# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000057# except urllib2.HTTPError, exc:
58# self.assertEqual(exc.code, 401)
59# else:
60# self.fail("urlopen() should have failed with 401")
61#
62# # success
63# auth_handler = urllib2.HTTPBasicAuthHandler()
64# auth_handler.add_password(test_realm, test_hostport,
65# test_user, test_password)
66# opener = urllib2.build_opener(auth_handler)
67# f = opener.open('http://localhost/')
Christian Heimes969fe572008-01-25 11:23:10 +000068# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +000069#
70# # The 'userinfo' URL component is deprecated by RFC 3986 for security
71# # reasons, let's not implement it! (it's already implemented for proxy
72# # specification strings (that is, URLs or authorities specifying a
73# # proxy), so we must keep that)
Georg Brandl24420152008-05-26 16:32:26 +000074# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +000075# urllib2.urlopen, "http://evil:thing@example.com")
76
77
Thomas Woutersb2137042007-02-01 18:02:27 +000078class CloseSocketTest(unittest.TestCase):
79
80 def test_close(self):
Georg Brandl24420152008-05-26 16:32:26 +000081 import socket, http.client, gc
Thomas Woutersb2137042007-02-01 18:02:27 +000082
83 # calling .close() on urllib2's response objects should close the
84 # underlying socket
85
Christian Heimes969fe572008-01-25 11:23:10 +000086 response = _urlopen_with_retry("http://www.python.org/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +000087 sock = response.fp
Georg Brandlab91fde2009-08-13 08:51:18 +000088 self.assertTrue(not sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +000089 response.close()
Georg Brandlab91fde2009-08-13 08:51:18 +000090 self.assertTrue(sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +000091
Thomas Wouters477c8d52006-05-27 19:21:47 +000092class OtherNetworkTests(unittest.TestCase):
93 def setUp(self):
94 if 0: # for debugging
95 import logging
96 logger = logging.getLogger("test_urllib2net")
97 logger.addHandler(logging.StreamHandler())
98
Thomas Wouters477c8d52006-05-27 19:21:47 +000099 # XXX The rest of these tests aren't very good -- they don't check much.
100 # They do sometimes catch some major disasters, though.
101
102 def test_ftp(self):
103 urls = [
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000104 'ftp://ftp.kernel.org/pub/linux/kernel/README',
Mark Dickinson934896d2009-02-21 20:59:32 +0000105 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file',
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000106 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
108 '/research-reports/00README-Legal-Rules-Regs',
109 ]
110 self._test_urls(urls, self._extra_handlers())
111
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000113 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114 f = open(TESTFN, 'w')
115 try:
116 f.write('hi there\n')
117 f.close()
118 urls = [
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000119 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
120 ('file:///nonsensename/etc/passwd', None,
121 urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122 ]
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000123 self._test_urls(urls, self._extra_handlers(), retry=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 finally:
125 os.remove(TESTFN)
126
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127 # XXX Following test depends on machine configurations that are internal
128 # to CNRI. Need to set up a public server with the right authentication
129 # configuration for test purposes.
130
131## def test_cnri(self):
132## if socket.gethostname() == 'bitdiddle':
133## localhost = 'bitdiddle.cnri.reston.va.us'
134## elif socket.gethostname() == 'bitdiddle.concentric.net':
135## localhost = 'localhost'
136## else:
137## localhost = None
138## if localhost is not None:
139## urls = [
140## 'file://%s/etc/passwd' % localhost,
141## 'http://%s/simple/' % localhost,
142## 'http://%s/digest/' % localhost,
143## 'http://%s/not/found.h' % localhost,
144## ]
145
146## bauth = HTTPBasicAuthHandler()
147## bauth.add_password('basic_test_realm', localhost, 'jhylton',
148## 'password')
149## dauth = HTTPDigestAuthHandler()
150## dauth.add_password('digest_test_realm', localhost, 'jhylton',
151## 'password')
152
153## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
154
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000155 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156 import socket
157 import time
158 import logging
159 debug = logging.getLogger("test_urllib2").debug
160
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000161 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000162 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000163 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164
165 for url in urls:
166 if isinstance(url, tuple):
167 url, req, expected_err = url
168 else:
169 req = expected_err = None
170 debug(url)
171 try:
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000172 f = urlopen(url, req, TIMEOUT)
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000173 except EnvironmentError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 debug(err)
175 if expected_err:
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000176 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
177 (expected_err, url, req, type(err), err))
Georg Brandlab91fde2009-08-13 08:51:18 +0000178 self.assertTrue(isinstance(err, expected_err), msg)
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000179 except urllib.error.URLError as err:
180 if isinstance(err[0], socket.timeout):
181 print("<timeout: %s>" % url, file=sys.stderr)
182 continue
183 else:
184 raise
Thomas Wouters477c8d52006-05-27 19:21:47 +0000185 else:
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000186 try:
187 with support.time_out, \
188 support.socket_peer_reset, \
189 support.ioerror_peer_reset:
190 buf = f.read()
191 debug("read %d bytes" % len(buf))
192 except socket.timeout:
193 print("<timeout: %s>" % url, file=sys.stderr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 f.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195 debug("******** next url coming up...")
196 time.sleep(0.1)
197
198 def _extra_handlers(self):
199 handlers = []
200
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000201 cfh = urllib.request.CacheFTPHandler()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000202 cfh.setTimeout(1)
203 handlers.append(cfh)
204
205 return handlers
206
Christian Heimesbbe741d2008-03-28 10:53:29 +0000207
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000208class TimeoutTest(unittest.TestCase):
209 def test_http_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000210 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000211 u = _urlopen_with_retry("http://www.python.org")
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000212 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000213
Georg Brandlf78e02b2008-06-10 17:40:04 +0000214 def test_http_default_timeout(self):
215 self.assertTrue(socket.getdefaulttimeout() is None)
216 socket.setdefaulttimeout(60)
217 try:
218 u = _urlopen_with_retry("http://www.python.org")
219 finally:
220 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000221 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000222
223 def test_http_no_timeout(self):
224 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000225 socket.setdefaulttimeout(60)
226 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000227 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000228 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000229 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000230 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000231
Georg Brandlf78e02b2008-06-10 17:40:04 +0000232 def test_http_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000233 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000234 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000235
Christian Heimes969fe572008-01-25 11:23:10 +0000236 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
237
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000238 def test_ftp_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000239 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000240 u = _urlopen_with_retry(self.FTP_HOST)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000241 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000242
Georg Brandlf78e02b2008-06-10 17:40:04 +0000243 def test_ftp_default_timeout(self):
244 self.assertTrue(socket.getdefaulttimeout() is None)
245 socket.setdefaulttimeout(60)
246 try:
247 u = _urlopen_with_retry(self.FTP_HOST)
248 finally:
249 socket.setdefaulttimeout(None)
250 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
251
252 def test_ftp_no_timeout(self):
253 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000254 socket.setdefaulttimeout(60)
255 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000256 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000257 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000258 socket.setdefaulttimeout(None)
Neal Norwitz2f142582008-01-26 19:49:41 +0000259 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000260
Georg Brandlf78e02b2008-06-10 17:40:04 +0000261 def test_ftp_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000262 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000263 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000264
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000266def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000267 support.requires("network")
268 support.run_unittest(AuthTests,
Thomas Woutersb2137042007-02-01 18:02:27 +0000269 OtherNetworkTests,
270 CloseSocketTest,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000271 TimeoutTest,
Thomas Woutersb2137042007-02-01 18:02:27 +0000272 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000273
274if __name__ == "__main__":
275 test_main()