diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index bdc8bc6..7675b7e 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -148,9 +148,15 @@ def add_text_content_type(application, content_type, default_encoding, :param loads: function that loads a dictionary from a string. ``loads(str, encoding:str) -> dict`` + Note that the ``charset`` parameter is stripped from `content_type` + if it is present. + """ - add_transcoder(application, content_type, - handlers.TextContentHandler(content_type, dumps, loads, + parsed = headers.parse_content_type(content_type) + parsed.parameters.pop('charset', None) + normalized = str(parsed) + add_transcoder(application, normalized, + handlers.TextContentHandler(normalized, dumps, loads, default_encoding)) diff --git a/tests.py b/tests.py index ebd9546..9464209 100644 --- a/tests.py +++ b/tests.py @@ -240,6 +240,14 @@ class ContentFunctionTests(unittest.TestCase): self.assertIs(transcoder._dumps, json.dumps) self.assertIs(transcoder._loads, json.loads) + def test_that_add_text_content_type_discards_charset_parameter(self): + content.add_text_content_type(self.context, + 'application/json;charset=UTF-8', 'utf8', + json.dumps, json.loads) + settings = content.ContentSettings.from_application(self.context) + transcoder = settings['application/json'] + self.assertIsInstance(transcoder, handlers.TextContentHandler) + class MsgPackTranscoderTests(unittest.TestCase):