summaryrefslogtreecommitdiff
path: root/nova/api/openstack/wsgi.py
diff options
context:
space:
mode:
authorAndrey Kurilin <akurilin@mirantis.com>2015-10-05 18:18:48 +0300
committerAndrey Kurilin <andr.kurilin@gmail.com>2015-12-10 21:53:01 +0200
commitcc1ff0051a767a5d2e0e525767455ca1c3363c2e (patch)
tree7d38db0ceff36150d7d6fcb067d7906531a01b61 /nova/api/openstack/wsgi.py
parent240df4285964d341239376d8ea355c1b3763007a (diff)
downloadnova-cc1ff0051a767a5d2e0e525767455ca1c3363c2e.tar.gz
[Py34] Enable api.openstack.test_wsgi unit test
Issues: - Method `webob.multidict.MultiDict.items` returns list object in py2 env[1] and listiterator in py3 env[2]. ListIterator prevents changing values of headers in a loop(changes of item's order prevent skipping some headers). - Statement `nova.utils.utf8(str(some_value))` did not work for bytes type in py3 env. Good example is `test_resource_headers_are_utf8` test. Int value 1 became to b'b"b\'1\'"' after deserialization. - Wrong checks for utf-8. (including check for encoding headers names, which is not encoded anywhere in the code) - Wrong checks for `body` property of `webob.response.Response` cls. Method `_body__get` returns bytes[3]. There is no problem in python 2, since type(b"some") equals to type("some"), but it wrong for python 3. [1] - https://github.com/Pylons/webob/blob/1.5.0b0/webob/multidict.py#L266-L267 [2] - https://github.com/Pylons/webob/blob/1.5.0b0/webob/multidict.py#L260-L264 [3] - https://github.com/Pylons/webob/blob/1.5.0b0/webob/response.py#L350 Related bp nova-python3-mitaka Change-Id: I15150c8c8f204081b9b5d8bc28e622692d7d749a
Diffstat (limited to 'nova/api/openstack/wsgi.py')
-rw-r--r--nova/api/openstack/wsgi.py33
1 files changed, 20 insertions, 13 deletions
diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py
index 668333049a..256d1bfc34 100644
--- a/nova/api/openstack/wsgi.py
+++ b/nova/api/openstack/wsgi.py
@@ -297,7 +297,7 @@ class JSONDictSerializer(DictSerializer):
"""Default JSON request body serialization."""
def default(self, data):
- return jsonutils.dumps(data)
+ return six.text_type(jsonutils.dumps(data))
def serializers(**serializers):
@@ -457,14 +457,20 @@ class ResponseObject(object):
default_serializers)
serializer = _serializer()
- response = webob.Response()
+ body = None
+ if self.obj is not None:
+ body = serializer.serialize(self.obj)
+ response = webob.Response(body=body)
+ if response.headers.get('Content-Length'):
+ # NOTE(andreykurilin): we need to encode 'Content-Length' header,
+ # since webob.Response auto sets it if "body" attr is presented.
+ # https://github.com/Pylons/webob/blob/1.5.0b0/webob/response.py#L147
+ response.headers['Content-Length'] = utils.utf8(
+ response.headers['Content-Length'])
response.status_int = self.code
for hdr, value in self._headers.items():
- response.headers[hdr] = utils.utf8(str(value))
+ response.headers[hdr] = utils.utf8(value)
response.headers['Content-Type'] = utils.utf8(content_type)
- if self.obj is not None:
- response.body = serializer.serialize(self.obj)
-
return response
@property
@@ -645,7 +651,7 @@ class Resource(wsgi.Application):
content_type = request.get_content_type()
except exception.InvalidContentType:
LOG.debug("Unrecognized Content-Type provided in request")
- return None, ''
+ return None, b''
return content_type, request.body
@@ -860,10 +866,9 @@ class Resource(wsgi.Application):
self.default_serializers)
if hasattr(response, 'headers'):
-
- for hdr, val in response.headers.items():
+ for hdr, val in list(response.headers.items()):
# Headers must be utf-8 strings
- response.headers[hdr] = utils.utf8(str(val))
+ response.headers[hdr] = utils.utf8(val)
if not request.api_version_request.is_null():
response.headers[API_VERSION_REQUEST_HEADER] = \
@@ -1158,7 +1163,7 @@ class Fault(webob.exc.HTTPException):
def __init__(self, exception):
"""Create a Fault for the given webob.exc.exception."""
self.wrapped_exc = exception
- for key, value in self.wrapped_exc.headers.items():
+ for key, value in list(self.wrapped_exc.headers.items()):
self.wrapped_exc.headers[key] = str(value)
self.status_int = exception.status_int
@@ -1195,8 +1200,9 @@ class Fault(webob.exc.HTTPException):
'application/json': JSONDictSerializer(),
}[content_type]
- self.wrapped_exc.body = serializer.serialize(fault_data)
self.wrapped_exc.content_type = content_type
+ self.wrapped_exc.charset = 'UTF-8'
+ self.wrapped_exc.text = serializer.serialize(fault_data)
return self.wrapped_exc
@@ -1245,7 +1251,8 @@ class RateLimitFault(webob.exc.HTTPException):
}[content_type]
content = serializer.serialize(self.content)
- self.wrapped_exc.body = content
+ self.wrapped_exc.charset = 'UTF-8'
self.wrapped_exc.content_type = content_type
+ self.wrapped_exc.text = content
return self.wrapped_exc