blob: 990d3da536325bafed3012a2437c01a18f5f1664 [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
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)
Neal Norwitz2f142582008-01-26 19:49:41 +000020 except urllib2.URLError as e:
21 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000022 continue
23 except:
24 raise
25 raise last_exc
26
27
Thomas Wouters477c8d52006-05-27 19:21:47 +000028
29class AuthTests(unittest.TestCase):
30 """Tests urllib2 authentication features."""
31
32## Disabled at the moment since there is no page under python.org which
33## could be used to HTTP authentication.
34#
35# def test_basic_auth(self):
Georg Brandl24420152008-05-26 16:32:26 +000036# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000037#
38# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
39# test_hostport = "www.python.org"
40# test_realm = 'Test Realm'
41# test_user = 'test.test_urllib2net'
42# test_password = 'blah'
43#
44# # failure
45# try:
Christian Heimes969fe572008-01-25 11:23:10 +000046# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000047# except urllib2.HTTPError, exc:
48# self.assertEqual(exc.code, 401)
49# else:
50# self.fail("urlopen() should have failed with 401")
51#
52# # success
53# auth_handler = urllib2.HTTPBasicAuthHandler()
54# auth_handler.add_password(test_realm, test_hostport,
55# test_user, test_password)
56# opener = urllib2.build_opener(auth_handler)
57# f = opener.open('http://localhost/')
Christian Heimes969fe572008-01-25 11:23:10 +000058# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +000059#
60# # The 'userinfo' URL component is deprecated by RFC 3986 for security
61# # reasons, let's not implement it! (it's already implemented for proxy
62# # specification strings (that is, URLs or authorities specifying a
63# # proxy), so we must keep that)
Georg Brandl24420152008-05-26 16:32:26 +000064# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +000065# urllib2.urlopen, "http://evil:thing@example.com")
66
67
Thomas Woutersb2137042007-02-01 18:02:27 +000068class CloseSocketTest(unittest.TestCase):
69
70 def test_close(self):
Georg Brandl24420152008-05-26 16:32:26 +000071 import socket, http.client, gc
Thomas Woutersb2137042007-02-01 18:02:27 +000072
73 # calling .close() on urllib2's response objects should close the
74 # underlying socket
75
76 # delve deep into response to fetch socket._socketobject
Christian Heimes969fe572008-01-25 11:23:10 +000077 response = _urlopen_with_retry("http://www.python.org/")
Thomas Woutersb2137042007-02-01 18:02:27 +000078 abused_fileobject = response.fp
Jeremy Hyltonec0c5082007-08-03 21:03:02 +000079 httpresponse = abused_fileobject.raw
Georg Brandl24420152008-05-26 16:32:26 +000080 self.assert_(httpresponse.__class__ is http.client.HTTPResponse)
Thomas Woutersb2137042007-02-01 18:02:27 +000081 fileobject = httpresponse.fp
Thomas Woutersb2137042007-02-01 18:02:27 +000082
83 self.assert_(not fileobject.closed)
84 response.close()
85 self.assert_(fileobject.closed)
86
Thomas Wouters477c8d52006-05-27 19:21:47 +000087class OtherNetworkTests(unittest.TestCase):
88 def setUp(self):
89 if 0: # for debugging
90 import logging
91 logger = logging.getLogger("test_urllib2net")
92 logger.addHandler(logging.StreamHandler())
93
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 # XXX The rest of these tests aren't very good -- they don't check much.
95 # They do sometimes catch some major disasters, though.
96
97 def test_ftp(self):
98 urls = [
Gregory P. Smithc111d9f2007-09-09 23:55:55 +000099 'ftp://ftp.kernel.org/pub/linux/kernel/README',
100 'ftp://ftp.kernel.org/pub/linux/kernel/non-existant-file',
101 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
103 '/research-reports/00README-Legal-Rules-Regs',
104 ]
105 self._test_urls(urls, self._extra_handlers())
106
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107 def test_file(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000108 TESTFN = support.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 f = open(TESTFN, 'w')
110 try:
111 f.write('hi there\n')
112 f.close()
113 urls = [
114 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000115 ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116 ]
Christian Heimes969fe572008-01-25 11:23:10 +0000117 self._test_urls(urls, self._extra_handlers(), urllib2.urlopen)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118 finally:
119 os.remove(TESTFN)
120
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121 # XXX Following test depends on machine configurations that are internal
122 # to CNRI. Need to set up a public server with the right authentication
123 # configuration for test purposes.
124
125## def test_cnri(self):
126## if socket.gethostname() == 'bitdiddle':
127## localhost = 'bitdiddle.cnri.reston.va.us'
128## elif socket.gethostname() == 'bitdiddle.concentric.net':
129## localhost = 'localhost'
130## else:
131## localhost = None
132## if localhost is not None:
133## urls = [
134## 'file://%s/etc/passwd' % localhost,
135## 'http://%s/simple/' % localhost,
136## 'http://%s/digest/' % localhost,
137## 'http://%s/not/found.h' % localhost,
138## ]
139
140## bauth = HTTPBasicAuthHandler()
141## bauth.add_password('basic_test_realm', localhost, 'jhylton',
142## 'password')
143## dauth = HTTPDigestAuthHandler()
144## dauth.add_password('digest_test_realm', localhost, 'jhylton',
145## 'password')
146
147## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
148
Christian Heimes969fe572008-01-25 11:23:10 +0000149 def _test_urls(self, urls, handlers, urlopen=_urlopen_with_retry):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150 import socket
151 import time
152 import logging
153 debug = logging.getLogger("test_urllib2").debug
154
155 urllib2.install_opener(urllib2.build_opener(*handlers))
156
157 for url in urls:
158 if isinstance(url, tuple):
159 url, req, expected_err = url
160 else:
161 req = expected_err = None
162 debug(url)
163 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000164 f = urlopen(url, req)
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000165 except EnvironmentError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000166 debug(err)
167 if expected_err:
Gregory P. Smithc111d9f2007-09-09 23:55:55 +0000168 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
169 (expected_err, url, req, type(err), err))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000170 self.assert_(isinstance(err, expected_err), msg)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000171 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000172 with support.transient_internet():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000173 buf = f.read()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 f.close()
175 debug("read %d bytes" % len(buf))
176 debug("******** next url coming up...")
177 time.sleep(0.1)
178
179 def _extra_handlers(self):
180 handlers = []
181
Thomas Wouters477c8d52006-05-27 19:21:47 +0000182 cfh = urllib2.CacheFTPHandler()
183 cfh.setTimeout(1)
184 handlers.append(cfh)
185
186 return handlers
187
Christian Heimesbbe741d2008-03-28 10:53:29 +0000188
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000189class TimeoutTest(unittest.TestCase):
190 def test_http_basic(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000191 u = _urlopen_with_retry("http://www.python.org")
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000192 self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000193
194 def test_http_NoneWithdefault(self):
195 prev = socket.getdefaulttimeout()
196 socket.setdefaulttimeout(60)
197 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000198 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000199 self.assertTrue(u.fp.raw.fp._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000200 finally:
201 socket.setdefaulttimeout(prev)
202
203 def test_http_Value(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000204 u = _urlopen_with_retry("http://www.python.org", timeout=120)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000205 self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000206
207 def test_http_NoneNodefault(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000208 u = _urlopen_with_retry("http://www.python.org", timeout=None)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000209 self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000210
Christian Heimes969fe572008-01-25 11:23:10 +0000211 FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
212
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000213 def test_ftp_basic(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000214 u = _urlopen_with_retry(self.FTP_HOST)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000215 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000216
217 def test_ftp_NoneWithdefault(self):
218 prev = socket.getdefaulttimeout()
219 socket.setdefaulttimeout(60)
220 try:
Christian Heimes969fe572008-01-25 11:23:10 +0000221 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Neal Norwitz2f142582008-01-26 19:49:41 +0000222 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000223 finally:
224 socket.setdefaulttimeout(prev)
225
226 def test_ftp_NoneNodefault(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000227 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Neal Norwitz2f142582008-01-26 19:49:41 +0000228 self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000229
230 def test_ftp_Value(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000231 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Jeremy Hyltoncf2f4192007-08-03 20:31:38 +0000232 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000233
Thomas Wouters477c8d52006-05-27 19:21:47 +0000234
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000235def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000236 support.requires("network")
237 support.run_unittest(AuthTests,
Thomas Woutersb2137042007-02-01 18:02:27 +0000238 OtherNetworkTests,
239 CloseSocketTest,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000240 TimeoutTest,
Thomas Woutersb2137042007-02-01 18:02:27 +0000241 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000242
243if __name__ == "__main__":
244 test_main()