blob: 1c4af18d973e7d0d5f2885a45df6b3f78bbe496c [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
7import socket
8import urllib2
9import sys
10import os
11import mimetools
12
Christian Heimes969fe572008-01-25 11:23:10 +000013
Georg Brandlc28e1fa2008-06-10 19:20:26 +000014def _retry_thrice(func, exc, *args, **kwargs):
Christian Heimes969fe572008-01-25 11:23:10 +000015 for i in range(3):
16 try:
Georg Brandlc28e1fa2008-06-10 19:20:26 +000017 return func(*args, **kwargs)
18 except exc as e:
Neal Norwitz2f142582008-01-26 19:49:41 +000019 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000020 continue
21 except:
22 raise
23 raise last_exc
24
Georg Brandlc28e1fa2008-06-10 19:20:26 +000025def _wrap_with_retry_thrice(func, exc):
26 def wrapped(*args, **kwargs):
27 return _retry_thrice(func, exc, *args, **kwargs)
28 return wrapped
29
30# Connecting to remote hosts is flaky. Make it more robust by retrying
31# the connection several times.
32_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000033
Thomas Wouters477c8d52006-05-27 19:21:47 +000034
35class AuthTests(unittest.TestCase):
36 """Tests urllib2 authentication features."""
37
38## Disabled at the moment since there is no page under python.org which
39## could be used to HTTP authentication.
40#
41# def test_basic_auth(self):
Georg Brandl24420152008-05-26 16:32:26 +000042# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000043#
44# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
45# test_hostport = "www.python.org"
46# test_realm = 'Test Realm'
47# test_user = 'test.test_urllib2net'
48# test_password = 'blah'
49#
50# # failure
51# try:
Christian Heimes969fe572008-01-25 11:23:10 +000052# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000053# except urllib2.HTTPError, exc:
54# self.assertEqual(exc.code, 401)
55# else:
56# self.fail("urlopen() should have failed with 401")
57#
58# # success
59# auth_handler = urllib2.HTTPBasicAuthHandler()
60# auth_handler.add_password(test_realm, test_hostport,
61# test_user, test_password)
62# opener = urllib2.build_opener(auth_handler)
63# f = opener.open('http://localhost/')
Christian Heimes969fe572008-01-25 11:23:10 +000064# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +000065#
66# # The 'userinfo' URL component is deprecated by RFC 3986 for security
67# # reasons, let's not implement it! (it's already implemented for proxy
68# # specification strings (that is, URLs or authorities specifying a
69# # proxy), so we must keep that)
Georg Brandl24420152008-05-26 16:32:26 +000070# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +000071# urllib2.urlopen, "http://evil:thing@example.com")
72
73
Thomas Woutersb2137042007-02-01 18:02:27 +000074class CloseSocketTest(unittest.TestCase):
75
76 def test_close(self):
Georg Brandl24420152008-05-26 16:32:26 +000077 import socket, http.client, gc
Thomas Woutersb2137042007-02-01 18:02:27 +000078
79 # calling .close() on urllib2's response objects should close the
80 # underlying socket
81
82 # delve deep into response to fetch socket._socketobject
Christian Heimes969fe572008-01-25 11:23:10 +000083 response = _urlopen_with_retry("http://www.python.org/")
Thomas Woutersb2137042007-02-01 18:02:27 +000084 abused_fileobject = response.fp
Jeremy Hyltonec0c5082007-08-03 21:03:02 +000085 httpresponse = abused_fileobject.raw
Georg Brandl24420152008-05-26 16:32:26 +000086 self.assert_(httpresponse.__class__ is http.client.HTTPResponse)
Thomas Woutersb2137042007-02-01 18:02:27 +000087 fileobject = httpresponse.fp
Thomas Woutersb2137042007-02-01 18:02:27 +000088
89 self.assert_(not fileobject.closed)
90 response.close()
91 self.assert_(fileobject.closed)
92
Thomas Wouters477c8d52006-05-27 19:21:47 +000093class OtherNetworkTests(unittest.TestCase):
94 def setUp(self):
95 if 0: # for debugging
96 import logging
97 logger = logging.getLogger("test_urllib2net")
98 logger.addHandler(logging.StreamHandler())
99
Thomas Wouters477c8d52006-05-27 19:21:47 +0000100 # XXX The rest of these tests aren't very good -- they don't check much.
101 # They do sometimes catch some major disasters, though.
102
103 def test_ftp(self):
104 urls = [
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000105 'ftp://ftp.kernel.org/pub/linux/kernel/README',
106 'ftp://ftp.kernel.org/pub/linux/kernel/non-existant-file',
107 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
109 '/research-reports/00README-Legal-Rules-Regs',
110 ]
111 self._test_urls(urls, self._extra_handlers())
112
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000114 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000115 f = open(TESTFN, 'w')
116 try:
117 f.write('hi there\n')
118 f.close()
119 urls = [
120 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000121 ('file:///nonsensename/etc/passwd', None, urllib2.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
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000161 urlopen = urllib2.build_opener(*handlers).open
162 if retry:
163 urlopen = _wrap_with_retry_thrice(urlopen, urllib2.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:
Christian Heimes969fe572008-01-25 11:23:10 +0000172 f = urlopen(url, req)
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))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 self.assert_(isinstance(err, expected_err), msg)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000179 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000180 with support.transient_internet():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000181 buf = f.read()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000182 f.close()
183 debug("read %d bytes" % len(buf))
184 debug("******** next url coming up...")
185 time.sleep(0.1)
186
187 def _extra_handlers(self):
188 handlers = []
189
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190 cfh = urllib2.CacheFTPHandler()
191 cfh.setTimeout(1)
192 handlers.append(cfh)
193
194 return handlers
195
Christian Heimesbbe741d2008-03-28 10:53:29 +0000196
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000197class TimeoutTest(unittest.TestCase):
198 def test_http_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000199 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000200 u = _urlopen_with_retry("http://www.python.org")
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000201 self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000202
Georg Brandlf78e02b2008-06-10 17:40:04 +0000203 def test_http_default_timeout(self):
204 self.assertTrue(socket.getdefaulttimeout() is None)
205 socket.setdefaulttimeout(60)
206 try:
207 u = _urlopen_with_retry("http://www.python.org")
208 finally:
209 socket.setdefaulttimeout(None)
210 self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 60)
211
212 def test_http_no_timeout(self):
213 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000214 socket.setdefaulttimeout(60)
215 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000216 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000217 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000218 socket.setdefaulttimeout(None)
219 self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000220
Georg Brandlf78e02b2008-06-10 17:40:04 +0000221 def test_http_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000222 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000223 self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000224
Christian Heimes969fe572008-01-25 11:23:10 +0000225 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
226
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000227 def test_ftp_basic(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000228 self.assertTrue(socket.getdefaulttimeout() is None)
Christian Heimes969fe572008-01-25 11:23:10 +0000229 u = _urlopen_with_retry(self.FTP_HOST)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000230 self.assertTrue(u.fp.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_ftp_default_timeout(self):
233 self.assertTrue(socket.getdefaulttimeout() is None)
234 socket.setdefaulttimeout(60)
235 try:
236 u = _urlopen_with_retry(self.FTP_HOST)
237 finally:
238 socket.setdefaulttimeout(None)
239 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
240
241 def test_ftp_no_timeout(self):
242 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000243 socket.setdefaulttimeout(60)
244 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000245 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000246 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000247 socket.setdefaulttimeout(None)
Neal Norwitz2f142582008-01-26 19:49:41 +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_timeout(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000251 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000252 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000253
Thomas Wouters477c8d52006-05-27 19:21:47 +0000254
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000255def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000256 support.requires("network")
257 support.run_unittest(AuthTests,
Thomas Woutersb2137042007-02-01 18:02:27 +0000258 OtherNetworkTests,
259 CloseSocketTest,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000260 TimeoutTest,
Thomas Woutersb2137042007-02-01 18:02:27 +0000261 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000262
263if __name__ == "__main__":
264 test_main()