Skip to content

Commit

Permalink
Merge pull request #917 from stealthycoin/message-truncation
Browse files Browse the repository at this point in the history
local mode message truncation fix
  • Loading branch information
stealthycoin authored Jul 28, 2018
2 parents 3bfbd3d + e484eb1 commit 21c6ebd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
CHANGELOG
=========

Next Release (TBD)
==================

* Fix local mode issue with unicode responses and Content-Length
(`#910 <https://github.com/aws/chalice/pull/910>`__)

1.6.0
=====

Expand Down
4 changes: 2 additions & 2 deletions chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,15 @@ def _send_http_response(self, code, headers, body):
def _send_http_response_with_body(self, code, headers, body):
# type: (int, HeaderType, Union[str,bytes]) -> None
self.send_response(code)
if not isinstance(body, bytes):
body = body.encode('utf-8')
self.send_header('Content-Length', str(len(body)))
content_type = headers.pop(
'Content-Type', 'application/json')
self.send_header('Content-Type', content_type)
for header_name, header_value in headers.items():
self.send_header(header_name, header_value)
self.end_headers()
if not isinstance(body, bytes):
body = body.encode('utf-8')
self.wfile.write(body)

do_GET = do_PUT = do_POST = do_HEAD = do_DELETE = do_PATCH = do_OPTIONS = \
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ def test_can_accept_get_request(config, sample_app, local_server_factory):
assert response.text == '{"hello": "world"}'


def test_can_get_unicode_string_content_length(
config, local_server_factory):
demo = app.Chalice('app-name')

@demo.route('/')
def index_view():
return u'\u2713'

local_server, port = local_server_factory(demo, config)
response = local_server.make_call(requests.get, '/', port)
assert response.headers['Content-Length'] == '3'


def test_can_accept_options_request(config, sample_app, local_server_factory):
local_server, port = local_server_factory(sample_app, config)
response = local_server.make_call(requests.options, '/test-cors', port)
Expand Down

0 comments on commit 21c6ebd

Please sign in to comment.