blob: c9a483a962b253e53fc8387c9cfc8afd926991ec [file] [log] [blame]
Skip Montanaro89feabc2003-03-30 04:54:24 +00001#!/usr/bin/env python
2
3import unittest
4from test import test_support
5
6import socket
Brett Cannona71319e2003-05-14 02:18:31 +00007import urllib
Skip Montanaro89feabc2003-03-30 04:54:24 +00008import sys
Brett Cannona71319e2003-05-14 02:18:31 +00009import os
10import mimetools
Skip Montanaro89feabc2003-03-30 04:54:24 +000011
Christian Heimesaf98da12008-01-27 15:18:18 +000012
13def _open_with_retry(func, host, *args, **kwargs):
14 # Connecting to remote hosts is flaky. Make it more robust
15 # by retrying the connection several times.
16 for i in range(3):
17 try:
18 return func(host, *args, **kwargs)
19 except IOError, last_exc:
20 continue
21 except:
22 raise
23 raise last_exc
24
25
Skip Montanaro89feabc2003-03-30 04:54:24 +000026class URLTimeoutTest(unittest.TestCase):
27
28 TIMEOUT = 10.0
29
30 def setUp(self):
31 socket.setdefaulttimeout(self.TIMEOUT)
32
33 def tearDown(self):
34 socket.setdefaulttimeout(None)
35
36 def testURLread(self):
Christian Heimesaf98da12008-01-27 15:18:18 +000037 f = _open_with_retry(urllib.urlopen, "http://www.python.org/")
Skip Montanaro89feabc2003-03-30 04:54:24 +000038 x = f.read()
39
Brett Cannona71319e2003-05-14 02:18:31 +000040class urlopenNetworkTests(unittest.TestCase):
41 """Tests urllib.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000042
Brett Cannona71319e2003-05-14 02:18:31 +000043 These tests are not exhaustive. Assuming that testing using files does a
44 good job overall of some of the basic interface features. There are no
45 tests exercising the optional 'data' and 'proxies' arguments. No tests
46 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000047
Brett Cannona71319e2003-05-14 02:18:31 +000048 setUp is not used for always constructing a connection to
49 http://www.python.org/ since there a few tests that don't use that address
50 and making a connection is expensive enough to warrant minimizing unneeded
51 connections.
Tim Peters813cec92003-05-16 15:35:10 +000052
Brett Cannona71319e2003-05-14 02:18:31 +000053 """
54
Christian Heimesaf98da12008-01-27 15:18:18 +000055 def urlopen(self, *args):
56 return _open_with_retry(urllib.urlopen, *args)
57
Brett Cannona71319e2003-05-14 02:18:31 +000058 def test_basic(self):
59 # Simple test expected to pass.
Christian Heimesaf98da12008-01-27 15:18:18 +000060 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000061 for attr in ("read", "readline", "readlines", "fileno", "close",
62 "info", "geturl"):
63 self.assert_(hasattr(open_url, attr), "object returned from "
64 "urlopen lacks the %s attribute" % attr)
65 try:
66 self.assert_(open_url.read(), "calling 'read' failed")
67 finally:
68 open_url.close()
69
70 def test_readlines(self):
71 # Test both readline and readlines.
Christian Heimesaf98da12008-01-27 15:18:18 +000072 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000073 try:
Jeremy Hylton3e186152007-08-04 03:46:11 +000074 self.assert_(isinstance(open_url.readline(), bytes),
75 "readline did not return bytes")
Brett Cannona71319e2003-05-14 02:18:31 +000076 self.assert_(isinstance(open_url.readlines(), list),
77 "readlines did not return a list")
78 finally:
79 open_url.close()
80
81 def test_info(self):
82 # Test 'info'.
Christian Heimesaf98da12008-01-27 15:18:18 +000083 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000084 try:
85 info_obj = open_url.info()
86 finally:
87 open_url.close()
88 self.assert_(isinstance(info_obj, mimetools.Message),
89 "object returned by 'info' is not an instance of "
90 "mimetools.Message")
91 self.assertEqual(info_obj.getsubtype(), "html")
92
93 def test_geturl(self):
94 # Make sure same URL as opened is returned by geturl.
95 URL = "http://www.python.org/"
Christian Heimesaf98da12008-01-27 15:18:18 +000096 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000097 try:
98 gotten_url = open_url.geturl()
99 finally:
100 open_url.close()
101 self.assertEqual(gotten_url, URL)
102
Christian Heimes9bd667a2008-01-20 15:14:11 +0000103 def test_getcode(self):
104 # test getcode() with the fancy opener to get 404 error codes
105 URL = "http://www.python.org/XXXinvalidXXX"
106 open_url = urllib.FancyURLopener().open(URL)
107 try:
108 code = open_url.getcode()
109 finally:
110 open_url.close()
111 self.assertEqual(code, 404)
112
Brett Cannona71319e2003-05-14 02:18:31 +0000113 def test_fileno(self):
Tim Petersf545baa2003-06-15 23:26:30 +0000114 if (sys.platform in ('win32',) or
Tim Peters813cec92003-05-16 15:35:10 +0000115 not hasattr(os, 'fdopen')):
116 # On Windows, socket handles are not file descriptors; this
117 # test can't pass on Windows.
118 return
Brett Cannona71319e2003-05-14 02:18:31 +0000119 # Make sure fd returned by fileno is valid.
Christian Heimesaf98da12008-01-27 15:18:18 +0000120 open_url = self.urlopen("http://www.python.org/")
Tim Peters813cec92003-05-16 15:35:10 +0000121 fd = open_url.fileno()
122 FILE = os.fdopen(fd)
123 try:
124 self.assert_(FILE.read(), "reading from file created using fd "
125 "returned by fileno failed")
126 finally:
127 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000128
129 def test_bad_address(self):
130 # Make sure proper exception is raised when connecting to a bogus
131 # address.
132 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000133 # SF patch 809915: In Sep 2003, VeriSign started
134 # highjacking invalid .com and .net addresses to
135 # boost traffic to their own site. This test
136 # started failing then. One hopes the .invalid
137 # domain will be spared to serve its defined
138 # purpose.
139 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 urllib.urlopen, "http://www.python.invalid./")
Brett Cannona71319e2003-05-14 02:18:31 +0000141
142class urlretrieveNetworkTests(unittest.TestCase):
143 """Tests urllib.urlretrieve using the network."""
144
Christian Heimesaf98da12008-01-27 15:18:18 +0000145 def urlretrieve(self, *args):
146 return _open_with_retry(urllib.urlretrieve, *args)
147
Brett Cannona71319e2003-05-14 02:18:31 +0000148 def test_basic(self):
149 # Test basic functionality.
Christian Heimesaf98da12008-01-27 15:18:18 +0000150 file_location,info = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000151 self.assert_(os.path.exists(file_location), "file location returned by"
152 " urlretrieve is not a valid path")
Alex Martelli01c77c62006-08-24 02:58:11 +0000153 FILE = open(file_location)
Brett Cannona71319e2003-05-14 02:18:31 +0000154 try:
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000155 self.assert_(FILE.read(), "reading from the file location returned"
156 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000157 finally:
158 FILE.close()
159 os.unlink(file_location)
160
161 def test_specified_path(self):
162 # Make sure that specifying the location of the file to write to works.
Christian Heimesaf98da12008-01-27 15:18:18 +0000163 file_location,info = self.urlretrieve("http://www.python.org/",
164 test_support.TESTFN)
Brett Cannona71319e2003-05-14 02:18:31 +0000165 self.assertEqual(file_location, test_support.TESTFN)
166 self.assert_(os.path.exists(file_location))
Alex Martelli01c77c62006-08-24 02:58:11 +0000167 FILE = open(file_location)
Brett Cannona71319e2003-05-14 02:18:31 +0000168 try:
169 self.assert_(FILE.read(), "reading from temporary file failed")
170 finally:
171 FILE.close()
172 os.unlink(file_location)
173
174 def test_header(self):
175 # Make sure header returned as 2nd value from urlretrieve is good.
Christian Heimesaf98da12008-01-27 15:18:18 +0000176 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000177 os.unlink(file_location)
178 self.assert_(isinstance(header, mimetools.Message),
179 "header is not an instance of mimetools.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000180
Brett Cannona71319e2003-05-14 02:18:31 +0000181
182
Skip Montanaro89feabc2003-03-30 04:54:24 +0000183def test_main():
184 test_support.requires('network')
Brett Cannona71319e2003-05-14 02:18:31 +0000185 test_support.run_unittest(URLTimeoutTest,
186 urlopenNetworkTests,
187 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000188
189if __name__ == "__main__":
190 test_main()