diff --git a/falcon/api.py b/falcon/api.py index 637c9ed..f96c84d 100644 --- a/falcon/api.py +++ b/falcon/api.py @@ -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 = [] diff --git a/falcon/api_helpers.py b/falcon/api_helpers.py index bbf17fc..fee5b31 100644 --- a/falcon/api_helpers.py +++ b/falcon/api_helpers.py @@ -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] diff --git a/falcon/tests/test_after_hooks.py b/falcon/tests/test_after_hooks.py index 3849589..17d831b 100644 --- a/falcon/tests/test_after_hooks.py +++ b/falcon/tests/test_after_hooks.py @@ -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) diff --git a/falcon/tests/test_hello.py b/falcon/tests/test_hello.py index 878cef7..b1626ef 100644 --- a/falcon/tests/test_hello.py +++ b/falcon/tests/test_hello.py @@ -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):