1
0

test_rest_cherrypy.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 urlencode # pylint: disable=no-name-in-module,import-error
  9. # Import Salt libs
  10. from tests.support.cherrypy_testclasses import BaseToolsTest
  11. class TestOutFormats(BaseToolsTest):
  12. def __get_cp_config__(self):
  13. return {
  14. 'tools.hypermedia_out.on': True,
  15. }
  16. def test_default_accept(self):
  17. request, response = self.request('/')
  18. self.assertEqual(response.headers['Content-type'], 'application/json')
  19. def test_unsupported_accept(self):
  20. request, response = self.request('/', headers=(
  21. ('Accept', 'application/ms-word'),
  22. ))
  23. self.assertEqual(response.status, '406 Not Acceptable')
  24. def test_json_out(self):
  25. request, response = self.request('/', headers=(
  26. ('Accept', 'application/json'),
  27. ))
  28. self.assertEqual(response.headers['Content-type'], 'application/json')
  29. def test_yaml_out(self):
  30. request, response = self.request('/', headers=(
  31. ('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('/', method='POST',
  42. body=urlencode(data), headers=(
  43. ('Content-type', 'application/x-www-form-urlencoded'),
  44. ))
  45. self.assertEqual(response.status, '200 OK')
  46. self.assertDictEqual(request.unserialized_data, data)
  47. def test_json_ctype(self):
  48. data = {'valid': 'stuff'}
  49. request, response = self.request('/', method='POST',
  50. body=salt.utils.json.dumps(data), headers=(
  51. ('Content-type', 'application/json'),
  52. ))
  53. self.assertEqual(response.status, '200 OK')
  54. self.assertDictEqual(request.unserialized_data, data)
  55. def test_json_as_text_out(self):
  56. '''
  57. Some service send JSON as text/plain for compatibility purposes
  58. '''
  59. data = {'valid': 'stuff'}
  60. request, response = self.request('/', method='POST',
  61. body=salt.utils.json.dumps(data), headers=(
  62. ('Content-type', 'text/plain'),
  63. ))
  64. self.assertEqual(response.status, '200 OK')
  65. self.assertDictEqual(request.unserialized_data, data)
  66. def test_yaml_ctype(self):
  67. data = {'valid': 'stuff'}
  68. request, response = self.request('/', method='POST',
  69. body=salt.utils.yaml.safe_dump(data), headers=(
  70. ('Content-type', 'application/x-yaml'),
  71. ))
  72. self.assertEqual(response.status, '200 OK')
  73. self.assertDictEqual(request.unserialized_data, data)