PyXR

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



0001 #!/usr/bin/env python
0002 
0003 import unittest
0004 from test import test_support
0005 
0006 import socket
0007 import urllib
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 = urllib.urlopen("http://www.python.org/")
0024         x = f.read()
0025 
0026 class urlopenNetworkTests(unittest.TestCase):
0027     """Tests urllib.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 = urllib.urlopen("http://www.python.org/")
0044         for attr in ("read", "readline", "readlines", "fileno", "close",
0045                      "info", "geturl"):
0046             self.assert_(hasattr(open_url, attr), "object returned from "
0047                             "urlopen lacks the %s attribute" % attr)
0048         try:
0049             self.assert_(open_url.read(), "calling 'read' failed")
0050         finally:
0051             open_url.close()
0052 
0053     def test_readlines(self):
0054         # Test both readline and readlines.
0055         open_url = urllib.urlopen("http://www.python.org/")
0056         try:
0057             self.assert_(isinstance(open_url.readline(), basestring),
0058                          "readline did not return a string")
0059             self.assert_(isinstance(open_url.readlines(), list),
0060                          "readlines did not return a list")
0061         finally:
0062             open_url.close()
0063 
0064     def test_info(self):
0065         # Test 'info'.
0066         open_url = urllib.urlopen("http://www.python.org/")
0067         try:
0068             info_obj = open_url.info()
0069         finally:
0070             open_url.close()
0071             self.assert_(isinstance(info_obj, mimetools.Message),
0072                          "object returned by 'info' is not an instance of "
0073                          "mimetools.Message")
0074             self.assertEqual(info_obj.getsubtype(), "html")
0075 
0076     def test_geturl(self):
0077         # Make sure same URL as opened is returned by geturl.
0078         URL = "http://www.python.org/"
0079         open_url = urllib.urlopen(URL)
0080         try:
0081             gotten_url = open_url.geturl()
0082         finally:
0083             open_url.close()
0084         self.assertEqual(gotten_url, URL)
0085 
0086     def test_fileno(self):
0087         if (sys.platform in ('win32',) or
0088                 not hasattr(os, 'fdopen')):
0089             # On Windows, socket handles are not file descriptors; this
0090             # test can't pass on Windows.
0091             return
0092         # Make sure fd returned by fileno is valid.
0093         open_url = urllib.urlopen("http://www.python.org/")
0094         fd = open_url.fileno()
0095         FILE = os.fdopen(fd)
0096         try:
0097             self.assert_(FILE.read(), "reading from file created using fd "
0098                                       "returned by fileno failed")
0099         finally:
0100             FILE.close()
0101 
0102     def test_bad_address(self):
0103         # Make sure proper exception is raised when connecting to a bogus
0104         # address.
0105         self.assertRaises(IOError,
0106                           # SF patch 809915:  In Sep 2003, VeriSign started
0107                           # highjacking invalid .com and .net addresses to
0108                           # boost traffic to their own site.  This test
0109                           # started failing then.  One hopes the .invalid
0110                           # domain will be spared to serve its defined
0111                           # purpose.
0112                           # urllib.urlopen, "http://www.sadflkjsasadf.com/")
0113                           urllib.urlopen, "http://www.python.invalid/")
0114 
0115 class urlretrieveNetworkTests(unittest.TestCase):
0116     """Tests urllib.urlretrieve using the network."""
0117 
0118     def test_basic(self):
0119         # Test basic functionality.
0120         file_location,info = urllib.urlretrieve("http://www.python.org/")
0121         self.assert_(os.path.exists(file_location), "file location returned by"
0122                         " urlretrieve is not a valid path")
0123         FILE = file(file_location)
0124         try:
0125             self.assert_(FILE.read(), "reading from the file location returned"
0126                          " by urlretrieve failed")
0127         finally:
0128             FILE.close()
0129             os.unlink(file_location)
0130 
0131     def test_specified_path(self):
0132         # Make sure that specifying the location of the file to write to works.
0133         file_location,info = urllib.urlretrieve("http://www.python.org/",
0134                                                 test_support.TESTFN)
0135         self.assertEqual(file_location, test_support.TESTFN)
0136         self.assert_(os.path.exists(file_location))
0137         FILE = file(file_location)
0138         try:
0139             self.assert_(FILE.read(), "reading from temporary file failed")
0140         finally:
0141             FILE.close()
0142             os.unlink(file_location)
0143 
0144     def test_header(self):
0145         # Make sure header returned as 2nd value from urlretrieve is good.
0146         file_location, header = urllib.urlretrieve("http://www.python.org/")
0147         os.unlink(file_location)
0148         self.assert_(isinstance(header, mimetools.Message),
0149                      "header is not an instance of mimetools.Message")
0150 
0151 
0152 
0153 def test_main():
0154     test_support.requires('network')
0155     test_support.run_unittest(URLTimeoutTest,
0156                               urlopenNetworkTests,
0157                               urlretrieveNetworkTests)
0158 
0159 if __name__ == "__main__":
0160     test_main()
0161 

Generated by PyXR 0.9.4
SourceForge.net Logo