salt/tests/unit/netapi/rest_cherrypy/test_tools.py
Erik Johnson 002aa88a97
Replace yaml usage with a helper to ensure unicode is handled properly
Without allow_unicode=True, unicode characters are processed through the
str representer and on Python 2 are dumped as a Unicode code point (i.e.
a literal \u0414). This commit makes allow_unicode=True the default in
our salt.utils.yamlloader.safe_dump() helper. It also adds a new
salt.utils.yamlloader.dump() helper which wraps yaml.dump() and also
makes allow_unicode=True the default.

To make importing and using our custom yaml loader/dumper easier, a
convenience module called salt.utils.yaml has been added, which does a
wildcard import from both salt.utils.yamldumper and
salt.utils.yamlloader.

Refs to yaml.load/dump and yaml.safe_load/safe_dump have been updated to
salt.utils.yaml, to ensure that unicode is handled properly.
2018-01-03 14:14:21 -06:00

90 lines
3.0 KiB
Python

# coding: utf-8
# Import Python libs
from __future__ import absolute_import
# Import Salt libs
import salt.utils.json
import salt.utils.yaml
# Import 3rd-party libs
from salt.ext.six.moves.urllib.parse import urlencode # pylint: disable=no-name-in-module,import-error
# Import Salt libs
from tests.support.cherrypy_testclasses import BaseToolsTest
class TestOutFormats(BaseToolsTest):
def __get_cp_config__(self):
return {
'tools.hypermedia_out.on': True,
}
def test_default_accept(self):
request, response = self.request('/')
self.assertEqual(response.headers['Content-type'], 'application/json')
def test_unsupported_accept(self):
request, response = self.request('/', headers=(
('Accept', 'application/ms-word'),
))
self.assertEqual(response.status, '406 Not Acceptable')
def test_json_out(self):
request, response = self.request('/', headers=(
('Accept', 'application/json'),
))
self.assertEqual(response.headers['Content-type'], 'application/json')
def test_yaml_out(self):
request, response = self.request('/', headers=(
('Accept', 'application/x-yaml'),
))
self.assertEqual(response.headers['Content-type'], 'application/x-yaml')
class TestInFormats(BaseToolsTest):
def __get_cp_config__(self):
return {
'tools.hypermedia_in.on': True,
}
def test_urlencoded_ctype(self):
data = {'valid': 'stuff'}
request, response = self.request('/', method='POST',
body=urlencode(data), headers=(
('Content-type', 'application/x-www-form-urlencoded'),
))
self.assertEqual(response.status, '200 OK')
self.assertDictEqual(request.unserialized_data, data)
def test_json_ctype(self):
data = {'valid': 'stuff'}
request, response = self.request('/', method='POST',
body=salt.utils.json.dumps(data), headers=(
('Content-type', 'application/json'),
))
self.assertEqual(response.status, '200 OK')
self.assertDictEqual(request.unserialized_data, data)
def test_json_as_text_out(self):
'''
Some service send JSON as text/plain for compatibility purposes
'''
data = {'valid': 'stuff'}
request, response = self.request('/', method='POST',
body=salt.utils.json.dumps(data), headers=(
('Content-type', 'text/plain'),
))
self.assertEqual(response.status, '200 OK')
self.assertDictEqual(request.unserialized_data, data)
def test_yaml_ctype(self):
data = {'valid': 'stuff'}
request, response = self.request('/', method='POST',
body=salt.utils.yaml.safe_dump(data), headers=(
('Content-type', 'application/x-yaml'),
))
self.assertEqual(response.status, '200 OK')
self.assertDictEqual(request.unserialized_data, data)