blob: eab9306ab96c29292607463e1b1404daf7f9a23a [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
Senthil Kumaran4c88db72010-08-08 11:30:58 +0000155 def test_urlwithfrag(self):
156 urlwith_frag = "http://docs.python.org/glossary.html#glossary"
157 req = urllib.request.Request(urlwith_frag)
158 res = urllib.request.urlopen(req)
159 self.assertEqual(res.geturl(),
160 "http://docs.python.org/glossary.html")
161
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000162 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163 import socket
164 import time
165 import logging
166 debug = logging.getLogger("test_urllib2").debug
167
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000168 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000169 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000170 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000171
172 for url in urls:
173 if isinstance(url, tuple):
174 url, req, expected_err = url
175 else:
176 req = expected_err = None
177 debug(url)
178 try:
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000179 f = urlopen(url, req, TIMEOUT)
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000180 except EnvironmentError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181 debug(err)
182 if expected_err:
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000183 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
184 (expected_err, url, req, type(err), err))
Georg Brandlab91fde2009-08-13 08:51:18 +0000185 self.assertTrue(isinstance(err, expected_err), msg)
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000186 except urllib.error.URLError as err:
187 if isinstance(err[0], socket.timeout):
188 print("<timeout: %s>" % url, file=sys.stderr)
189 continue
190 else:
191 raise
Thomas Wouters477c8d52006-05-27 19:21:47 +0000192 else:
Senthil Kumarandcedfb12010-04-20 10:40:21 +0000193 try:
194 with support.time_out, \
195 support.socket_peer_reset, \
196 support.ioerror_peer_reset:
197 buf = f.read()
198 debug("read %d bytes" % len(buf))
199 except socket.timeout:
200 print("<timeout: %s>" % url, file=sys.stderr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201 f.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000202 debug("******** next url coming up...")
203 time.sleep(0.1)
204
205 def _extra_handlers(self):
206 handlers = []
207
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000208 cfh = urllib.request.CacheFTPHandler()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209 cfh.setTimeout(1)
210 handlers.append(cfh)
211
212 return handlers
213
Christian Heimesbbe741d2008-03-28 10:53:29 +0000214
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000215class TimeoutTest(unittest.TestCase):
216 def test_http_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000217 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000218 u = _urlopen_with_retry("http://www.python.org")
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000219 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000220
Georg Brandlf78e02b2008-06-10 17:40:04 +0000221 def test_http_default_timeout(self):
222 self.assertTrue(socket.getdefaulttimeout() is None)
223 socket.setdefaulttimeout(60)
224 try:
225 u = _urlopen_with_retry("http://www.python.org")
226 finally:
227 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000228 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000229
230 def test_http_no_timeout(self):
231 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000232 socket.setdefaulttimeout(60)
233 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000234 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000235 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000236 socket.setdefaulttimeout(None)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000237 self.assertTrue(u.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000238
Georg Brandlf78e02b2008-06-10 17:40:04 +0000239 def test_http_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000240 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Benjamin Peterson4376dbc2009-04-03 23:57:05 +0000241 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000242
Christian Heimes969fe572008-01-25 11:23:10 +0000243 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
244
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000245 def test_ftp_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000246 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000247 u = _urlopen_with_retry(self.FTP_HOST)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000248 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000249
Georg Brandlf78e02b2008-06-10 17:40:04 +0000250 def test_ftp_default_timeout(self):
251 self.assertTrue(socket.getdefaulttimeout() is None)
252 socket.setdefaulttimeout(60)
253 try:
254 u = _urlopen_with_retry(self.FTP_HOST)
255 finally:
256 socket.setdefaulttimeout(None)
257 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
258
259 def test_ftp_no_timeout(self):
260 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000261 socket.setdefaulttimeout(60)
262 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000263 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000264 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000265 socket.setdefaulttimeout(None)
Neal Norwitz2f142582008-01-26 19:49:41 +0000266 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000267
Georg Brandlf78e02b2008-06-10 17:40:04 +0000268 def test_ftp_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000269 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000270 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000271
Thomas Wouters477c8d52006-05-27 19:21:47 +0000272
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000273def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000274 support.requires("network")
275 support.run_unittest(AuthTests,
Thomas Woutersb2137042007-02-01 18:02:27 +0000276 OtherNetworkTests,
277 CloseSocketTest,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000278 TimeoutTest,
Thomas Woutersb2137042007-02-01 18:02:27 +0000279 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000280
281if __name__ == "__main__":
282 test_main()