PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_urllibnet.py

https://bitbucket.org/glix/python
Python | 194 lines | 168 code | 12 blank | 14 comment | 4 complexity | 815baf1a83ab37d1d8b3d0bb6b75d31e MD5 | raw file
  1. #!/usr/bin/env python
  2. import unittest
  3. from test import test_support
  4. import socket
  5. import urllib
  6. import sys
  7. import os
  8. import mimetools
  9. def _open_with_retry(func, host, *args, **kwargs):
  10. # Connecting to remote hosts is flaky. Make it more robust
  11. # by retrying the connection several times.
  12. for i in range(3):
  13. try:
  14. return func(host, *args, **kwargs)
  15. except IOError, last_exc:
  16. continue
  17. except:
  18. raise
  19. raise last_exc
  20. class URLTimeoutTest(unittest.TestCase):
  21. TIMEOUT = 10.0
  22. def setUp(self):
  23. socket.setdefaulttimeout(self.TIMEOUT)
  24. def tearDown(self):
  25. socket.setdefaulttimeout(None)
  26. def testURLread(self):
  27. f = _open_with_retry(urllib.urlopen, "http://www.python.org/")
  28. x = f.read()
  29. class urlopenNetworkTests(unittest.TestCase):
  30. """Tests urllib.urlopen using the network.
  31. These tests are not exhaustive. Assuming that testing using files does a
  32. good job overall of some of the basic interface features. There are no
  33. tests exercising the optional 'data' and 'proxies' arguments. No tests
  34. for transparent redirection have been written.
  35. setUp is not used for always constructing a connection to
  36. http://www.python.org/ since there a few tests that don't use that address
  37. and making a connection is expensive enough to warrant minimizing unneeded
  38. connections.
  39. """
  40. def urlopen(self, *args):
  41. return _open_with_retry(urllib.urlopen, *args)
  42. def test_basic(self):
  43. # Simple test expected to pass.
  44. open_url = self.urlopen("http://www.python.org/")
  45. for attr in ("read", "readline", "readlines", "fileno", "close",
  46. "info", "geturl"):
  47. self.assert_(hasattr(open_url, attr), "object returned from "
  48. "urlopen lacks the %s attribute" % attr)
  49. try:
  50. self.assert_(open_url.read(), "calling 'read' failed")
  51. finally:
  52. open_url.close()
  53. def test_readlines(self):
  54. # Test both readline and readlines.
  55. open_url = self.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. def test_info(self):
  64. # Test 'info'.
  65. open_url = self.urlopen("http://www.python.org/")
  66. try:
  67. info_obj = open_url.info()
  68. finally:
  69. open_url.close()
  70. self.assert_(isinstance(info_obj, mimetools.Message),
  71. "object returned by 'info' is not an instance of "
  72. "mimetools.Message")
  73. self.assertEqual(info_obj.getsubtype(), "html")
  74. def test_geturl(self):
  75. # Make sure same URL as opened is returned by geturl.
  76. URL = "http://www.python.org/"
  77. open_url = self.urlopen(URL)
  78. try:
  79. gotten_url = open_url.geturl()
  80. finally:
  81. open_url.close()
  82. self.assertEqual(gotten_url, URL)
  83. def test_getcode(self):
  84. # test getcode() with the fancy opener to get 404 error codes
  85. URL = "http://www.python.org/XXXinvalidXXX"
  86. open_url = urllib.FancyURLopener().open(URL)
  87. try:
  88. code = open_url.getcode()
  89. finally:
  90. open_url.close()
  91. self.assertEqual(code, 404)
  92. def test_fileno(self):
  93. if (sys.platform in ('win32',) or
  94. not hasattr(os, 'fdopen')):
  95. # On Windows, socket handles are not file descriptors; this
  96. # test can't pass on Windows.
  97. return
  98. # Make sure fd returned by fileno is valid.
  99. open_url = self.urlopen("http://www.python.org/")
  100. fd = open_url.fileno()
  101. FILE = os.fdopen(fd)
  102. try:
  103. self.assert_(FILE.read(), "reading from file created using fd "
  104. "returned by fileno failed")
  105. finally:
  106. FILE.close()
  107. def test_bad_address(self):
  108. # Make sure proper exception is raised when connecting to a bogus
  109. # address.
  110. self.assertRaises(IOError,
  111. # SF patch 809915: In Sep 2003, VeriSign started
  112. # highjacking invalid .com and .net addresses to
  113. # boost traffic to their own site. This test
  114. # started failing then. One hopes the .invalid
  115. # domain will be spared to serve its defined
  116. # purpose.
  117. # urllib.urlopen, "http://www.sadflkjsasadf.com/")
  118. urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")
  119. class urlretrieveNetworkTests(unittest.TestCase):
  120. """Tests urllib.urlretrieve using the network."""
  121. def urlretrieve(self, *args):
  122. return _open_with_retry(urllib.urlretrieve, *args)
  123. def test_basic(self):
  124. # Test basic functionality.
  125. file_location,info = self.urlretrieve("http://www.python.org/")
  126. self.assert_(os.path.exists(file_location), "file location returned by"
  127. " urlretrieve is not a valid path")
  128. FILE = file(file_location)
  129. try:
  130. self.assert_(FILE.read(), "reading from the file location returned"
  131. " by urlretrieve failed")
  132. finally:
  133. FILE.close()
  134. os.unlink(file_location)
  135. def test_specified_path(self):
  136. # Make sure that specifying the location of the file to write to works.
  137. file_location,info = self.urlretrieve("http://www.python.org/",
  138. test_support.TESTFN)
  139. self.assertEqual(file_location, test_support.TESTFN)
  140. self.assert_(os.path.exists(file_location))
  141. FILE = file(file_location)
  142. try:
  143. self.assert_(FILE.read(), "reading from temporary file failed")
  144. finally:
  145. FILE.close()
  146. os.unlink(file_location)
  147. def test_header(self):
  148. # Make sure header returned as 2nd value from urlretrieve is good.
  149. file_location, header = self.urlretrieve("http://www.python.org/")
  150. os.unlink(file_location)
  151. self.assert_(isinstance(header, mimetools.Message),
  152. "header is not an instance of mimetools.Message")
  153. def test_main():
  154. test_support.requires('network')
  155. from warnings import filterwarnings, catch_warnings
  156. with catch_warnings():
  157. filterwarnings('ignore', '.*urllib\.urlopen.*Python 3.0',
  158. DeprecationWarning)
  159. test_support.run_unittest(URLTimeoutTest,
  160. urlopenNetworkTests,
  161. urlretrieveNetworkTests)
  162. if __name__ == "__main__":
  163. test_main()