PyXR

c:\python24\lib \ test \ test_urllib2net.py



0001 #!/usr/bin/env python
0002 
0003 import unittest
0004 from test import test_support
0005 
0006 import socket
0007 import urllib2
0008 import sys
0009 import os
0010 import mimetools
0011 
0012 class URLTimeoutTest(unittest.TestCase):
0013 
0014     TIMEOUT = 10.0
0015 
0016     def setUp(self):
0017         socket.setdefaulttimeout(self.TIMEOUT)
0018 
0019     def tearDown(self):
0020         socket.setdefaulttimeout(None)
0021 
0022     def testURLread(self):
0023         f = urllib2.urlopen("http://www.python.org/")
0024         x = f.read()
0025 
0026 class urlopenNetworkTests(unittest.TestCase):
0027     """Tests urllib2.urlopen using the network.
0028 
0029     These tests are not exhaustive.  Assuming that testing using files does a
0030     good job overall of some of the basic interface features.  There are no
0031     tests exercising the optional 'data' and 'proxies' arguments.  No tests
0032     for transparent redirection have been written.
0033 
0034     setUp is not used for always constructing a connection to
0035     http://www.python.org/ since there a few tests that don't use that address
0036     and making a connection is expensive enough to warrant minimizing unneeded
0037     connections.
0038 
0039     """
0040 
0041     def test_basic(self):
0042         # Simple test expected to pass.
0043         open_url = urllib2.urlopen("http://www.python.org/")
0044         for attr in ("read", "close", "info", "geturl"):
0045             self.assert_(hasattr(open_url, attr), "object returned from "
0046                             "urlopen lacks the %s attribute" % attr)
0047         try:
0048             self.assert_(open_url.read(), "calling 'read' failed")
0049         finally:
0050             open_url.close()
0051 
0052     def test_info(self):
0053         # Test 'info'.
0054         open_url = urllib2.urlopen("http://www.python.org/")
0055         try:
0056             info_obj = open_url.info()
0057         finally:
0058             open_url.close()
0059             self.assert_(isinstance(info_obj, mimetools.Message),
0060                          "object returned by 'info' is not an instance of "
0061                          "mimetools.Message")
0062             self.assertEqual(info_obj.getsubtype(), "html")
0063 
0064     def test_geturl(self):
0065         # Make sure same URL as opened is returned by geturl.
0066         URL = "http://www.python.org/"
0067         open_url = urllib2.urlopen(URL)
0068         try:
0069             gotten_url = open_url.geturl()
0070         finally:
0071             open_url.close()
0072         self.assertEqual(gotten_url, URL)
0073 
0074     def test_bad_address(self):
0075         # Make sure proper exception is raised when connecting to a bogus
0076         # address.
0077         self.assertRaises(IOError,
0078                           # SF patch 809915:  In Sep 2003, VeriSign started
0079                           # highjacking invalid .com and .net addresses to
0080                           # boost traffic to their own site.  This test
0081                           # started failing then.  One hopes the .invalid
0082                           # domain will be spared to serve its defined
0083                           # purpose.
0084                           # urllib2.urlopen, "http://www.sadflkjsasadf.com/")
0085                           urllib2.urlopen, "http://www.python.invalid/")
0086 
0087 def test_main():
0088     test_support.requires("network")
0089     test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests)
0090 
0091 if __name__ == "__main__":
0092     test_main()
0093 

Generated by PyXR 0.9.4
SourceForge.net Logo