blob: 162b8164879e65a6c17103fe77738a0b997c6e5c [file] [log] [blame]
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00001#!/usr/bin/env python
2
3import unittest
4from test import test_support
Georg Brandl1b06a1d2006-05-03 05:15:10 +00005from test.test_urllib2 import sanepathname2url
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00006
7import socket
8import urllib2
9import sys
10import os
Senthil Kumaranc00deec2010-04-20 07:06:45 +000011import sys
12
13TIMEOUT = 60 # seconds
Jeremy Hylton5d9c3032004-08-07 17:40:50 +000014
Neal Norwitz769d0ee2008-01-25 06:37:23 +000015
Facundo Batista6a5a1772008-06-07 13:36:36 +000016def _retry_thrice(func, exc, *args, **kwargs):
Neal Norwitz769d0ee2008-01-25 06:37:23 +000017 for i in range(3):
18 try:
Facundo Batista6a5a1772008-06-07 13:36:36 +000019 return func(*args, **kwargs)
20 except exc, last_exc:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000021 continue
22 except:
23 raise
24 raise last_exc
25
Facundo Batista6a5a1772008-06-07 13:36:36 +000026def _wrap_with_retry_thrice(func, exc):
27 def wrapped(*args, **kwargs):
28 return _retry_thrice(func, exc, *args, **kwargs)
29 return wrapped
30
31# Connecting to remote hosts is flaky. Make it more robust by retrying
32# the connection several times.
33_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
Neal Norwitz769d0ee2008-01-25 06:37:23 +000034
Georg Brandlfa42bd72006-04-30 07:06:11 +000035
36class AuthTests(unittest.TestCase):
37 """Tests urllib2 authentication features."""
38
39## Disabled at the moment since there is no page under python.org which
40## could be used to HTTP authentication.
41#
42# def test_basic_auth(self):
43# import httplib
44#
45# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
46# test_hostport = "www.python.org"
47# test_realm = 'Test Realm'
48# test_user = 'test.test_urllib2net'
49# test_password = 'blah'
50#
51# # failure
52# try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000053# _urlopen_with_retry(test_url)
Georg Brandlfa42bd72006-04-30 07:06:11 +000054# except urllib2.HTTPError, exc:
55# self.assertEqual(exc.code, 401)
56# else:
57# self.fail("urlopen() should have failed with 401")
58#
59# # success
60# auth_handler = urllib2.HTTPBasicAuthHandler()
61# auth_handler.add_password(test_realm, test_hostport,
62# test_user, test_password)
63# opener = urllib2.build_opener(auth_handler)
64# f = opener.open('http://localhost/')
Neal Norwitz769d0ee2008-01-25 06:37:23 +000065# response = _urlopen_with_retry("http://www.python.org/")
Georg Brandlfa42bd72006-04-30 07:06:11 +000066#
67# # The 'userinfo' URL component is deprecated by RFC 3986 for security
68# # reasons, let's not implement it! (it's already implemented for proxy
69# # specification strings (that is, URLs or authorities specifying a
70# # proxy), so we must keep that)
71# self.assertRaises(httplib.InvalidURL,
72# urllib2.urlopen, "http://evil:thing@example.com")
73
74
Georg Brandldd7b0522007-01-21 10:35:10 +000075class CloseSocketTest(unittest.TestCase):
76
77 def test_close(self):
78 import socket, httplib, gc
79
80 # calling .close() on urllib2's response objects should close the
81 # underlying socket
82
83 # delve deep into response to fetch socket._socketobject
Neal Norwitz769d0ee2008-01-25 06:37:23 +000084 response = _urlopen_with_retry("http://www.python.org/")
Georg Brandldd7b0522007-01-21 10:35:10 +000085 abused_fileobject = response.fp
86 self.assert_(abused_fileobject.__class__ is socket._fileobject)
87 httpresponse = abused_fileobject._sock
88 self.assert_(httpresponse.__class__ is httplib.HTTPResponse)
89 fileobject = httpresponse.fp
90 self.assert_(fileobject.__class__ is socket._fileobject)
91
92 self.assert_(not fileobject.closed)
93 response.close()
94 self.assert_(fileobject.closed)
95
Georg Brandl1b06a1d2006-05-03 05:15:10 +000096class OtherNetworkTests(unittest.TestCase):
97 def setUp(self):
98 if 0: # for debugging
99 import logging
100 logger = logging.getLogger("test_urllib2net")
101 logger.addHandler(logging.StreamHandler())
102
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000103 # XXX The rest of these tests aren't very good -- they don't check much.
104 # They do sometimes catch some major disasters, though.
105
106 def test_ftp(self):
107 urls = [
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000108 'ftp://ftp.kernel.org/pub/linux/kernel/README',
Jesus Cea585ad8a2009-07-02 15:37:21 +0000109 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file',
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000110 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000111 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
112 '/research-reports/00README-Legal-Rules-Regs',
113 ]
114 self._test_urls(urls, self._extra_handlers())
115
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000116 def test_file(self):
117 TESTFN = test_support.TESTFN
118 f = open(TESTFN, 'w')
119 try:
120 f.write('hi there\n')
121 f.close()
122 urls = [
123 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000124 ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000125 ]
Facundo Batista6a5a1772008-06-07 13:36:36 +0000126 self._test_urls(urls, self._extra_handlers(), retry=True)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000127 finally:
128 os.remove(TESTFN)
129
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000130 # XXX Following test depends on machine configurations that are internal
131 # to CNRI. Need to set up a public server with the right authentication
132 # configuration for test purposes.
133
134## def test_cnri(self):
135## if socket.gethostname() == 'bitdiddle':
136## localhost = 'bitdiddle.cnri.reston.va.us'
137## elif socket.gethostname() == 'bitdiddle.concentric.net':
138## localhost = 'localhost'
139## else:
140## localhost = None
141## if localhost is not None:
142## urls = [
143## 'file://%s/etc/passwd' % localhost,
144## 'http://%s/simple/' % localhost,
145## 'http://%s/digest/' % localhost,
146## 'http://%s/not/found.h' % localhost,
147## ]
148
149## bauth = HTTPBasicAuthHandler()
150## bauth.add_password('basic_test_realm', localhost, 'jhylton',
151## 'password')
152## dauth = HTTPDigestAuthHandler()
153## dauth.add_password('digest_test_realm', localhost, 'jhylton',
154## 'password')
155
156## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
157
Facundo Batista6a5a1772008-06-07 13:36:36 +0000158 def _test_urls(self, urls, handlers, retry=True):
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000159 import socket
160 import time
161 import logging
162 debug = logging.getLogger("test_urllib2").debug
163
Facundo Batista6a5a1772008-06-07 13:36:36 +0000164 urlopen = urllib2.build_opener(*handlers).open
165 if retry:
166 urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError)
Georg Brandl1b06a1d2006-05-03 05:15:10 +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 Kumaranc00deec2010-04-20 07:06:45 +0000175 f = urlopen(url, req, TIMEOUT)
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000176 except EnvironmentError, err:
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000177 debug(err)
178 if expected_err:
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000179 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
180 (expected_err, url, req, type(err), err))
Neal Norwitzf054aeb2006-06-11 20:42:02 +0000181 self.assert_(isinstance(err, expected_err), msg)
Senthil Kumaranc00deec2010-04-20 07:06:45 +0000182 except urllib2.URLError as err:
183 if isinstance(err[0], socket.timeout):
184 print >>sys.stderr, "<timeout: %s>" % url
185 continue
186 else:
187 raise
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000188 else:
Senthil Kumaranc00deec2010-04-20 07:06:45 +0000189 try:
190 with test_support.transient_internet():
191 buf = f.read()
192 debug("read %d bytes" % len(buf))
193 except socket.timeout:
194 print >>sys.stderr, "<timeout: %s>" % url
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000195 f.close()
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000196 debug("******** next url coming up...")
197 time.sleep(0.1)
198
199 def _extra_handlers(self):
200 handlers = []
201
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000202 cfh = urllib2.CacheFTPHandler()
203 cfh.setTimeout(1)
204 handlers.append(cfh)
205
206 return handlers
207
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000208
Facundo Batista10951d52007-06-06 17:15:23 +0000209class TimeoutTest(unittest.TestCase):
210 def test_http_basic(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000211 self.assertTrue(socket.getdefaulttimeout() is None)
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000212 u = _urlopen_with_retry("http://www.python.org")
Facundo Batista10951d52007-06-06 17:15:23 +0000213 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
214
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000215 def test_http_default_timeout(self):
216 self.assertTrue(socket.getdefaulttimeout() is None)
217 socket.setdefaulttimeout(60)
218 try:
219 u = _urlopen_with_retry("http://www.python.org")
220 finally:
221 socket.setdefaulttimeout(None)
222 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 60)
223
224 def test_http_no_timeout(self):
225 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000226 socket.setdefaulttimeout(60)
227 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000228 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000229 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000230 socket.setdefaulttimeout(None)
231 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000232
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000233 def test_http_timeout(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000234 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Facundo Batista10951d52007-06-06 17:15:23 +0000235 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)
236
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000237 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
238
Facundo Batista10951d52007-06-06 17:15:23 +0000239 def test_ftp_basic(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000240 self.assertTrue(socket.getdefaulttimeout() is None)
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000241 u = _urlopen_with_retry(self.FTP_HOST)
Facundo Batista10951d52007-06-06 17:15:23 +0000242 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
243
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000244 def test_ftp_default_timeout(self):
245 self.assertTrue(socket.getdefaulttimeout() is None)
246 socket.setdefaulttimeout(60)
247 try:
248 u = _urlopen_with_retry(self.FTP_HOST)
249 finally:
250 socket.setdefaulttimeout(None)
251 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
252
253 def test_ftp_no_timeout(self):
254 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000255 socket.setdefaulttimeout(60)
256 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000257 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000258 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000259 socket.setdefaulttimeout(None)
Facundo Batista10951d52007-06-06 17:15:23 +0000260 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
261
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000262 def test_ftp_timeout(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000263 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Facundo Batista10951d52007-06-06 17:15:23 +0000264 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
265
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000266
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000267def test_main():
268 test_support.requires("network")
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000269 test_support.run_unittest(AuthTests,
Georg Brandldd7b0522007-01-21 10:35:10 +0000270 OtherNetworkTests,
271 CloseSocketTest,
Facundo Batista10951d52007-06-06 17:15:23 +0000272 TimeoutTest,
Georg Brandldd7b0522007-01-21 10:35:10 +0000273 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000274
275if __name__ == "__main__":
276 test_main()