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 |
| 5 | |
| 6 | import socket |
| 7 | import urllib2 |
| 8 | import sys |
| 9 | import os |
| 10 | import mimetools |
| 11 | |
| 12 | class URLTimeoutTest(unittest.TestCase): |
| 13 | |
| 14 | TIMEOUT = 10.0 |
| 15 | |
| 16 | def setUp(self): |
| 17 | socket.setdefaulttimeout(self.TIMEOUT) |
| 18 | |
| 19 | def tearDown(self): |
| 20 | socket.setdefaulttimeout(None) |
| 21 | |
| 22 | def testURLread(self): |
| 23 | f = urllib2.urlopen("http://www.python.org/") |
| 24 | x = f.read() |
| 25 | |
Georg Brandl | fa42bd7 | 2006-04-30 07:06:11 +0000 | [diff] [blame] | 26 | |
| 27 | class AuthTests(unittest.TestCase): |
| 28 | """Tests urllib2 authentication features.""" |
| 29 | |
| 30 | ## Disabled at the moment since there is no page under python.org which |
| 31 | ## could be used to HTTP authentication. |
| 32 | # |
| 33 | # def test_basic_auth(self): |
| 34 | # import httplib |
| 35 | # |
| 36 | # test_url = "http://www.python.org/test/test_urllib2/basic_auth" |
| 37 | # test_hostport = "www.python.org" |
| 38 | # test_realm = 'Test Realm' |
| 39 | # test_user = 'test.test_urllib2net' |
| 40 | # test_password = 'blah' |
| 41 | # |
| 42 | # # failure |
| 43 | # try: |
| 44 | # urllib2.urlopen(test_url) |
| 45 | # except urllib2.HTTPError, exc: |
| 46 | # self.assertEqual(exc.code, 401) |
| 47 | # else: |
| 48 | # self.fail("urlopen() should have failed with 401") |
| 49 | # |
| 50 | # # success |
| 51 | # auth_handler = urllib2.HTTPBasicAuthHandler() |
| 52 | # auth_handler.add_password(test_realm, test_hostport, |
| 53 | # test_user, test_password) |
| 54 | # opener = urllib2.build_opener(auth_handler) |
| 55 | # f = opener.open('http://localhost/') |
| 56 | # response = urllib2.urlopen("http://www.python.org/") |
| 57 | # |
| 58 | # # The 'userinfo' URL component is deprecated by RFC 3986 for security |
| 59 | # # reasons, let's not implement it! (it's already implemented for proxy |
| 60 | # # specification strings (that is, URLs or authorities specifying a |
| 61 | # # proxy), so we must keep that) |
| 62 | # self.assertRaises(httplib.InvalidURL, |
| 63 | # urllib2.urlopen, "http://evil:thing@example.com") |
| 64 | |
| 65 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 66 | class urlopenNetworkTests(unittest.TestCase): |
| 67 | """Tests urllib2.urlopen using the network. |
| 68 | |
| 69 | These tests are not exhaustive. Assuming that testing using files does a |
| 70 | good job overall of some of the basic interface features. There are no |
| 71 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 72 | for transparent redirection have been written. |
| 73 | |
| 74 | setUp is not used for always constructing a connection to |
| 75 | http://www.python.org/ since there a few tests that don't use that address |
| 76 | and making a connection is expensive enough to warrant minimizing unneeded |
| 77 | connections. |
| 78 | |
| 79 | """ |
| 80 | |
| 81 | def test_basic(self): |
| 82 | # Simple test expected to pass. |
| 83 | open_url = urllib2.urlopen("http://www.python.org/") |
| 84 | for attr in ("read", "close", "info", "geturl"): |
| 85 | self.assert_(hasattr(open_url, attr), "object returned from " |
| 86 | "urlopen lacks the %s attribute" % attr) |
| 87 | try: |
| 88 | self.assert_(open_url.read(), "calling 'read' failed") |
| 89 | finally: |
| 90 | open_url.close() |
| 91 | |
| 92 | def test_info(self): |
| 93 | # Test 'info'. |
| 94 | open_url = urllib2.urlopen("http://www.python.org/") |
| 95 | try: |
| 96 | info_obj = open_url.info() |
| 97 | finally: |
| 98 | open_url.close() |
| 99 | self.assert_(isinstance(info_obj, mimetools.Message), |
| 100 | "object returned by 'info' is not an instance of " |
| 101 | "mimetools.Message") |
| 102 | self.assertEqual(info_obj.getsubtype(), "html") |
| 103 | |
| 104 | def test_geturl(self): |
| 105 | # Make sure same URL as opened is returned by geturl. |
| 106 | URL = "http://www.python.org/" |
| 107 | open_url = urllib2.urlopen(URL) |
| 108 | try: |
| 109 | gotten_url = open_url.geturl() |
| 110 | finally: |
| 111 | open_url.close() |
| 112 | self.assertEqual(gotten_url, URL) |
| 113 | |
| 114 | def test_bad_address(self): |
| 115 | # Make sure proper exception is raised when connecting to a bogus |
| 116 | # address. |
| 117 | self.assertRaises(IOError, |
| 118 | # SF patch 809915: In Sep 2003, VeriSign started |
| 119 | # highjacking invalid .com and .net addresses to |
| 120 | # boost traffic to their own site. This test |
| 121 | # started failing then. One hopes the .invalid |
| 122 | # domain will be spared to serve its defined |
| 123 | # purpose. |
| 124 | # urllib2.urlopen, "http://www.sadflkjsasadf.com/") |
| 125 | urllib2.urlopen, "http://www.python.invalid/") |
| 126 | |
| 127 | def test_main(): |
| 128 | test_support.requires("network") |
Georg Brandl | fa42bd7 | 2006-04-30 07:06:11 +0000 | [diff] [blame] | 129 | test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests, |
| 130 | AuthTests) |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 131 | |
| 132 | if __name__ == "__main__": |
| 133 | test_main() |