test_http.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import Salt Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.unit import TestCase
  9. # Import Salt Libs
  10. import salt.utils.http as http
  11. class HTTPTestCase(TestCase):
  12. '''
  13. Unit TestCase for the salt.utils.http module.
  14. '''
  15. # sanitize_url tests
  16. def test_sanitize_url_hide_fields_none(self):
  17. '''
  18. Tests sanitizing a url when the hide_fields kwarg is None.
  19. '''
  20. mock_url = 'https://api.testing.com/?&foo=bar&test=testing'
  21. ret = http.sanitize_url(mock_url, hide_fields=None)
  22. self.assertEqual(ret, mock_url)
  23. def test_sanitize_url_no_elements(self):
  24. '''
  25. Tests sanitizing a url when no elements should be sanitized.
  26. '''
  27. mock_url = 'https://api.testing.com/?&foo=bar&test=testing'
  28. ret = http.sanitize_url(mock_url, [''])
  29. self.assertEqual(ret, mock_url)
  30. def test_sanitize_url_single_element(self):
  31. '''
  32. Tests sanitizing a url with only a single element to be sanitized.
  33. '''
  34. mock_url = 'https://api.testing.com/?&keep_it_secret=abcdefghijklmn' \
  35. '&api_action=module.function'
  36. mock_ret = 'https://api.testing.com/?&keep_it_secret=XXXXXXXXXX&' \
  37. 'api_action=module.function'
  38. ret = http.sanitize_url(mock_url, ['keep_it_secret'])
  39. self.assertEqual(ret, mock_ret)
  40. def test_sanitize_url_multiple_elements(self):
  41. '''
  42. Tests sanitizing a url with multiple elements to be sanitized.
  43. '''
  44. mock_url = 'https://api.testing.com/?rootPass=badpassword%21' \
  45. '&skipChecks=True&api_key=abcdefghijklmn' \
  46. '&NodeID=12345&api_action=module.function'
  47. mock_ret = 'https://api.testing.com/?rootPass=XXXXXXXXXX' \
  48. '&skipChecks=True&api_key=XXXXXXXXXX' \
  49. '&NodeID=12345&api_action=module.function'
  50. ret = http.sanitize_url(mock_url, ['api_key', 'rootPass'])
  51. self.assertEqual(ret, mock_ret)
  52. # _sanitize_components tests
  53. def test_sanitize_components_no_elements(self):
  54. '''
  55. Tests when zero elements need to be sanitized.
  56. '''
  57. mock_component_list = ['foo=bar', 'bar=baz', 'hello=world']
  58. mock_ret = 'foo=bar&bar=baz&hello=world&'
  59. ret = http._sanitize_url_components(mock_component_list, 'api_key')
  60. self.assertEqual(ret, mock_ret)
  61. def test_sanitize_components_one_element(self):
  62. '''
  63. Tests a single component to be sanitized.
  64. '''
  65. mock_component_list = ['foo=bar', 'api_key=abcdefghijklmnop']
  66. mock_ret = 'foo=bar&api_key=XXXXXXXXXX&'
  67. ret = http._sanitize_url_components(mock_component_list, 'api_key')
  68. self.assertEqual(ret, mock_ret)
  69. def test_sanitize_components_multiple_elements(self):
  70. '''
  71. Tests two componenets to be sanitized.
  72. '''
  73. mock_component_list = ['foo=bar', 'foo=baz', 'api_key=testing']
  74. mock_ret = 'foo=XXXXXXXXXX&foo=XXXXXXXXXX&api_key=testing&'
  75. ret = http._sanitize_url_components(mock_component_list, 'foo')
  76. self.assertEqual(ret, mock_ret)