diff --git a/requires/installation.txt b/requires/installation.txt index 6dfc681..2a8bd43 100644 --- a/requires/installation.txt +++ b/requires/installation.txt @@ -1,2 +1,2 @@ -ietfparse>=1.4,<1.5 +ietfparse>=1.5.1,<2 tornado>=5,<6 diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index e3d636c..d865480 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -317,6 +317,9 @@ class ContentMixin: acceptable, settings.available_content_types) self._best_response_match = '/'.join( [selected.content_type, selected.content_subtype]) + if selected.content_suffix is not None: + self._best_response_match = '+'.join( + [self._best_response_match, selected.content_suffix]) except errors.NoMatch: self._best_response_match = settings.default_content_type @@ -340,6 +343,9 @@ class ContentMixin: settings.default_content_type)) content_type = '/'.join([content_type_header.content_type, content_type_header.content_subtype]) + if content_type_header.content_suffix is not None: + content_type = '+'.join([content_type, + content_type_header.content_suffix]) try: handler = settings[content_type] except KeyError: diff --git a/tests.py b/tests.py index 6f59077..efa1dc2 100644 --- a/tests.py +++ b/tests.py @@ -101,6 +101,17 @@ class SendResponseTests(testing.AsyncHTTPTestCase): self.assertEqual(response.code, 200) self.assertEqual(response.headers['Vary'], 'Accept') + def test_that_accept_header_with_suffix_is_obeyed(self): + content.add_transcoder( + self._app, + transcoders.MsgPackTranscoder(content_type='expected/content'), + 'application/vendor+msgpack') + response = self.fetch('/', method='POST', body='{}', + headers={'Accept': 'application/vendor+msgpack', + 'Content-Type': 'application/json'}) + self.assertEqual(response.code, 200) + self.assertEqual(response.headers['Content-Type'], 'expected/content') + class GetRequestBodyTests(testing.AsyncHTTPTestCase): @@ -135,6 +146,17 @@ class GetRequestBodyTests(testing.AsyncHTTPTestCase): '').encode('utf-8')) self.assertEqual(response.code, 400) + def test_that_content_type_suffix_is_handled(self): + content.add_transcoder( + self._app, transcoders.JSONTranscoder(), + 'application/vendor+json') + body = {'hello': 'world'} + response = self.fetch( + '/', method='POST', body=json.dumps(body), + headers={'Content-Type': 'application/vendor+json'}) + self.assertEqual(response.code, 200) + self.assertEqual(json.loads(response.body.decode()), body) + class JSONTranscoderTests(unittest.TestCase):