4 from test
import test_support
5 from test
.test_urllib2
import sanepathname2url
13 class URLTimeoutTest(unittest
.TestCase
):
18 socket
.setdefaulttimeout(self
.TIMEOUT
)
21 socket
.setdefaulttimeout(None)
23 def testURLread(self
):
24 f
= urllib2
.urlopen("http://www.python.org/")
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):
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'
45 # urllib2.urlopen(test_url)
46 # except urllib2.HTTPError, exc:
47 # self.assertEqual(exc.code, 401)
49 # self.fail("urlopen() should have failed with 401")
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
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
)
89 self
.assert_(open_url
.read(), "calling 'read' failed")
95 open_url
= urllib2
.urlopen("http://www.python.org/")
97 info_obj
= open_url
.info()
100 self
.assert_(isinstance(info_obj
, mimetools
.Message
),
101 "object returned by 'info' is not an instance of "
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
)
110 gotten_url
= open_url
.geturl()
113 self
.assertEqual(gotten_url
, URL
)
115 def test_bad_address(self
):
116 # Make sure proper exception is raised when connecting to a bogus
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
125 # urllib2.urlopen, "http://www.sadflkjsasadf.com/")
126 urllib2
.urlopen
, "http://www.python.invalid./")
129 class OtherNetworkTests(unittest
.TestCase
):
131 if 0: # for debugging
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
)
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.
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
):
157 warnings
.filterwarnings("ignore",
158 "the gopherlib module is deprecated",
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
())
169 TESTFN
= test_support
.TESTFN
170 f
= open(TESTFN
, 'w')
172 f
.write('hi there\n')
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
())
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'
206 ## if localhost is not None:
208 ## 'file://%s/etc/passwd' % localhost,
209 ## 'http://%s/simple/' % localhost,
210 ## 'http://%s/digest/' % localhost,
211 ## 'http://%s/not/found.h' % localhost,
214 ## bauth = HTTPBasicAuthHandler()
215 ## bauth.add_password('basic_test_realm', localhost, 'jhylton',
217 ## dauth = HTTPDigestAuthHandler()
218 ## dauth.add_password('digest_test_realm', localhost, 'jhylton',
221 ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
223 def _test_urls(self
, urls
, handlers
):
227 debug
= logging
.getLogger("test_urllib2").debug
229 urllib2
.install_opener(urllib2
.build_opener(*handlers
))
232 if isinstance(url
, tuple):
233 url
, req
, expected_err
= url
235 req
= expected_err
= None
238 f
= urllib2
.urlopen(url
, req
)
239 except (IOError, socket
.error
, OSError), err
:
242 self
.assert_(isinstance(err
, expected_err
))
246 debug("read %d bytes" % len(buf
))
247 debug("******** next url coming up...")
250 def _extra_handlers(self
):
253 handlers
.append(urllib2
.GopherHandler
)
255 cfh
= urllib2
.CacheFTPHandler()
264 test_support
.requires("network")
265 test_support
.run_unittest(URLTimeoutTest
, urlopenNetworkTests
,
266 AuthTests
, OtherNetworkTests
)
268 if __name__
== "__main__":