test_urllibnet.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import unittest
  2. from test import test_support
  3. import socket
  4. import urllib
  5. import sys
  6. import os
  7. import time
  8. try:
  9. import ssl
  10. except ImportError:
  11. ssl = None
  12. here = os.path.dirname(__file__)
  13. # Self-signed cert file for self-signed.pythontest.net
  14. CERT_selfsigned_pythontestdotnet = os.path.join(here, 'selfsigned_pythontestdotnet.pem')
  15. mimetools = test_support.import_module("mimetools", deprecated=True)
  16. def _open_with_retry(func, host, *args, **kwargs):
  17. # Connecting to remote hosts is flaky. Make it more robust
  18. # by retrying the connection several times.
  19. for i in range(3):
  20. try:
  21. return func(host, *args, **kwargs)
  22. except IOError, last_exc:
  23. continue
  24. except:
  25. raise
  26. raise last_exc
  27. class URLTimeoutTest(unittest.TestCase):
  28. TIMEOUT = 10.0
  29. def setUp(self):
  30. socket.setdefaulttimeout(self.TIMEOUT)
  31. def tearDown(self):
  32. socket.setdefaulttimeout(None)
  33. def testURLread(self):
  34. f = _open_with_retry(urllib.urlopen, "http://www.example.com/")
  35. x = f.read()
  36. class urlopenNetworkTests(unittest.TestCase):
  37. """Tests urllib.urlopen using the network.
  38. These tests are not exhaustive. Assuming that testing using files does a
  39. good job overall of some of the basic interface features. There are no
  40. tests exercising the optional 'data' and 'proxies' arguments. No tests
  41. for transparent redirection have been written.
  42. setUp is not used for always constructing a connection to
  43. http://www.example.com/ since there a few tests that don't use that address
  44. and making a connection is expensive enough to warrant minimizing unneeded
  45. connections.
  46. """
  47. def urlopen(self, *args):
  48. return _open_with_retry(urllib.urlopen, *args)
  49. def test_basic(self):
  50. # Simple test expected to pass.
  51. open_url = self.urlopen("http://www.example.com/")
  52. for attr in ("read", "readline", "readlines", "fileno", "close",
  53. "info", "geturl"):
  54. self.assertTrue(hasattr(open_url, attr), "object returned from "
  55. "urlopen lacks the %s attribute" % attr)
  56. try:
  57. self.assertTrue(open_url.read(), "calling 'read' failed")
  58. finally:
  59. open_url.close()
  60. def test_readlines(self):
  61. # Test both readline and readlines.
  62. open_url = self.urlopen("http://www.example.com/")
  63. try:
  64. self.assertIsInstance(open_url.readline(), basestring,
  65. "readline did not return a string")
  66. self.assertIsInstance(open_url.readlines(), list,
  67. "readlines did not return a list")
  68. finally:
  69. open_url.close()
  70. def test_info(self):
  71. # Test 'info'.
  72. open_url = self.urlopen("http://www.example.com/")
  73. try:
  74. info_obj = open_url.info()
  75. finally:
  76. open_url.close()
  77. self.assertIsInstance(info_obj, mimetools.Message,
  78. "object returned by 'info' is not an "
  79. "instance of mimetools.Message")
  80. self.assertEqual(info_obj.getsubtype(), "html")
  81. def test_geturl(self):
  82. # Make sure same URL as opened is returned by geturl.
  83. URL = "http://www.example.com/"
  84. open_url = self.urlopen(URL)
  85. try:
  86. gotten_url = open_url.geturl()
  87. finally:
  88. open_url.close()
  89. self.assertEqual(gotten_url, URL)
  90. def test_getcode(self):
  91. # test getcode() with the fancy opener to get 404 error codes
  92. URL = "http://www.pythontest.net/XXXinvalidXXX"
  93. open_url = urllib.FancyURLopener().open(URL)
  94. try:
  95. code = open_url.getcode()
  96. finally:
  97. open_url.close()
  98. self.assertEqual(code, 404)
  99. @unittest.skipIf(sys.platform in ('win32',), 'not appropriate for Windows')
  100. @unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available')
  101. def test_fileno(self):
  102. # Make sure fd returned by fileno is valid.
  103. open_url = self.urlopen("http://www.example.com/")
  104. fd = open_url.fileno()
  105. FILE = os.fdopen(fd)
  106. try:
  107. self.assertTrue(FILE.read(), "reading from file created using fd "
  108. "returned by fileno failed")
  109. finally:
  110. FILE.close()
  111. def test_bad_address(self):
  112. # Make sure proper exception is raised when connecting to a bogus
  113. # address.
  114. bogus_domain = "sadflkjsasf.i.nvali.d"
  115. try:
  116. socket.gethostbyname(bogus_domain)
  117. except socket.gaierror:
  118. pass
  119. else:
  120. # This happens with some overzealous DNS providers such as OpenDNS
  121. self.skipTest("%r should not resolve for test to work" % bogus_domain)
  122. self.assertRaises(IOError,
  123. # SF patch 809915: In Sep 2003, VeriSign started
  124. # highjacking invalid .com and .net addresses to
  125. # boost traffic to their own site. This test
  126. # started failing then. One hopes the .invalid
  127. # domain will be spared to serve its defined
  128. # purpose.
  129. # urllib.urlopen, "http://www.sadflkjsasadf.com/")
  130. urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")
  131. class urlretrieveNetworkTests(unittest.TestCase):
  132. """Tests urllib.urlretrieve using the network."""
  133. def urlretrieve(self, *args):
  134. return _open_with_retry(urllib.urlretrieve, *args)
  135. def test_basic(self):
  136. # Test basic functionality.
  137. file_location,info = self.urlretrieve("http://www.example.com/")
  138. self.assertTrue(os.path.exists(file_location), "file location returned by"
  139. " urlretrieve is not a valid path")
  140. FILE = file(file_location)
  141. try:
  142. self.assertTrue(FILE.read(), "reading from the file location returned"
  143. " by urlretrieve failed")
  144. finally:
  145. FILE.close()
  146. os.unlink(file_location)
  147. def test_specified_path(self):
  148. # Make sure that specifying the location of the file to write to works.
  149. file_location,info = self.urlretrieve("http://www.example.com/",
  150. test_support.TESTFN)
  151. self.assertEqual(file_location, test_support.TESTFN)
  152. self.assertTrue(os.path.exists(file_location))
  153. FILE = file(file_location)
  154. try:
  155. self.assertTrue(FILE.read(), "reading from temporary file failed")
  156. finally:
  157. FILE.close()
  158. os.unlink(file_location)
  159. def test_header(self):
  160. # Make sure header returned as 2nd value from urlretrieve is good.
  161. file_location, header = self.urlretrieve("http://www.example.com/")
  162. os.unlink(file_location)
  163. self.assertIsInstance(header, mimetools.Message,
  164. "header is not an instance of mimetools.Message")
  165. def test_data_header(self):
  166. logo = "http://www.example.com/"
  167. file_location, fileheaders = self.urlretrieve(logo)
  168. os.unlink(file_location)
  169. datevalue = fileheaders.getheader('Date')
  170. dateformat = '%a, %d %b %Y %H:%M:%S GMT'
  171. try:
  172. time.strptime(datevalue, dateformat)
  173. except ValueError:
  174. self.fail('Date value not in %r format', dateformat)
  175. @unittest.skipIf(ssl is None, "requires ssl")
  176. class urlopen_HttpsTests(unittest.TestCase):
  177. def test_context_argument(self):
  178. context = ssl.create_default_context(cafile=CERT_selfsigned_pythontestdotnet)
  179. response = urllib.urlopen("https://self-signed.pythontest.net", context=context)
  180. self.assertIn("Python", response.read())
  181. def test_main():
  182. test_support.requires('network')
  183. with test_support.check_py3k_warnings(
  184. ("urllib.urlopen.. has been removed", DeprecationWarning)):
  185. test_support.run_unittest(URLTimeoutTest,
  186. urlopenNetworkTests,
  187. urlretrieveNetworkTests,
  188. urlopen_HttpsTests)
  189. if __name__ == "__main__":
  190. test_main()