Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | from test import test_support |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 5 | from test.test_urllib2 import sanepathname2url |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 6 | |
| 7 | import socket |
| 8 | import urllib2 |
| 9 | import sys |
| 10 | import os |
| 11 | import mimetools |
| 12 | |
| 13 | class URLTimeoutTest(unittest.TestCase): |
| 14 | |
| 15 | TIMEOUT = 10.0 |
| 16 | |
| 17 | def setUp(self): |
| 18 | socket.setdefaulttimeout(self.TIMEOUT) |
| 19 | |
| 20 | def tearDown(self): |
| 21 | socket.setdefaulttimeout(None) |
| 22 | |
| 23 | def testURLread(self): |
| 24 | f = urllib2.urlopen("http://www.python.org/") |
| 25 | x = f.read() |
| 26 | |
Georg Brandl | fa42bd7 | 2006-04-30 07:06:11 +0000 | [diff] [blame] | 27 | |
| 28 | class 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: |
| 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") |
| 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/') |
| 57 | # response = urllib2.urlopen("http://www.python.org/") |
| 58 | # |
| 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 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 67 | class urlopenNetworkTests(unittest.TestCase): |
| 68 | """Tests urllib2.urlopen using the network. |
| 69 | |
| 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. |
| 74 | |
| 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. |
| 79 | |
| 80 | """ |
| 81 | |
| 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() |
| 92 | |
| 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") |
| 104 | |
| 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) |
| 114 | |
| 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/") |
Neal Norwitz | a29fc29 | 2006-06-11 20:25:56 +0000 | [diff] [blame] | 126 | urllib2.urlopen, "http://www.python.invalid./") |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 127 | |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 128 | |
| 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()) |
| 135 | |
| 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) |
| 142 | |
| 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. |
| 145 | |
| 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', |
| 152 | ] |
| 153 | self._test_urls(urls, self._extra_handlers()) |
| 154 | |
| 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! |
Neal Norwitz | f054aeb | 2006-06-11 20:42:02 +0000 | [diff] [blame] | 163 | 'gopher://gopher.lib.ncsu.edu./11/library/stacks/Alex', |
| 164 | 'gopher://gopher.vt.edu.:10010/10/33', |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 165 | ] |
| 166 | self._test_urls(urls, self._extra_handlers()) |
| 167 | |
| 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)), |
| 176 | |
| 177 | # XXX bug, should raise URLError |
| 178 | #('file://nonsensename/etc/passwd', None, urllib2.URLError) |
Neal Norwitz | 896c1ea | 2006-06-11 20:46:46 +0000 | [diff] [blame] | 179 | ('file://nonsensename/etc/passwd', None, (EnvironmentError, socket.error)) |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 180 | ] |
| 181 | self._test_urls(urls, self._extra_handlers()) |
| 182 | finally: |
| 183 | os.remove(TESTFN) |
| 184 | |
| 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/', |
| 192 | ] |
| 193 | self._test_urls(urls, self._extra_handlers()) |
| 194 | |
| 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. |
| 198 | |
| 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 | ## ] |
| 213 | |
| 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') |
| 220 | |
| 221 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 222 | |
| 223 | def _test_urls(self, urls, handlers): |
| 224 | import socket |
| 225 | import time |
| 226 | import logging |
| 227 | debug = logging.getLogger("test_urllib2").debug |
| 228 | |
| 229 | urllib2.install_opener(urllib2.build_opener(*handlers)) |
| 230 | |
| 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: |
Neal Norwitz | f054aeb | 2006-06-11 20:42:02 +0000 | [diff] [blame] | 242 | msg = ("Didn't get expected error(s) %s for %s %s, got %s" % |
| 243 | (expected_err, url, req, err)) |
| 244 | self.assert_(isinstance(err, expected_err), msg) |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 245 | else: |
| 246 | buf = f.read() |
| 247 | f.close() |
| 248 | debug("read %d bytes" % len(buf)) |
| 249 | debug("******** next url coming up...") |
| 250 | time.sleep(0.1) |
| 251 | |
| 252 | def _extra_handlers(self): |
| 253 | handlers = [] |
| 254 | |
| 255 | handlers.append(urllib2.GopherHandler) |
| 256 | |
| 257 | cfh = urllib2.CacheFTPHandler() |
| 258 | cfh.setTimeout(1) |
| 259 | handlers.append(cfh) |
| 260 | |
| 261 | return handlers |
| 262 | |
| 263 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 264 | def test_main(): |
| 265 | test_support.requires("network") |
Georg Brandl | fa42bd7 | 2006-04-30 07:06:11 +0000 | [diff] [blame] | 266 | test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests, |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 267 | AuthTests, OtherNetworkTests) |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 268 | |
| 269 | if __name__ == "__main__": |
| 270 | test_main() |