Ensure that headers are returned as strings, not integers.

The 'Retry-After' header was set as an integer, and it was causing that
mod_wsgi failed with a 500 error, instead of the actual error. This
ensures that the headers are converted to str.

Fixes bug 1155585
Change-Id: If1b1de2308ccdc6f0588c8df5dc896fe5f767dbe
This commit is contained in:
Alvaro Lopez Garcia
2013-03-19 15:04:25 +01:00
parent 0158304b1d
commit 1c46788b3e
4 changed files with 9 additions and 3 deletions

View File

@@ -588,7 +588,7 @@ class ResponseObject(object):
response = webob.Response()
response.status_int = self.code
for hdr, value in self._headers.items():
response.headers[hdr] = value
response.headers[hdr] = str(value)
response.headers['Content-Type'] = content_type
if self.obj is not None:
response.body = serializer.serialize(self.obj)
@@ -1150,6 +1150,8 @@ 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():
self.wrapped_exc.headers[key] = str(value)
self.status_int = exception.status_int
@webob.dec.wsgify(RequestClass=Request)

View File

@@ -157,7 +157,7 @@ class APITest(test.TestCase):
if hasattr(exception_type, 'headers'):
for (key, value) in exception_type.headers.iteritems():
self.assertTrue(key in resp.headers)
self.assertEquals(resp.headers[key], value)
self.assertEquals(resp.headers[key], str(value))
def test_quota_error_mapping(self):
self._do_test_exception_mapping(exception.QuotaError, 'too many used')

View File

@@ -68,6 +68,8 @@ class TestFaults(test.TestCase):
for request in requests:
exc = webob.exc.HTTPRequestEntityTooLarge
# NOTE(aloga): we intentionally pass an integer for the
# 'Retry-After' header. It should be then converted to a str
fault = wsgi.Fault(exc(explanation='sorry',
headers={'Retry-After': 4}))
response = request.get_response(fault)
@@ -76,7 +78,7 @@ class TestFaults(test.TestCase):
"overLimit": {
"message": "sorry",
"code": 413,
"retryAfter": 4,
"retryAfter": "4",
},
}
actual = jsonutils.loads(response.body)

View File

@@ -867,6 +867,7 @@ class ResponseObjectTest(test.TestCase):
atom=AtomSerializer)
robj['X-header1'] = 'header1'
robj['X-header2'] = 'header2'
robj['X-header3'] = 3
for content_type, mtype in wsgi._MEDIA_TYPE_MAP.items():
request = wsgi.Request.blank('/tests/123')
@@ -875,6 +876,7 @@ class ResponseObjectTest(test.TestCase):
self.assertEqual(response.headers['Content-Type'], content_type)
self.assertEqual(response.headers['X-header1'], 'header1')
self.assertEqual(response.headers['X-header2'], 'header2')
self.assertEqual(response.headers['X-header3'], '3')
self.assertEqual(response.status_int, 202)
self.assertEqual(response.body, mtype)