fix: Encode body and update resp.body

The patch sets the encoded body to resp.body so that other functions /
calls using it can operate on the updated value, which is the one that
will be sent back to the client.

fixes #158
This commit is contained in:
Flaper Fesp
2013-07-18 22:01:03 +02:00
parent b7eb59bed5
commit c3143d4f4b
4 changed files with 15 additions and 8 deletions

View File

@@ -123,8 +123,12 @@ class API(object):
#
use_body = not helpers.should_ignore_body(resp.status, req.method)
if use_body:
helpers.set_content_length(resp)
# get_body must be called before
# set_content_length so that all
# encodings and transformations
# on the body can be applied first.
body = helpers.get_body(resp)
helpers.set_content_length(resp)
else:
# Default: return an empty body
body = []

View File

@@ -99,6 +99,9 @@ def set_content_length(resp):
def get_body(resp):
"""Converts resp content into an iterable as required by PEP 333
Post:
If resp.body is set, it'll be encoded.
Args:
resp: Instance of falcon.Response
@@ -115,7 +118,8 @@ def get_body(resp):
if body is not None:
if isinstance(body, six.text_type):
return [body.encode('utf-8')]
resp.body = body.encode('utf-8')
return [resp.body]
else:
return [body]

View File

@@ -85,7 +85,7 @@ class TestHooks(testing.TestBase):
self.api.add_route(self.test_route, zoo_resource)
self.simulate_request(self.test_route)
self.assertEqual('fluffy', zoo_resource.resp.body)
self.assertEqual(b'fluffy', zoo_resource.resp.body)
def test_multiple_global_hook(self):
self.api = falcon.API(after=[fluffiness, cuteness])
@@ -94,7 +94,7 @@ class TestHooks(testing.TestBase):
self.api.add_route(self.test_route, zoo_resource)
self.simulate_request(self.test_route)
self.assertEqual('fluffy and cute', zoo_resource.resp.body)
self.assertEqual(b'fluffy and cute', zoo_resource.resp.body)
def test_output_validator(self):
self.simulate_request(self.test_route)
@@ -105,10 +105,10 @@ class TestHooks(testing.TestBase):
self.simulate_request(self.test_route, method='PUT')
actual_body = self.resource.resp.body
self.assertEqual('{"animal": "falcon"}', actual_body)
self.assertEqual(b'{"animal": "falcon"}', actual_body)
def test_wrapped_resource(self):
expected = 'fluffy and cute'
expected = b'fluffy and cute'
self.simulate_request('/wrapped')
self.assertEqual(falcon.HTTP_200, self.srmock.status)
@@ -116,7 +116,6 @@ class TestHooks(testing.TestBase):
self.simulate_request('/wrapped', method='HEAD')
self.assertEqual(falcon.HTTP_200, self.srmock.status)
self.assertEqual(expected, self.wrapped_resource.resp.body)
self.simulate_request('/wrapped', method='POST')
self.assertEqual(falcon.HTTP_405, self.srmock.status)

View File

@@ -101,7 +101,7 @@ class TestHelloWorld(testing.TestBase):
self.assertEquals(self.srmock.status, self.resource.sample_status)
self.assertEquals(resp.status, self.resource.sample_status)
self.assertEquals(resp.body, self.resource.sample_unicode)
self.assertEquals(resp.body, self.resource.sample_utf8)
self.assertEquals(body, [self.resource.sample_utf8])
def test_body_bytes(self):