summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRohan Rhishikesh Kanade <rohan.kanade@nttdata.com>2013-05-17 04:15:26 -0700
committerRohan Rhishikesh Kanade <rohan.kanade@nttdata.com>2013-05-20 03:43:04 -0700
commit781815b3b27cc3fcbf153db57deb9bd82ae1d8d3 (patch)
tree97ecd72e5c3f7b5999133266a6a9cd526509491c
parent5c3113b066e61cbc5d8d4d464f8200d4cb5e8395 (diff)
downloadnova-781815b3b27cc3fcbf153db57deb9bd82ae1d8d3.tar.gz
nova-781815b3b27cc3fcbf153db57deb9bd82ae1d8d3.tar.xz
nova-781815b3b27cc3fcbf153db57deb9bd82ae1d8d3.zip
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
-rw-r--r--nova/api/openstack/wsgi.py3
-rw-r--r--nova/tests/api/openstack/test_wsgi.py69
2 files changed, 71 insertions, 1 deletions
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 = """ <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):