4 from test
import test_support
12 mimetools
= test_support
.import_module("mimetools", deprecated
=True)
15 def _open_with_retry(func
, host
, *args
, **kwargs
):
16 # Connecting to remote hosts is flaky. Make it more robust
17 # by retrying the connection several times.
20 return func(host
, *args
, **kwargs
)
21 except IOError, last_exc
:
28 class URLTimeoutTest(unittest
.TestCase
):
33 socket
.setdefaulttimeout(self
.TIMEOUT
)
36 socket
.setdefaulttimeout(None)
38 def testURLread(self
):
39 f
= _open_with_retry(urllib
.urlopen
, "http://www.python.org/")
42 class urlopenNetworkTests(unittest
.TestCase
):
43 """Tests urllib.urlopen using the network.
45 These tests are not exhaustive. Assuming that testing using files does a
46 good job overall of some of the basic interface features. There are no
47 tests exercising the optional 'data' and 'proxies' arguments. No tests
48 for transparent redirection have been written.
50 setUp is not used for always constructing a connection to
51 http://www.python.org/ since there a few tests that don't use that address
52 and making a connection is expensive enough to warrant minimizing unneeded
57 def urlopen(self
, *args
):
58 return _open_with_retry(urllib
.urlopen
, *args
)
61 # Simple test expected to pass.
62 open_url
= self
.urlopen("http://www.python.org/")
63 for attr
in ("read", "readline", "readlines", "fileno", "close",
65 self
.assertTrue(hasattr(open_url
, attr
), "object returned from "
66 "urlopen lacks the %s attribute" % attr
)
68 self
.assertTrue(open_url
.read(), "calling 'read' failed")
72 def test_readlines(self
):
73 # Test both readline and readlines.
74 open_url
= self
.urlopen("http://www.python.org/")
76 self
.assertIsInstance(open_url
.readline(), basestring
,
77 "readline did not return a string")
78 self
.assertIsInstance(open_url
.readlines(), list,
79 "readlines did not return a list")
85 open_url
= self
.urlopen("http://www.python.org/")
87 info_obj
= open_url
.info()
90 self
.assertIsInstance(info_obj
, mimetools
.Message
,
91 "object returned by 'info' is not an "
92 "instance of mimetools.Message")
93 self
.assertEqual(info_obj
.getsubtype(), "html")
95 def test_geturl(self
):
96 # Make sure same URL as opened is returned by geturl.
97 URL
= "http://www.python.org/"
98 open_url
= self
.urlopen(URL
)
100 gotten_url
= open_url
.geturl()
103 self
.assertEqual(gotten_url
, URL
)
105 def test_getcode(self
):
106 # test getcode() with the fancy opener to get 404 error codes
107 URL
= "http://www.python.org/XXXinvalidXXX"
108 open_url
= urllib
.FancyURLopener().open(URL
)
110 code
= open_url
.getcode()
113 self
.assertEqual(code
, 404)
115 def test_fileno(self
):
116 if (sys
.platform
in ('win32',) or
117 not hasattr(os
, 'fdopen')):
118 # On Windows, socket handles are not file descriptors; this
119 # test can't pass on Windows.
121 # Make sure fd returned by fileno is valid.
122 open_url
= self
.urlopen("http://www.python.org/")
123 fd
= open_url
.fileno()
126 self
.assertTrue(FILE
.read(), "reading from file created using fd "
127 "returned by fileno failed")
131 def test_bad_address(self
):
132 # Make sure proper exception is raised when connecting to a bogus
134 self
.assertRaises(IOError,
135 # SF patch 809915: In Sep 2003, VeriSign started
136 # highjacking invalid .com and .net addresses to
137 # boost traffic to their own site. This test
138 # started failing then. One hopes the .invalid
139 # domain will be spared to serve its defined
141 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
142 urllib
.urlopen
, "http://sadflkjsasf.i.nvali.d/")
144 class urlretrieveNetworkTests(unittest
.TestCase
):
145 """Tests urllib.urlretrieve using the network."""
147 def urlretrieve(self
, *args
):
148 return _open_with_retry(urllib
.urlretrieve
, *args
)
150 def test_basic(self
):
151 # Test basic functionality.
152 file_location
,info
= self
.urlretrieve("http://www.python.org/")
153 self
.assertTrue(os
.path
.exists(file_location
), "file location returned by"
154 " urlretrieve is not a valid path")
155 FILE
= file(file_location
)
157 self
.assertTrue(FILE
.read(), "reading from the file location returned"
158 " by urlretrieve failed")
161 os
.unlink(file_location
)
163 def test_specified_path(self
):
164 # Make sure that specifying the location of the file to write to works.
165 file_location
,info
= self
.urlretrieve("http://www.python.org/",
167 self
.assertEqual(file_location
, test_support
.TESTFN
)
168 self
.assertTrue(os
.path
.exists(file_location
))
169 FILE
= file(file_location
)
171 self
.assertTrue(FILE
.read(), "reading from temporary file failed")
174 os
.unlink(file_location
)
176 def test_header(self
):
177 # Make sure header returned as 2nd value from urlretrieve is good.
178 file_location
, header
= self
.urlretrieve("http://www.python.org/")
179 os
.unlink(file_location
)
180 self
.assertIsInstance(header
, mimetools
.Message
,
181 "header is not an instance of mimetools.Message")
183 def test_data_header(self
):
184 logo
= "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
185 file_location
, fileheaders
= self
.urlretrieve(logo
)
186 os
.unlink(file_location
)
187 datevalue
= fileheaders
.getheader('Date')
188 dateformat
= '%a, %d %b %Y %H:%M:%S GMT'
190 time
.strptime(datevalue
, dateformat
)
192 self
.fail('Date value not in %r format', dateformat
)
197 test_support
.requires('network')
198 with test_support
.check_py3k_warnings(
199 ("urllib.urlopen.. has been removed", DeprecationWarning)):
200 test_support
.run_unittest(URLTimeoutTest
,
202 urlretrieveNetworkTests
)
204 if __name__
== "__main__":