1
0

test_rest_cherrypy.py 3.4 KB

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