test_utils.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """unit tests module for ndg.httpsclient.utils module
  2. PyOpenSSL utility to make a httplib-like interface suitable for use with
  3. urllib2
  4. """
  5. __author__ = "P J Kershaw (STFC)"
  6. __date__ = "06/01/12"
  7. __copyright__ = "(C) 2012 Science and Technology Facilities Council"
  8. __license__ = "BSD - see LICENSE file in top-level directory"
  9. __contact__ = "Philip.Kershaw@stfc.ac.uk"
  10. __revision__ = '$Id$'
  11. import unittest
  12. import os
  13. from OpenSSL import SSL
  14. from ndg.httpsclient.test import Constants
  15. from ndg.httpsclient.utils import (Configuration, fetch_from_url, open_url,
  16. _should_use_proxy)
  17. class TestUtilsModule(unittest.TestCase):
  18. '''Test ndg.httpsclient.utils module'''
  19. def test01_configuration(self):
  20. config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
  21. self.assert_(config.ssl_context)
  22. self.assertEquals(config.debug, True)
  23. def test02_fetch_from_url(self):
  24. config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
  25. res = fetch_from_url(Constants.TEST_URI, config)
  26. self.assert_(res)
  27. def test03_open_url(self):
  28. config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
  29. res = open_url(Constants.TEST_URI, config)
  30. self.assertEqual(res[0], 200,
  31. 'open_url for %r failed' % Constants.TEST_URI)
  32. def test04__should_use_proxy(self):
  33. if 'no_proxy' in os.environ:
  34. no_proxy = os.environ['no_proxy']
  35. del os.environ['no_proxy']
  36. else:
  37. no_proxy = None
  38. self.assertTrue(_should_use_proxy(Constants.TEST_URI),
  39. 'Expecting use proxy = True')
  40. os.environ['no_proxy'] = 'localhost,localhost.localdomain'
  41. self.assertFalse(_should_use_proxy(Constants.TEST_URI),
  42. 'Expecting use proxy = False')
  43. if no_proxy is not None:
  44. os.environ['no_proxy'] = no_proxy
  45. else:
  46. del os.environ['no_proxy']
  47. if __name__ == "__main__":
  48. unittest.main()