Strip charset parameter from text content types.

This commit is contained in:
Dave Shawley 2016-01-31 12:13:28 -05:00
parent 995c715405
commit ed357d878f
2 changed files with 16 additions and 2 deletions

View file

@ -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))

View file

@ -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):