From e810667e57750ed4b0f269fbed15a27cdd8aebf9 Mon Sep 17 00:00:00 2001 From: Riley Patterson Date: Thu, 2 Jun 2016 18:13:01 -0700 Subject: response: improve charset defaults Refactored the logic in Response.__init__ to handle default charset more consistently. Added logic in the charset setter that ignores attempts to set it on JSON content types. Removed explicit charset specification from exceptions since this is handled correctly for the text types within Response. Fixed some affected tests, and added assertions for content types in exceptions. Addresses https://github.com/Pylons/webob/issues/237 --- tests/test_exc.py | 3 +++ tests/test_request.py | 10 +++++----- tests/test_response.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/test_exc.py b/tests/test_exc.py index 1e57917..14da0f3 100644 --- a/tests/test_exc.py +++ b/tests/test_exc.py @@ -139,6 +139,8 @@ def test_WSGIHTTPException_respects_application_json(): title = 'Validation Failed' explanation = 'Validation of an attribute failed.' def start_response(status, headers, exc_info=None): + # check that json doesn't contain a charset + assert ('Content-Type', 'application/json') in headers pass exc = ValidationError(detail='Attribute "xyz" is invalid.') @@ -216,6 +218,7 @@ def test_WSGIHTTPException_allows_custom_json_formatter(): def test_WSGIHTTPException_generate_response(): def start_response(status, headers, exc_info=None): + assert ('Content-Type', 'text/html; charset=UTF-8') in headers pass environ = { 'wsgi.url_scheme': 'HTTP', diff --git a/tests/test_request.py b/tests/test_request.py index 808840b..87e8b81 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -3265,9 +3265,9 @@ class TestRequest_functional(object): def test_call_WSGI_app(self): req = self._blankOne('/') def wsgi_app(environ, start_response): - start_response('200 OK', [('Content-type', 'text/plain')]) + start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hi!'] - assert req.call_application(wsgi_app) == ('200 OK', [('Content-type', 'text/plain')], [b'Hi!']) + assert req.call_application(wsgi_app) == ('200 OK', [('Content-Type', 'text/plain')], [b'Hi!']) res = req.get_response(wsgi_app) from webob.response import Response @@ -3275,13 +3275,13 @@ class TestRequest_functional(object): assert res.status == '200 OK' from webob.headers import ResponseHeaders assert isinstance(res.headers, ResponseHeaders) - assert list(res.headers.items()) == [('Content-type', 'text/plain')] + assert list(res.headers.items()) == [('Content-Type', 'text/plain; charset=UTF-8')] assert res.body == b'Hi!' def test_get_response_catch_exc_info_true(self): req = self._blankOne('/') def wsgi_app(environ, start_response): - start_response('200 OK', [('Content-type', 'text/plain')]) + start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hi!'] res = req.get_response(wsgi_app, catch_exc_info=True) from webob.response import Response @@ -3289,7 +3289,7 @@ class TestRequest_functional(object): assert res.status == '200 OK' from webob.headers import ResponseHeaders assert isinstance(res.headers, ResponseHeaders) - assert list(res.headers.items()) == [('Content-type', 'text/plain')] + assert list(res.headers.items()) == [('Content-Type', 'text/plain; charset=UTF-8')] assert res.body == b'Hi!' def equal_req(self, req, inp): diff --git a/tests/test_response.py b/tests/test_response.py index aaac831..7b46058 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -60,6 +60,7 @@ def test_response(): del req.environ with pytest.raises(TypeError): Response(charset=None, + content_type='image/jpeg', body=text_(b"unicode body")) with pytest.raises(TypeError): Response(wrong_key='dummy') @@ -140,6 +141,15 @@ def test_init_keeps_specified_charset_when_json(): expected = content_type assert Response(content_type=content_type).headers['content-type'] == expected +def test_set_charset_fails_when_json(): + content_type = 'application/json' + expected = content_type + res = Response(content_type=content_type) + res.charset = 'utf-8' + assert res.headers['content-type'] == expected + res.content_type_params = {'charset': 'utf-8'} + assert res.headers['content-type'] == expected + def test_cookies(): res = Response() -- cgit v1.2.1