blob: 5415145e0ad91f2950b89c5ee5f47367d1249ca1 [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
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00009import os
Jeremy Hylton5d9c3032004-08-07 17:40:50 +000010
Neal Norwitz769d0ee2008-01-25 06:37:23 +000011
Facundo Batista6a5a1772008-06-07 13:36:36 +000012def _retry_thrice(func, exc, *args, **kwargs):
Neal Norwitz769d0ee2008-01-25 06:37:23 +000013 for i in range(3):
14 try:
Facundo Batista6a5a1772008-06-07 13:36:36 +000015 return func(*args, **kwargs)
16 except exc, last_exc:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000017 continue
18 except:
19 raise
20 raise last_exc
21
Facundo Batista6a5a1772008-06-07 13:36:36 +000022def _wrap_with_retry_thrice(func, exc):
23 def wrapped(*args, **kwargs):
24 return _retry_thrice(func, exc, *args, **kwargs)
25 return wrapped
26
27# Connecting to remote hosts is flaky. Make it more robust by retrying
28# the connection several times.
29_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
Neal Norwitz769d0ee2008-01-25 06:37:23 +000030
Georg Brandlfa42bd72006-04-30 07:06:11 +000031
32class AuthTests(unittest.TestCase):
33 """Tests urllib2 authentication features."""
34
35## Disabled at the moment since there is no page under python.org which
36## could be used to HTTP authentication.
37#
38# def test_basic_auth(self):
39# import httplib
40#
41# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
42# test_hostport = "www.python.org"
43# test_realm = 'Test Realm'
44# test_user = 'test.test_urllib2net'
45# test_password = 'blah'
46#
47# # failure
48# try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000049# _urlopen_with_retry(test_url)
Georg Brandlfa42bd72006-04-30 07:06:11 +000050# except urllib2.HTTPError, exc:
51# self.assertEqual(exc.code, 401)
52# else:
53# self.fail("urlopen() should have failed with 401")
54#
55# # success
56# auth_handler = urllib2.HTTPBasicAuthHandler()
57# auth_handler.add_password(test_realm, test_hostport,
58# test_user, test_password)
59# opener = urllib2.build_opener(auth_handler)
60# f = opener.open('http://localhost/')
Neal Norwitz769d0ee2008-01-25 06:37:23 +000061# response = _urlopen_with_retry("http://www.python.org/")
Georg Brandlfa42bd72006-04-30 07:06:11 +000062#
63# # The 'userinfo' URL component is deprecated by RFC 3986 for security
64# # reasons, let's not implement it! (it's already implemented for proxy
65# # specification strings (that is, URLs or authorities specifying a
66# # proxy), so we must keep that)
67# self.assertRaises(httplib.InvalidURL,
68# urllib2.urlopen, "http://evil:thing@example.com")
69
70
Georg Brandldd7b0522007-01-21 10:35:10 +000071class CloseSocketTest(unittest.TestCase):
72
73 def test_close(self):
Georg Brandla4f46e12010-02-07 17:03:15 +000074 import httplib
Georg Brandldd7b0522007-01-21 10:35:10 +000075
76 # calling .close() on urllib2's response objects should close the
77 # underlying socket
78
79 # delve deep into response to fetch socket._socketobject
Neal Norwitz769d0ee2008-01-25 06:37:23 +000080 response = _urlopen_with_retry("http://www.python.org/")
Georg Brandldd7b0522007-01-21 10:35:10 +000081 abused_fileobject = response.fp
Benjamin Peterson5c8da862009-06-30 22:57:08 +000082 self.assertTrue(abused_fileobject.__class__ is socket._fileobject)
Georg Brandldd7b0522007-01-21 10:35:10 +000083 httpresponse = abused_fileobject._sock
Benjamin Peterson5c8da862009-06-30 22:57:08 +000084 self.assertTrue(httpresponse.__class__ is httplib.HTTPResponse)
Georg Brandldd7b0522007-01-21 10:35:10 +000085 fileobject = httpresponse.fp
Benjamin Peterson5c8da862009-06-30 22:57:08 +000086 self.assertTrue(fileobject.__class__ is socket._fileobject)
Georg Brandldd7b0522007-01-21 10:35:10 +000087
Benjamin Peterson5c8da862009-06-30 22:57:08 +000088 self.assertTrue(not fileobject.closed)
Georg Brandldd7b0522007-01-21 10:35:10 +000089 response.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000090 self.assertTrue(fileobject.closed)
Georg Brandldd7b0522007-01-21 10:35:10 +000091
Georg Brandl1b06a1d2006-05-03 05:15:10 +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
Georg Brandl1b06a1d2006-05-03 05:15:10 +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. Smithe9fef692007-09-09 23:36:46 +0000104 'ftp://ftp.kernel.org/pub/linux/kernel/README',
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000105 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file',
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000106 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Georg Brandl1b06a1d2006-05-03 05:15:10 +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
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000112 def test_file(self):
113 TESTFN = test_support.TESTFN
114 f = open(TESTFN, 'w')
115 try:
116 f.write('hi there\n')
117 f.close()
118 urls = [
119 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000120 ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000121 ]
Facundo Batista6a5a1772008-06-07 13:36:36 +0000122 self._test_urls(urls, self._extra_handlers(), retry=True)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000123 finally:
124 os.remove(TESTFN)
125
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000126 # XXX Following test depends on machine configurations that are internal
127 # to CNRI. Need to set up a public server with the right authentication
128 # configuration for test purposes.
129
130## def test_cnri(self):
131## if socket.gethostname() == 'bitdiddle':
132## localhost = 'bitdiddle.cnri.reston.va.us'
133## elif socket.gethostname() == 'bitdiddle.concentric.net':
134## localhost = 'localhost'
135## else:
136## localhost = None
137## if localhost is not None:
138## urls = [
139## 'file://%s/etc/passwd' % localhost,
140## 'http://%s/simple/' % localhost,
141## 'http://%s/digest/' % localhost,
142## 'http://%s/not/found.h' % localhost,
143## ]
144
145## bauth = HTTPBasicAuthHandler()
146## bauth.add_password('basic_test_realm', localhost, 'jhylton',
147## 'password')
148## dauth = HTTPDigestAuthHandler()
149## dauth.add_password('digest_test_realm', localhost, 'jhylton',
150## 'password')
151
152## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
153
Facundo Batista6a5a1772008-06-07 13:36:36 +0000154 def _test_urls(self, urls, handlers, retry=True):
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000155 import time
156 import logging
157 debug = logging.getLogger("test_urllib2").debug
158
Facundo Batista6a5a1772008-06-07 13:36:36 +0000159 urlopen = urllib2.build_opener(*handlers).open
160 if retry:
161 urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000162
163 for url in urls:
164 if isinstance(url, tuple):
165 url, req, expected_err = url
166 else:
167 req = expected_err = None
168 debug(url)
169 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000170 f = urlopen(url, req)
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000171 except EnvironmentError, err:
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000172 debug(err)
173 if expected_err:
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000174 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
175 (expected_err, url, req, type(err), err))
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000176 self.assertIsInstance(err, expected_err, msg)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000177 else:
Brett Cannonea2835a2007-03-14 21:44:15 +0000178 with test_support.transient_internet():
179 buf = f.read()
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000180 f.close()
181 debug("read %d bytes" % len(buf))
182 debug("******** next url coming up...")
183 time.sleep(0.1)
184
185 def _extra_handlers(self):
186 handlers = []
187
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000188 cfh = urllib2.CacheFTPHandler()
189 cfh.setTimeout(1)
190 handlers.append(cfh)
191
192 return handlers
193
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000194
Facundo Batista10951d52007-06-06 17:15:23 +0000195class TimeoutTest(unittest.TestCase):
196 def test_http_basic(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000197 self.assertTrue(socket.getdefaulttimeout() is None)
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000198 u = _urlopen_with_retry("http://www.python.org")
Facundo Batista10951d52007-06-06 17:15:23 +0000199 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
200
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000201 def test_http_default_timeout(self):
202 self.assertTrue(socket.getdefaulttimeout() is None)
203 socket.setdefaulttimeout(60)
204 try:
205 u = _urlopen_with_retry("http://www.python.org")
206 finally:
207 socket.setdefaulttimeout(None)
208 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 60)
209
210 def test_http_no_timeout(self):
211 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000212 socket.setdefaulttimeout(60)
213 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000214 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000215 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000216 socket.setdefaulttimeout(None)
217 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000218
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000219 def test_http_timeout(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000220 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Facundo Batista10951d52007-06-06 17:15:23 +0000221 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)
222
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000223 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
224
Facundo Batista10951d52007-06-06 17:15:23 +0000225 def test_ftp_basic(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000226 self.assertTrue(socket.getdefaulttimeout() is None)
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000227 u = _urlopen_with_retry(self.FTP_HOST)
Facundo Batista10951d52007-06-06 17:15:23 +0000228 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
229
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000230 def test_ftp_default_timeout(self):
231 self.assertTrue(socket.getdefaulttimeout() is None)
232 socket.setdefaulttimeout(60)
233 try:
234 u = _urlopen_with_retry(self.FTP_HOST)
235 finally:
236 socket.setdefaulttimeout(None)
237 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
238
239 def test_ftp_no_timeout(self):
240 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista10951d52007-06-06 17:15:23 +0000241 socket.setdefaulttimeout(60)
242 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000243 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000244 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000245 socket.setdefaulttimeout(None)
Facundo Batista10951d52007-06-06 17:15:23 +0000246 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
247
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000248 def test_ftp_timeout(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000249 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Facundo Batista10951d52007-06-06 17:15:23 +0000250 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
251
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000252
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000253def test_main():
254 test_support.requires("network")
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000255 test_support.run_unittest(AuthTests,
Georg Brandldd7b0522007-01-21 10:35:10 +0000256 OtherNetworkTests,
257 CloseSocketTest,
Facundo Batista10951d52007-06-06 17:15:23 +0000258 TimeoutTest,
Georg Brandldd7b0522007-01-21 10:35:10 +0000259 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000260
261if __name__ == "__main__":
262 test_main()