From 781815b3b27cc3fcbf153db57deb9bd82ae1d8d3 Mon Sep 17 00:00:00 2001 From: Rohan Rhishikesh Kanade Date: Fri, 17 May 2013 04:15:26 -0700 Subject: Fixes encoding issues for nova api req body. * Ensure utf-8 encoding for POST/PUT body for api requests * As per https://wiki.openstack.org/wiki/Encoding. Fixes bug #1180344 Change-Id: Iea0f16b0c024795fb3aee1cd38bb54ee9da6d90d --- nova/api/openstack/wsgi.py | 3 +- nova/tests/api/openstack/test_wsgi.py | 69 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 9c5f9855d..9d2f1846c 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -917,7 +917,8 @@ class Resource(wsgi.Application): if body: msg = _("Action: '%(action)s', body: " - "%(body)s") % {'action': action, 'body': body} + "%(body)s") % {'action': action, + 'body': unicode(body, 'utf-8')} LOG.debug(msg) LOG.debug(_("Calling method %s") % meth) diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py index a19204dff..4c10ec6b6 100644 --- a/nova/tests/api/openstack/test_wsgi.py +++ b/nova/tests/api/openstack/test_wsgi.py @@ -171,6 +171,35 @@ class JSONDeserializerTest(test.TestCase): deserializer = wsgi.JSONDeserializer() self.assertEqual(deserializer.deserialize(data), as_dict) + def test_json_valid_utf8(self): + data = """{"server": {"min_count": 1, "flavorRef": "1", + "name": "\xe6\xa6\x82\xe5\xbf\xb5", + "imageRef": "10bab10c-1304-47d", + "max_count": 1}} """ + as_dict = { + 'body': { + u'server': { + u'min_count': 1, u'flavorRef': u'1', + u'name': u'\u6982\u5ff5', + u'imageRef': u'10bab10c-1304-47d', + u'max_count': 1 + } + } + } + deserializer = wsgi.JSONDeserializer() + self.assertEqual(deserializer.deserialize(data), as_dict) + + def test_json_invalid_utf8(self): + """ Send invalid utf-8 to JSONDeserializer""" + data = """{"server": {"min_count": 1, "flavorRef": "1", + "name": "\xf0\x28\x8c\x28", + "imageRef": "10bab10c-1304-47d", + "max_count": 1}} """ + + deserializer = wsgi.JSONDeserializer() + self.assertRaises(exception.MalformedRequestBody, + deserializer.deserialize, data) + class XMLDeserializerTest(test.TestCase): def test_xml(self): @@ -202,6 +231,19 @@ class XMLDeserializerTest(test.TestCase): deserializer = wsgi.XMLDeserializer() self.assertEqual(deserializer.deserialize(xml), as_dict) + def test_xml_valid_utf8(self): + xml = """ \xe6\xa6\x82\xe5\xbf\xb5 """ + deserializer = wsgi.XMLDeserializer() + as_dict = {'body': {u'a': {u'name': u'\u6982\u5ff5'}}} + self.assertEqual(deserializer.deserialize(xml), as_dict) + + def test_xml_invalid_utf8(self): + """ Send invalid utf-8 to XMLDeserializer""" + xml = """ \xf0\x28\x8c\x28 """ + deserializer = wsgi.XMLDeserializer() + self.assertRaises(exception.MalformedRequestBody, + deserializer.deserialize, xml) + class ResourceTest(test.TestCase): def test_resource_call(self): @@ -780,6 +822,33 @@ class ResourceTest(test.TestCase): except wsgi.Fault as fault: self.assertEqual(400, fault.status_int) + def test_resource_valid_utf8_body(self): + class Controller(object): + def index(self, req, body): + return body + + req = webob.Request.blank('/tests') + body = """ {"name": "\xe6\xa6\x82\xe5\xbf\xb5" } """ + expected_body = '{"name": "\\u6982\\u5ff5"}' + req.body = body + req.headers['Content-Type'] = 'application/json' + app = fakes.TestRouter(Controller()) + response = req.get_response(app) + self.assertEqual(response.body, expected_body) + self.assertEqual(response.status_int, 200) + + def test_resource_invalid_utf8(self): + class Controller(object): + def index(self, req, body): + return body + + req = webob.Request.blank('/tests') + body = """ {"name": "\xf0\x28\x8c\x28" } """ + req.body = body + req.headers['Content-Type'] = 'application/json' + app = fakes.TestRouter(Controller()) + self.assertRaises(UnicodeDecodeError, req.get_response, app) + class ResponseObjectTest(test.TestCase): def test_default_code(self): -- cgit