blob: 1ebaf8f3c5ce41a2fecf2fc9711055199efd5cdd [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
11import mimetools
12
Neal Norwitz769d0ee2008-01-25 06:37:23 +000013
14def _urlopen_with_retry(host, *args, **kwargs):
15 # Connecting to remote hosts is flaky. Make it more robust
16 # by retrying the connection several times.
17 for i in range(3):
18 try:
19 return urllib2.urlopen(host, *args, **kwargs)
20 except urllib2.URLError, last_exc:
21 continue
22 except:
23 raise
24 raise last_exc
25
26
Georg Brandlfa42bd72006-04-30 07:06:11 +000027
28class AuthTests(unittest.TestCase):
29 """Tests urllib2 authentication features."""
30
31## Disabled at the moment since there is no page under python.org which
32## could be used to HTTP authentication.
33#
34# def test_basic_auth(self):
35# import httplib
36#
37# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
38# test_hostport = "www.python.org"
39# test_realm = 'Test Realm'
40# test_user = 'test.test_urllib2net'
41# test_password = 'blah'
42#
43# # failure
44# try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000045# _urlopen_with_retry(test_url)
Georg Brandlfa42bd72006-04-30 07:06:11 +000046# except urllib2.HTTPError, exc:
47# self.assertEqual(exc.code, 401)
48# else:
49# self.fail("urlopen() should have failed with 401")
50#
51# # success
52# auth_handler = urllib2.HTTPBasicAuthHandler()
53# auth_handler.add_password(test_realm, test_hostport,
54# test_user, test_password)
55# opener = urllib2.build_opener(auth_handler)
56# f = opener.open('http://localhost/')
Neal Norwitz769d0ee2008-01-25 06:37:23 +000057# response = _urlopen_with_retry("http://www.python.org/")
Georg Brandlfa42bd72006-04-30 07:06:11 +000058#
59# # The 'userinfo' URL component is deprecated by RFC 3986 for security
60# # reasons, let's not implement it! (it's already implemented for proxy
61# # specification strings (that is, URLs or authorities specifying a
62# # proxy), so we must keep that)
63# self.assertRaises(httplib.InvalidURL,
64# urllib2.urlopen, "http://evil:thing@example.com")
65
66
Georg Brandldd7b0522007-01-21 10:35:10 +000067class CloseSocketTest(unittest.TestCase):
68
69 def test_close(self):
70 import socket, httplib, gc
71
72 # calling .close() on urllib2's response objects should close the
73 # underlying socket
74
75 # delve deep into response to fetch socket._socketobject
Neal Norwitz769d0ee2008-01-25 06:37:23 +000076 response = _urlopen_with_retry("http://www.python.org/")
Georg Brandldd7b0522007-01-21 10:35:10 +000077 abused_fileobject = response.fp
78 self.assert_(abused_fileobject.__class__ is socket._fileobject)
79 httpresponse = abused_fileobject._sock
80 self.assert_(httpresponse.__class__ is httplib.HTTPResponse)
81 fileobject = httpresponse.fp
82 self.assert_(fileobject.__class__ is socket._fileobject)
83
84 self.assert_(not fileobject.closed)
85 response.close()
86 self.assert_(fileobject.closed)
87
Georg Brandl1b06a1d2006-05-03 05:15:10 +000088class OtherNetworkTests(unittest.TestCase):
89 def setUp(self):
90 if 0: # for debugging
91 import logging
92 logger = logging.getLogger("test_urllib2net")
93 logger.addHandler(logging.StreamHandler())
94
Georg Brandl1b06a1d2006-05-03 05:15:10 +000095 # XXX The rest of these tests aren't very good -- they don't check much.
96 # They do sometimes catch some major disasters, though.
97
98 def test_ftp(self):
99 urls = [
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000100 'ftp://ftp.kernel.org/pub/linux/kernel/README',
101 'ftp://ftp.kernel.org/pub/linux/kernel/non-existant-file',
102 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000103 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
104 '/research-reports/00README-Legal-Rules-Regs',
105 ]
106 self._test_urls(urls, self._extra_handlers())
107
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000108 def test_file(self):
109 TESTFN = test_support.TESTFN
110 f = open(TESTFN, 'w')
111 try:
112 f.write('hi there\n')
113 f.close()
114 urls = [
115 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000116 ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000117 ]
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000118 self._test_urls(urls, self._extra_handlers(), urllib2.urlopen)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000119 finally:
120 os.remove(TESTFN)
121
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000122 # XXX Following test depends on machine configurations that are internal
123 # to CNRI. Need to set up a public server with the right authentication
124 # configuration for test purposes.
125
126## def test_cnri(self):
127## if socket.gethostname() == 'bitdiddle':
128## localhost = 'bitdiddle.cnri.reston.va.us'
129## elif socket.gethostname() == 'bitdiddle.concentric.net':
130## localhost = 'localhost'
131## else:
132## localhost = None
133## if localhost is not None:
134## urls = [
135## 'file://%s/etc/passwd' % localhost,
136## 'http://%s/simple/' % localhost,
137## 'http://%s/digest/' % localhost,
138## 'http://%s/not/found.h' % localhost,
139## ]
140
141## bauth = HTTPBasicAuthHandler()
142## bauth.add_password('basic_test_realm', localhost, 'jhylton',
143## 'password')
144## dauth = HTTPDigestAuthHandler()
145## dauth.add_password('digest_test_realm', localhost, 'jhylton',
146## 'password')
147
148## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
149
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000150 def _test_urls(self, urls, handlers, urlopen=_urlopen_with_retry):
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000151 import socket
152 import time
153 import logging
154 debug = logging.getLogger("test_urllib2").debug
155
156 urllib2.install_opener(urllib2.build_opener(*handlers))
157
158 for url in urls:
159 if isinstance(url, tuple):
160 url, req, expected_err = url
161 else:
162 req = expected_err = None
163 debug(url)
164 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000165 f = urlopen(url, req)
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000166 except EnvironmentError, err:
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000167 debug(err)
168 if expected_err:
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000169 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
170 (expected_err, url, req, type(err), err))
Neal Norwitzf054aeb2006-06-11 20:42:02 +0000171 self.assert_(isinstance(err, expected_err), msg)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000172 else:
Brett Cannonea2835a2007-03-14 21:44:15 +0000173 with test_support.transient_internet():
174 buf = f.read()
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000175 f.close()
176 debug("read %d bytes" % len(buf))
177 debug("******** next url coming up...")
178 time.sleep(0.1)
179
180 def _extra_handlers(self):
181 handlers = []
182
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000183 cfh = urllib2.CacheFTPHandler()
184 cfh.setTimeout(1)
185 handlers.append(cfh)
186
187 return handlers
188
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000189
Facundo Batista10951d52007-06-06 17:15:23 +0000190class TimeoutTest(unittest.TestCase):
191 def test_http_basic(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000192 u = _urlopen_with_retry("http://www.python.org")
Facundo Batista10951d52007-06-06 17:15:23 +0000193 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
194
195 def test_http_NoneWithdefault(self):
196 prev = socket.getdefaulttimeout()
197 socket.setdefaulttimeout(60)
198 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000199 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000200 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 60)
201 finally:
202 socket.setdefaulttimeout(prev)
203
204 def test_http_Value(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000205 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Facundo Batista10951d52007-06-06 17:15:23 +0000206 self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)
207
208 def test_http_NoneNodefault(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000209 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000210 self.assertTrue(u.fp._sock.fp._sock.gettimeout() is None)
211
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000212 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
213
Facundo Batista10951d52007-06-06 17:15:23 +0000214 def test_ftp_basic(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000215 u = _urlopen_with_retry(self.FTP_HOST)
Facundo Batista10951d52007-06-06 17:15:23 +0000216 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
217
218 def test_ftp_NoneWithdefault(self):
219 prev = socket.getdefaulttimeout()
220 socket.setdefaulttimeout(60)
221 try:
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000222 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000223 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
224 finally:
225 socket.setdefaulttimeout(prev)
226
227 def test_ftp_NoneNodefault(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000228 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Facundo Batista10951d52007-06-06 17:15:23 +0000229 self.assertTrue(u.fp.fp._sock.gettimeout() is None)
230
231 def test_ftp_Value(self):
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000232 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Facundo Batista10951d52007-06-06 17:15:23 +0000233 self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
234
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000235
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000236def test_main():
237 test_support.requires("network")
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000238 test_support.run_unittest(AuthTests,
Georg Brandldd7b0522007-01-21 10:35:10 +0000239 OtherNetworkTests,
240 CloseSocketTest,
Facundo Batista10951d52007-06-06 17:15:23 +0000241 TimeoutTest,
Georg Brandldd7b0522007-01-21 10:35:10 +0000242 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000243
244if __name__ == "__main__":
245 test_main()