test_http.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import socket
  8. from contextlib import closing
  9. # Import Salt Testing Libs
  10. from tests.support.unit import TestCase
  11. from tests.support.helpers import MirrorPostHandler, Webserver
  12. # Import Salt Libs
  13. import salt.utils.http as http
  14. class HTTPTestCase(TestCase):
  15. '''
  16. Unit TestCase for the salt.utils.http module.
  17. '''
  18. @classmethod
  19. def setUpClass(cls):
  20. cls.post_webserver = Webserver(handler=MirrorPostHandler)
  21. cls.post_webserver.start()
  22. cls.post_web_root = cls.post_webserver.web_root
  23. @classmethod
  24. def tearDownClass(cls):
  25. cls.post_webserver.stop()
  26. del cls.post_webserver
  27. # sanitize_url tests
  28. def test_sanitize_url_hide_fields_none(self):
  29. '''
  30. Tests sanitizing a url when the hide_fields kwarg is None.
  31. '''
  32. mock_url = 'https://api.testing.com/?&foo=bar&test=testing'
  33. ret = http.sanitize_url(mock_url, hide_fields=None)
  34. self.assertEqual(ret, mock_url)
  35. def test_sanitize_url_no_elements(self):
  36. '''
  37. Tests sanitizing a url when no elements should be sanitized.
  38. '''
  39. mock_url = 'https://api.testing.com/?&foo=bar&test=testing'
  40. ret = http.sanitize_url(mock_url, [''])
  41. self.assertEqual(ret, mock_url)
  42. def test_sanitize_url_single_element(self):
  43. '''
  44. Tests sanitizing a url with only a single element to be sanitized.
  45. '''
  46. mock_url = 'https://api.testing.com/?&keep_it_secret=abcdefghijklmn' \
  47. '&api_action=module.function'
  48. mock_ret = 'https://api.testing.com/?&keep_it_secret=XXXXXXXXXX&' \
  49. 'api_action=module.function'
  50. ret = http.sanitize_url(mock_url, ['keep_it_secret'])
  51. self.assertEqual(ret, mock_ret)
  52. def test_sanitize_url_multiple_elements(self):
  53. '''
  54. Tests sanitizing a url with multiple elements to be sanitized.
  55. '''
  56. mock_url = 'https://api.testing.com/?rootPass=badpassword%21' \
  57. '&skipChecks=True&api_key=abcdefghijklmn' \
  58. '&NodeID=12345&api_action=module.function'
  59. mock_ret = 'https://api.testing.com/?rootPass=XXXXXXXXXX' \
  60. '&skipChecks=True&api_key=XXXXXXXXXX' \
  61. '&NodeID=12345&api_action=module.function'
  62. ret = http.sanitize_url(mock_url, ['api_key', 'rootPass'])
  63. self.assertEqual(ret, mock_ret)
  64. # _sanitize_components tests
  65. def test_sanitize_components_no_elements(self):
  66. '''
  67. Tests when zero elements need to be sanitized.
  68. '''
  69. mock_component_list = ['foo=bar', 'bar=baz', 'hello=world']
  70. mock_ret = 'foo=bar&bar=baz&hello=world&'
  71. ret = http._sanitize_url_components(mock_component_list, 'api_key')
  72. self.assertEqual(ret, mock_ret)
  73. def test_sanitize_components_one_element(self):
  74. '''
  75. Tests a single component to be sanitized.
  76. '''
  77. mock_component_list = ['foo=bar', 'api_key=abcdefghijklmnop']
  78. mock_ret = 'foo=bar&api_key=XXXXXXXXXX&'
  79. ret = http._sanitize_url_components(mock_component_list, 'api_key')
  80. self.assertEqual(ret, mock_ret)
  81. def test_sanitize_components_multiple_elements(self):
  82. '''
  83. Tests two componenets to be sanitized.
  84. '''
  85. mock_component_list = ['foo=bar', 'foo=baz', 'api_key=testing']
  86. mock_ret = 'foo=XXXXXXXXXX&foo=XXXXXXXXXX&api_key=testing&'
  87. ret = http._sanitize_url_components(mock_component_list, 'foo')
  88. self.assertEqual(ret, mock_ret)
  89. def test_query_null_response(self):
  90. '''
  91. This tests that we get a null response when raise_error=False and the
  92. host/port cannot be reached.
  93. '''
  94. host = '127.0.0.1'
  95. # Find unused port
  96. with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  97. sock.bind((host, 0))
  98. port = sock.getsockname()[1]
  99. url = 'http://{host}:{port}/'.format(host=host, port=port)
  100. result = http.query(url, raise_error=False)
  101. assert result == {'body': None}, result
  102. def test_requests_multipart_formdata_post(self):
  103. '''
  104. Test handling of a multipart/form-data POST using the requests backend
  105. '''
  106. match_this = '{0}\r\nContent-Disposition: form-data; name="fieldname_here"\r\n\r\nmydatahere\r\n{0}--\r\n'
  107. ret = http.query(
  108. self.post_web_root,
  109. method='POST',
  110. data='mydatahere',
  111. formdata=True,
  112. formdata_fieldname='fieldname_here',
  113. backend='requests'
  114. )
  115. body = ret.get('body', '')
  116. boundary = body[:body.find('\r')]
  117. self.assertEqual(body, match_this.format(boundary))