Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | from test import test_support |
| 5 | |
| 6 | import socket |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 7 | import urllib |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 8 | import sys |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 9 | import os |
| 10 | import mimetools |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 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): |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 23 | f = urllib.urlopen("http://www.python.org/") |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 24 | x = f.read() |
| 25 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 26 | class urlopenNetworkTests(unittest.TestCase): |
| 27 | """Tests urllib.urlopen using the network. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 28 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 29 | These tests are not exhaustive. Assuming that testing using files does a |
| 30 | good job overall of some of the basic interface features. There are no |
| 31 | tests exercising the optional 'data' and 'proxies' arguments. No tests |
| 32 | for transparent redirection have been written. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 33 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 34 | setUp is not used for always constructing a connection to |
| 35 | http://www.python.org/ since there a few tests that don't use that address |
| 36 | and making a connection is expensive enough to warrant minimizing unneeded |
| 37 | connections. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 38 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 39 | """ |
| 40 | |
| 41 | def test_basic(self): |
| 42 | # Simple test expected to pass. |
| 43 | open_url = urllib.urlopen("http://www.python.org/") |
| 44 | for attr in ("read", "readline", "readlines", "fileno", "close", |
| 45 | "info", "geturl"): |
| 46 | self.assert_(hasattr(open_url, attr), "object returned from " |
| 47 | "urlopen lacks the %s attribute" % attr) |
| 48 | try: |
| 49 | self.assert_(open_url.read(), "calling 'read' failed") |
| 50 | finally: |
| 51 | open_url.close() |
| 52 | |
| 53 | def test_readlines(self): |
| 54 | # Test both readline and readlines. |
| 55 | open_url = urllib.urlopen("http://www.python.org/") |
| 56 | try: |
| 57 | self.assert_(isinstance(open_url.readline(), basestring), |
| 58 | "readline did not return a string") |
| 59 | self.assert_(isinstance(open_url.readlines(), list), |
| 60 | "readlines did not return a list") |
| 61 | finally: |
| 62 | open_url.close() |
| 63 | |
| 64 | def test_info(self): |
| 65 | # Test 'info'. |
| 66 | open_url = urllib.urlopen("http://www.python.org/") |
| 67 | try: |
| 68 | info_obj = open_url.info() |
| 69 | finally: |
| 70 | open_url.close() |
| 71 | self.assert_(isinstance(info_obj, mimetools.Message), |
| 72 | "object returned by 'info' is not an instance of " |
| 73 | "mimetools.Message") |
| 74 | self.assertEqual(info_obj.getsubtype(), "html") |
| 75 | |
| 76 | def test_geturl(self): |
| 77 | # Make sure same URL as opened is returned by geturl. |
| 78 | URL = "http://www.python.org/" |
| 79 | open_url = urllib.urlopen(URL) |
| 80 | try: |
| 81 | gotten_url = open_url.geturl() |
| 82 | finally: |
| 83 | open_url.close() |
| 84 | self.assertEqual(gotten_url, URL) |
| 85 | |
| 86 | def test_fileno(self): |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 87 | if (sys.platform in ('win32',) or |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 88 | not hasattr(os, 'fdopen')): |
| 89 | # On Windows, socket handles are not file descriptors; this |
| 90 | # test can't pass on Windows. |
| 91 | return |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 92 | # Make sure fd returned by fileno is valid. |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 93 | open_url = urllib.urlopen("http://www.python.org/") |
| 94 | fd = open_url.fileno() |
| 95 | FILE = os.fdopen(fd) |
| 96 | try: |
| 97 | self.assert_(FILE.read(), "reading from file created using fd " |
| 98 | "returned by fileno failed") |
| 99 | finally: |
| 100 | FILE.close() |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 101 | |
| 102 | def test_bad_address(self): |
| 103 | # Make sure proper exception is raised when connecting to a bogus |
| 104 | # address. |
| 105 | self.assertRaises(IOError, |
Tim Peters | 0aab002 | 2003-09-20 22:16:26 +0000 | [diff] [blame] | 106 | # SF patch 809915: In Sep 2003, VeriSign started |
| 107 | # highjacking invalid .com and .net addresses to |
| 108 | # boost traffic to their own site. This test |
| 109 | # started failing then. One hopes the .invalid |
| 110 | # domain will be spared to serve its defined |
| 111 | # purpose. |
| 112 | # urllib.urlopen, "http://www.sadflkjsasadf.com/") |
| 113 | urllib.urlopen, "http://www.python.invalid/") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 114 | |
| 115 | class urlretrieveNetworkTests(unittest.TestCase): |
| 116 | """Tests urllib.urlretrieve using the network.""" |
| 117 | |
| 118 | def test_basic(self): |
| 119 | # Test basic functionality. |
| 120 | file_location,info = urllib.urlretrieve("http://www.python.org/") |
| 121 | self.assert_(os.path.exists(file_location), "file location returned by" |
| 122 | " urlretrieve is not a valid path") |
| 123 | FILE = file(file_location) |
| 124 | try: |
Jeremy Hylton | bd9f520 | 2003-07-17 16:31:00 +0000 | [diff] [blame] | 125 | self.assert_(FILE.read(), "reading from the file location returned" |
| 126 | " by urlretrieve failed") |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 127 | finally: |
| 128 | FILE.close() |
| 129 | os.unlink(file_location) |
| 130 | |
| 131 | def test_specified_path(self): |
| 132 | # Make sure that specifying the location of the file to write to works. |
| 133 | file_location,info = urllib.urlretrieve("http://www.python.org/", |
| 134 | test_support.TESTFN) |
| 135 | self.assertEqual(file_location, test_support.TESTFN) |
| 136 | self.assert_(os.path.exists(file_location)) |
| 137 | FILE = file(file_location) |
| 138 | try: |
| 139 | self.assert_(FILE.read(), "reading from temporary file failed") |
| 140 | finally: |
| 141 | FILE.close() |
| 142 | os.unlink(file_location) |
| 143 | |
| 144 | def test_header(self): |
| 145 | # Make sure header returned as 2nd value from urlretrieve is good. |
| 146 | file_location, header = urllib.urlretrieve("http://www.python.org/") |
| 147 | os.unlink(file_location) |
| 148 | self.assert_(isinstance(header, mimetools.Message), |
| 149 | "header is not an instance of mimetools.Message") |
Tim Peters | 813cec9 | 2003-05-16 15:35:10 +0000 | [diff] [blame] | 150 | |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 151 | |
| 152 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 153 | def test_main(): |
| 154 | test_support.requires('network') |
Brett Cannon | a71319e | 2003-05-14 02:18:31 +0000 | [diff] [blame] | 155 | test_support.run_unittest(URLTimeoutTest, |
| 156 | urlopenNetworkTests, |
| 157 | urlretrieveNetworkTests) |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 158 | |
| 159 | if __name__ == "__main__": |
| 160 | test_main() |