diff options
| author | Jenkins <jenkins@review.openstack.org> | 2013-05-29 09:15:45 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2013-05-29 09:15:45 +0000 |
| commit | 8a92af1e84ee7d6f623f677263f9fa5b9487253d (patch) | |
| tree | 4fe935504ce2dcf8aa599eb66f4d4bb4684cf45a | |
| parent | 64b234d5cb69fbfd183d74f20d7b243569f6c6f6 (diff) | |
| parent | 781815b3b27cc3fcbf153db57deb9bd82ae1d8d3 (diff) | |
| download | nova-8a92af1e84ee7d6f623f677263f9fa5b9487253d.tar.gz nova-8a92af1e84ee7d6f623f677263f9fa5b9487253d.tar.xz nova-8a92af1e84ee7d6f623f677263f9fa5b9487253d.zip | |
Merge "Fixes encoding issues for nova api req body."
| -rw-r--r-- | nova/api/openstack/wsgi.py | 3 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_wsgi.py | 69 |
2 files changed, 71 insertions, 1 deletions
diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 32823ae01..eda82483f 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 = """ <a><name>\xe6\xa6\x82\xe5\xbf\xb5</name></a> """ + 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 = """ <a><name>\xf0\x28\x8c\x28</name></a> """ + 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): |
