Try to fix several networking tests. The problem is that if hosts have
[python.git] / Lib / test / test_urllib2net.py
blob8b13096c2b9504ed72ab252ab45c0c00fb16e295
1 #!/usr/bin/env python
3 import unittest
4 from test import test_support
5 from test.test_urllib2 import sanepathname2url
7 import socket
8 import urllib2
9 import sys
10 import os
11 import mimetools
13 class URLTimeoutTest(unittest.TestCase):
15 TIMEOUT = 10.0
17 def setUp(self):
18 socket.setdefaulttimeout(self.TIMEOUT)
20 def tearDown(self):
21 socket.setdefaulttimeout(None)
23 def testURLread(self):
24 f = urllib2.urlopen("http://www.python.org/")
25 x = f.read()
28 class AuthTests(unittest.TestCase):
29 """Tests urllib2 authentication features."""
31 ## Disabled at the moment since there is no page under python.org which
32 ## could be used to HTTP authentication.
34 # def test_basic_auth(self):
35 # import httplib
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'
43 # # failure
44 # try:
45 # urllib2.urlopen(test_url)
46 # except urllib2.HTTPError, exc:
47 # self.assertEqual(exc.code, 401)
48 # else:
49 # self.fail("urlopen() should have failed with 401")
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/')
57 # response = urllib2.urlopen("http://www.python.org/")
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")
67 class urlopenNetworkTests(unittest.TestCase):
68 """Tests urllib2.urlopen using the network.
70 These tests are not exhaustive. Assuming that testing using files does a
71 good job overall of some of the basic interface features. There are no
72 tests exercising the optional 'data' and 'proxies' arguments. No tests
73 for transparent redirection have been written.
75 setUp is not used for always constructing a connection to
76 http://www.python.org/ since there a few tests that don't use that address
77 and making a connection is expensive enough to warrant minimizing unneeded
78 connections.
80 """
82 def test_basic(self):
83 # Simple test expected to pass.
84 open_url = urllib2.urlopen("http://www.python.org/")
85 for attr in ("read", "close", "info", "geturl"):
86 self.assert_(hasattr(open_url, attr), "object returned from "
87 "urlopen lacks the %s attribute" % attr)
88 try:
89 self.assert_(open_url.read(), "calling 'read' failed")
90 finally:
91 open_url.close()
93 def test_info(self):
94 # Test 'info'.
95 open_url = urllib2.urlopen("http://www.python.org/")
96 try:
97 info_obj = open_url.info()
98 finally:
99 open_url.close()
100 self.assert_(isinstance(info_obj, mimetools.Message),
101 "object returned by 'info' is not an instance of "
102 "mimetools.Message")
103 self.assertEqual(info_obj.getsubtype(), "html")
105 def test_geturl(self):
106 # Make sure same URL as opened is returned by geturl.
107 URL = "http://www.python.org/"
108 open_url = urllib2.urlopen(URL)
109 try:
110 gotten_url = open_url.geturl()
111 finally:
112 open_url.close()
113 self.assertEqual(gotten_url, URL)
115 def test_bad_address(self):
116 # Make sure proper exception is raised when connecting to a bogus
117 # address.
118 self.assertRaises(IOError,
119 # SF patch 809915: In Sep 2003, VeriSign started
120 # highjacking invalid .com and .net addresses to
121 # boost traffic to their own site. This test
122 # started failing then. One hopes the .invalid
123 # domain will be spared to serve its defined
124 # purpose.
125 # urllib2.urlopen, "http://www.sadflkjsasadf.com/")
126 urllib2.urlopen, "http://www.python.invalid./")
129 class OtherNetworkTests(unittest.TestCase):
130 def setUp(self):
131 if 0: # for debugging
132 import logging
133 logger = logging.getLogger("test_urllib2net")
134 logger.addHandler(logging.StreamHandler())
136 def test_range (self):
137 req = urllib2.Request("http://www.python.org",
138 headers={'Range': 'bytes=20-39'})
139 result = urllib2.urlopen(req)
140 data = result.read()
141 self.assertEqual(len(data), 20)
143 # XXX The rest of these tests aren't very good -- they don't check much.
144 # They do sometimes catch some major disasters, though.
146 def test_ftp(self):
147 urls = [
148 'ftp://www.python.org/pub/python/misc/sousa.au',
149 'ftp://www.python.org/pub/tmp/blat',
150 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
151 '/research-reports/00README-Legal-Rules-Regs',
153 self._test_urls(urls, self._extra_handlers())
155 def test_gopher(self):
156 import warnings
157 warnings.filterwarnings("ignore",
158 "the gopherlib module is deprecated",
159 DeprecationWarning,
160 "urllib2$")
161 urls = [
162 # Thanks to Fred for finding these!
163 'gopher://gopher.lib.ncsu.edu/11/library/stacks/Alex',
164 'gopher://gopher.vt.edu:10010/10/33',
166 self._test_urls(urls, self._extra_handlers())
168 def test_file(self):
169 TESTFN = test_support.TESTFN
170 f = open(TESTFN, 'w')
171 try:
172 f.write('hi there\n')
173 f.close()
174 urls = [
175 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
177 # XXX bug, should raise URLError
178 #('file://nonsensename/etc/passwd', None, urllib2.URLError)
179 ('file://nonsensename/etc/passwd', None, (OSError, socket.error))
181 self._test_urls(urls, self._extra_handlers())
182 finally:
183 os.remove(TESTFN)
185 def test_http(self):
186 urls = [
187 'http://www.espn.com/', # redirect
188 'http://www.python.org/Spanish/Inquistion/',
189 ('http://www.python.org/cgi-bin/faqw.py',
190 'query=pythonistas&querytype=simple&casefold=yes&req=search', None),
191 'http://www.python.org/',
193 self._test_urls(urls, self._extra_handlers())
195 # XXX Following test depends on machine configurations that are internal
196 # to CNRI. Need to set up a public server with the right authentication
197 # configuration for test purposes.
199 ## def test_cnri(self):
200 ## if socket.gethostname() == 'bitdiddle':
201 ## localhost = 'bitdiddle.cnri.reston.va.us'
202 ## elif socket.gethostname() == 'bitdiddle.concentric.net':
203 ## localhost = 'localhost'
204 ## else:
205 ## localhost = None
206 ## if localhost is not None:
207 ## urls = [
208 ## 'file://%s/etc/passwd' % localhost,
209 ## 'http://%s/simple/' % localhost,
210 ## 'http://%s/digest/' % localhost,
211 ## 'http://%s/not/found.h' % localhost,
212 ## ]
214 ## bauth = HTTPBasicAuthHandler()
215 ## bauth.add_password('basic_test_realm', localhost, 'jhylton',
216 ## 'password')
217 ## dauth = HTTPDigestAuthHandler()
218 ## dauth.add_password('digest_test_realm', localhost, 'jhylton',
219 ## 'password')
221 ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
223 def _test_urls(self, urls, handlers):
224 import socket
225 import time
226 import logging
227 debug = logging.getLogger("test_urllib2").debug
229 urllib2.install_opener(urllib2.build_opener(*handlers))
231 for url in urls:
232 if isinstance(url, tuple):
233 url, req, expected_err = url
234 else:
235 req = expected_err = None
236 debug(url)
237 try:
238 f = urllib2.urlopen(url, req)
239 except (IOError, socket.error, OSError), err:
240 debug(err)
241 if expected_err:
242 self.assert_(isinstance(err, expected_err))
243 else:
244 buf = f.read()
245 f.close()
246 debug("read %d bytes" % len(buf))
247 debug("******** next url coming up...")
248 time.sleep(0.1)
250 def _extra_handlers(self):
251 handlers = []
253 handlers.append(urllib2.GopherHandler)
255 cfh = urllib2.CacheFTPHandler()
256 cfh.setTimeout(1)
257 handlers.append(cfh)
259 return handlers
263 def test_main():
264 test_support.requires("network")
265 test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests,
266 AuthTests, OtherNetworkTests)
268 if __name__ == "__main__":
269 test_main()