From 173457ded8fd257b42c0f824e3c4af2290b71d14 Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Thu, 27 Apr 2017 17:36:51 -0700 Subject: [PATCH] Don't duplicate content type header in local mode Fixes #310 --- chalice/local.py | 6 +++--- tests/unit/test_local.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/chalice/local.py b/chalice/local.py index 92bd8eb0b..dc2da48ee 100644 --- a/chalice/local.py +++ b/chalice/local.py @@ -120,9 +120,9 @@ def _send_http_response(self, lambda_event, response): # type: (EventType, Dict[str, Any]) -> None self.send_response(response['statusCode']) self.send_header('Content-Length', str(len(response['body']))) - self.send_header( - 'Content-Type', - response['headers'].get('Content-Type', 'application/json')) + content_type = response['headers'].pop( + 'Content-Type', 'application/json') + self.send_header('Content-Type', content_type) headers = response['headers'] for header in headers: self.send_header(header, headers[header]) diff --git a/tests/unit/test_local.py b/tests/unit/test_local.py index 462fe0957..914ddc9a8 100644 --- a/tests/unit/test_local.py +++ b/tests/unit/test_local.py @@ -1,4 +1,5 @@ from chalice import local, BadRequestError +from chalice import Response import json import decimal import pytest @@ -66,6 +67,12 @@ def decimals(): def query_string(): return demo.current_request.query_params + @demo.route('/custom-response') + def custom_response(): + return Response(body='text', + status_code=200, + headers={'Content-Type': 'text/plain'}) + return demo @@ -187,6 +194,16 @@ def test_querystring_is_mapped(handler): assert _get_body_from_response_stream(handler) == {'a': 'b', 'c': 'd'} +def test_content_type_included_once(handler): + set_current_request(handler, method='GET', path='/custom-response') + handler.do_GET() + value = handler.wfile.getvalue() + response_lines = value.splitlines() + content_header_lines = [line for line in response_lines + if line.startswith('Content-Type')] + assert len(content_header_lines) == 1 + + @pytest.mark.parametrize('actual_url,matched_url', [ ('/foo', '/foo'), ('/foo/bar', '/foo/bar'),