diff --git a/kombu/serialization.py b/kombu/serialization.py index ed7bb315..86621df7 100644 --- a/kombu/serialization.py +++ b/kombu/serialization.py @@ -445,7 +445,17 @@ for ep, args in entrypoints('kombu.serializers'): # pragma: no cover def prepare_accept_content(content_types, name_to_type=None): + """Replace aliases of content_types with full names from registry. + + Raises: + SerializerNotInstalled: If the serialization method + requested is not available. + """ name_to_type = registry.name_to_type if not name_to_type else name_to_type if content_types is not None: - return {n if '/' in n else name_to_type[n] for n in content_types} + try: + return {n if '/' in n else name_to_type[n] for n in content_types} + except KeyError as e: + raise SerializerNotInstalled( + f'No encoder/decoder installed for {e.args[0]}') return content_types diff --git a/t/unit/test_serialization.py b/t/unit/test_serialization.py index 82aec807..fe5ea625 100644 --- a/t/unit/test_serialization.py +++ b/t/unit/test_serialization.py @@ -13,7 +13,7 @@ from kombu.serialization import ( raw_encode, register_yaml, register_msgpack, dumps, loads, pickle, pickle_protocol, unregister, register_pickle, enable_insecure_serializers, - disable_insecure_serializers, + disable_insecure_serializers, prepare_accept_content ) from kombu.utils.encoding import str_to_bytes @@ -307,3 +307,12 @@ class test_Serialization: register_msgpack() with pytest.raises(SerializerNotInstalled): loads('foo', 'application/x-msgpack', 'utf-8') + + def test_prepare_accept_content(self): + assert {'application/json'} == prepare_accept_content(['json']) + assert {'application/json'} == prepare_accept_content( + ['application/json']) + + def test_prepare_accept_content_bad_serializer(self): + with pytest.raises(SerializerNotInstalled): + prepare_accept_content(['bad_serializer'])