1
0

test_rest_cherrypy.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # coding: utf-8
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. # Import Salt libs
  5. import salt.utils.json
  6. import salt.utils.yaml
  7. # Import 3rd-party libs
  8. from salt.ext.six.moves.urllib.parse import ( # pylint: disable=no-name-in-module,import-error
  9. urlencode,
  10. )
  11. # Import Salt libs
  12. from tests.support.cherrypy_testclasses import BaseToolsTest
  13. class TestOutFormats(BaseToolsTest):
  14. def __get_cp_config__(self):
  15. return {
  16. "tools.hypermedia_out.on": True,
  17. }
  18. def test_default_accept(self):
  19. request, response = self.request("/")
  20. self.assertEqual(response.headers["Content-type"], "application/json")
  21. def test_unsupported_accept(self):
  22. request, response = self.request(
  23. "/", headers=(("Accept", "application/ms-word"),)
  24. )
  25. self.assertEqual(response.status, "406 Not Acceptable")
  26. def test_json_out(self):
  27. request, response = self.request("/", headers=(("Accept", "application/json"),))
  28. self.assertEqual(response.headers["Content-type"], "application/json")
  29. def test_yaml_out(self):
  30. request, response = self.request(
  31. "/", headers=(("Accept", "application/x-yaml"),)
  32. )
  33. self.assertEqual(response.headers["Content-type"], "application/x-yaml")
  34. class TestInFormats(BaseToolsTest):
  35. def __get_cp_config__(self):
  36. return {
  37. "tools.hypermedia_in.on": True,
  38. }
  39. def test_urlencoded_ctype(self):
  40. data = {"valid": "stuff"}
  41. request, response = self.request(
  42. "/",
  43. method="POST",
  44. body=urlencode(data),
  45. headers=(("Content-type", "application/x-www-form-urlencoded"),),
  46. )
  47. self.assertEqual(response.status, "200 OK")
  48. self.assertDictEqual(request.unserialized_data, data)
  49. def test_json_ctype(self):
  50. data = {"valid": "stuff"}
  51. request, response = self.request(
  52. "/",
  53. method="POST",
  54. body=salt.utils.json.dumps(data),
  55. headers=(("Content-type", "application/json"),),
  56. )
  57. self.assertEqual(response.status, "200 OK")
  58. self.assertDictEqual(request.unserialized_data, data)
  59. def test_json_as_text_out(self):
  60. """
  61. Some service send JSON as text/plain for compatibility purposes
  62. """
  63. data = {"valid": "stuff"}
  64. request, response = self.request(
  65. "/",
  66. method="POST",
  67. body=salt.utils.json.dumps(data),
  68. headers=(("Content-type", "text/plain"),),
  69. )
  70. self.assertEqual(response.status, "200 OK")
  71. self.assertDictEqual(request.unserialized_data, data)
  72. def test_yaml_ctype(self):
  73. data = {"valid": "stuff"}
  74. request, response = self.request(
  75. "/",
  76. method="POST",
  77. body=salt.utils.yaml.safe_dump(data),
  78. headers=(("Content-type", "application/x-yaml"),),
  79. )
  80. self.assertEqual(response.status, "200 OK")
  81. self.assertDictEqual(request.unserialized_data, data)
  82. class TestCors(BaseToolsTest):
  83. def __get_cp_config__(self):
  84. return {
  85. "tools.cors_tool.on": True,
  86. }
  87. def test_option_request(self):
  88. request, response = self.request(
  89. "/", method="OPTIONS", headers=(("Origin", "https://domain.com"),)
  90. )
  91. self.assertEqual(response.status, "200 OK")
  92. self.assertEqual(
  93. response.headers.get("Access-Control-Allow-Origin"), "https://domain.com"
  94. )