Skip to content

Commit

Permalink
Merge "Return unicode for object in json and xml serializer"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Apr 2, 2013
2 parents 54f1e75 + 26dca9f commit 0deaadd
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 13 deletions.
4 changes: 2 additions & 2 deletions quantum/api/v2/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ def resource(request):
except (exceptions.QuantumException,
netaddr.AddrFormatError) as e:
LOG.exception(_('%s failed'), action)
body = serializer.serialize({'QuantumError': str(e)})
body = serializer.serialize({'QuantumError': e})
kwargs = {'body': body, 'content_type': content_type}
for fault in faults:
if isinstance(e, fault):
raise faults[fault](**kwargs)
raise webob.exc.HTTPInternalServerError(**kwargs)
except webob.exc.HTTPException as e:
LOG.exception(_('%s failed'), action)
e.body = serializer.serialize({'QuantumError': str(e)})
e.body = serializer.serialize({'QuantumError': e})
e.content_type = content_type
raise
except Exception as e:
Expand Down
94 changes: 85 additions & 9 deletions quantum/tests/unit/test_api_v2_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,81 @@ def test_context_without_quantum_context(self):


class ResourceTestCase(base.BaseTestCase):
def test_unmapped_quantum_error(self):
def test_unmapped_quantum_error_with_json(self):
msg = u'\u7f51\u7edc'

class TestException(q_exc.QuantumException):
message = msg
expected_res = {'body': {'QuantumError': msg}}
controller = mock.MagicMock()
controller.test.side_effect = q_exc.QuantumException()
controller.test.side_effect = TestException()

resource = webtest.TestApp(wsgi_resource.Resource(controller))

environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'json'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPInternalServerError.code)
self.assertEqual(wsgi.JSONDeserializer().deserialize(res.body),
expected_res)

def test_mapped_quantum_error(self):
def test_unmapped_quantum_error_with_xml(self):
msg = u'\u7f51\u7edc'

class TestException(q_exc.QuantumException):
message = msg
expected_res = {'body': {'QuantumError': msg}}
controller = mock.MagicMock()
controller.test.side_effect = q_exc.QuantumException()
controller.test.side_effect = TestException()

resource = webtest.TestApp(wsgi_resource.Resource(controller))

environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'xml'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPInternalServerError.code)
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
expected_res)

faults = {q_exc.QuantumException: exc.HTTPGatewayTimeout}
def test_mapped_quantum_error_with_json(self):
msg = u'\u7f51\u7edc'

class TestException(q_exc.QuantumException):
message = msg
expected_res = {'body': {'QuantumError': msg}}
controller = mock.MagicMock()
controller.test.side_effect = TestException()

faults = {TestException: exc.HTTPGatewayTimeout}
resource = webtest.TestApp(wsgi_resource.Resource(controller,
faults=faults))

environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'json'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)
self.assertEqual(wsgi.JSONDeserializer().deserialize(res.body),
expected_res)

def test_mapped_quantum_error_with_xml(self):
msg = u'\u7f51\u7edc'

class TestException(q_exc.QuantumException):
message = msg
expected_res = {'body': {'QuantumError': msg}}
controller = mock.MagicMock()
controller.test.side_effect = TestException()

faults = {TestException: exc.HTTPGatewayTimeout}
resource = webtest.TestApp(wsgi_resource.Resource(controller,
faults=faults))

environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'xml'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
expected_res)

def test_http_error(self):
controller = mock.MagicMock()
Expand All @@ -132,15 +186,37 @@ def test_http_error(self):
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)

def test_unhandled_error(self):
def test_unhandled_error_with_json(self):
expected_res = {'body': {'QuantumError':
_('Request Failed: internal server error '
'while processing your request.')}}
controller = mock.MagicMock()
controller.test.side_effect = Exception()

resource = webtest.TestApp(wsgi_resource.Resource(controller))

environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'json'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPInternalServerError.code)
self.assertEqual(wsgi.JSONDeserializer().deserialize(res.body),
expected_res)

def test_unhandled_error_with_xml(self):
expected_res = {'body': {'QuantumError':
_('Request Failed: internal server error '
'while processing your request.')}}
controller = mock.MagicMock()
controller.test.side_effect = Exception()

resource = webtest.TestApp(wsgi_resource.Resource(controller))

environ = {'wsgiorg.routing_args': (None, {'action': 'test',
'format': 'xml'})}
res = resource.get('', extra_environ=environ, expect_errors=True)
self.assertEqual(res.status_int, exc.HTTPInternalServerError.code)
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
expected_res)

def test_status_200(self):
controller = mock.MagicMock()
Expand Down
68 changes: 68 additions & 0 deletions quantum/tests/unit/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,24 @@ def test_json(self):

self.assertEqual(result, expected_json)

def test_json_with_utf8(self):
input_dict = dict(servers=dict(a=(2, '\xe7\xbd\x91\xe7\xbb\x9c')))
expected_json = '{"servers":{"a":[2,"\\u7f51\\u7edc"]}}'
serializer = wsgi.JSONDictSerializer()
result = serializer.serialize(input_dict)
result = result.replace('\n', '').replace(' ', '')

self.assertEqual(result, expected_json)

def test_json_with_unicode(self):
input_dict = dict(servers=dict(a=(2, u'\u7f51\u7edc')))
expected_json = '{"servers":{"a":[2,"\\u7f51\\u7edc"]}}'
serializer = wsgi.JSONDictSerializer()
result = serializer.serialize(input_dict)
result = result.replace('\n', '').replace(' ', '')

self.assertEqual(result, expected_json)


class TextDeserializerTest(base.BaseTestCase):

Expand Down Expand Up @@ -654,6 +672,20 @@ def test_default_raise_Malformed_Exception(self):
self.assertRaises(
exception.MalformedRequestBody, deserializer.default, data_string)

def test_json_with_utf8(self):
data = '{"a": "\xe7\xbd\x91\xe7\xbb\x9c"}'
as_dict = {'body': {'a': u'\u7f51\u7edc'}}
deserializer = wsgi.JSONDeserializer()
self.assertEqual(
deserializer.deserialize(data), as_dict)

def test_json_with_unicode(self):
data = '{"a": "\u7f51\u7edc"}'
as_dict = {'body': {'a': u'\u7f51\u7edc'}}
deserializer = wsgi.JSONDeserializer()
self.assertEqual(
deserializer.deserialize(data), as_dict)


class XMLDeserializerTest(base.BaseTestCase):
def test_xml_empty(self):
Expand Down Expand Up @@ -681,6 +713,14 @@ def test_default_raise_Malformed_Exception(self):
self.assertRaises(
exception.MalformedRequestBody, deserializer.default, data_string)

def test_xml_with_utf8(self):
xml = '<a>\xe7\xbd\x91\xe7\xbb\x9c</a>'
as_dict = {'body': {'a': u'\u7f51\u7edc'}}
deserializer = wsgi.XMLDeserializer()

self.assertEqual(
deserializer.deserialize(xml), as_dict)


class RequestHeadersDeserializerTest(base.BaseTestCase):

Expand Down Expand Up @@ -1044,6 +1084,34 @@ def test_call(self):
result = result.replace('\n', '').replace(' ', '')
self.assertEqual(expected, result)

def test_xml_with_utf8(self):
data = {'servers': '\xe7\xbd\x91\xe7\xbb\x9c'}
serializer = wsgi.XMLDictSerializer()
expected = (
'<?xmlversion=\'1.0\'encoding=\'UTF-8\'?>'
'<serversxmlns="http://openstack.org/quantum/api/v2.0"'
'xmlns:quantum="http://openstack.org/quantum/api/v2.0"'
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
'\xe7\xbd\x91\xe7\xbb\x9c</servers>'
)
result = serializer(data)
result = result.replace('\n', '').replace(' ', '')
self.assertEqual(expected, result)

def test_xml_with_unicode(self):
data = {'servers': u'\u7f51\u7edc'}
serializer = wsgi.XMLDictSerializer()
expected = (
'<?xmlversion=\'1.0\'encoding=\'UTF-8\'?>'
'<serversxmlns="http://openstack.org/quantum/api/v2.0"'
'xmlns:quantum="http://openstack.org/quantum/api/v2.0"'
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
'\xe7\xbd\x91\xe7\xbb\x9c</servers>'
)
result = serializer(data)
result = result.replace('\n', '').replace(' ', '')
self.assertEqual(expected, result)


class TestWSGIServerWithSSL(base.BaseTestCase):
"""WSGI server tests."""
Expand Down
9 changes: 7 additions & 2 deletions quantum/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ class JSONDictSerializer(DictSerializer):
"""Default JSON request body serialization"""

def default(self, data):
return jsonutils.dumps(data)
def sanitizer(obj):
return unicode(obj)
return jsonutils.dumps(data, default=sanitizer)


class XMLDictSerializer(DictSerializer):
Expand Down Expand Up @@ -467,7 +469,10 @@ def _to_xml_node(self, parent, metadata, nodename, data, used_prefixes):
LOG.debug(_("Data %(data)s type is %(type)s"),
{'data': data,
'type': type(data)})
result.text = str(data)
if isinstance(data, str):
result.text = unicode(data, 'utf-8')
else:
result.text = unicode(data)
return result

def _create_link_nodes(self, xml_doc, links):
Expand Down

0 comments on commit 0deaadd

Please sign in to comment.