1
0

test_http.py 3.3 KB

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