diff --git a/src/cloudevents/v1/sdk/converters/structured.py b/src/cloudevents/v1/sdk/converters/structured.py index 9b35784c..97c01595 100644 --- a/src/cloudevents/v1/sdk/converters/structured.py +++ b/src/cloudevents/v1/sdk/converters/structured.py @@ -34,7 +34,7 @@ def can_read( headers = {} return ( isinstance(content_type, str) - and content_type.startswith(self.MIME_TYPE) + and content_type.split(";", 1)[0].strip().lower() == self.MIME_TYPE or not has_binary_headers(headers) ) diff --git a/src/cloudevents/v1/sdk/converters/util.py b/src/cloudevents/v1/sdk/converters/util.py index ec709d3c..8d9c02f4 100644 --- a/src/cloudevents/v1/sdk/converters/util.py +++ b/src/cloudevents/v1/sdk/converters/util.py @@ -21,9 +21,10 @@ def has_binary_headers(headers: typing.Mapping[str, str]) -> bool: :returns: True if all the headers are present, False otherwise. """ + lowered_headers = {key.lower() for key in headers} return ( - "ce-specversion" in headers - and "ce-source" in headers - and "ce-type" in headers - and "ce-id" in headers + "ce-specversion" in lowered_headers + and "ce-source" in lowered_headers + and "ce-type" in lowered_headers + and "ce-id" in lowered_headers ) diff --git a/src/cloudevents/v1/sdk/event/base.py b/src/cloudevents/v1/sdk/event/base.py index 589e37c8..7b1dbdb3 100644 --- a/src/cloudevents/v1/sdk/event/base.py +++ b/src/cloudevents/v1/sdk/event/base.py @@ -256,7 +256,7 @@ def UnmarshalBinary( data_unmarshaller: types.UnmarshallerType, ) -> None: required_binary_fields = {f"ce-{field}" for field in self._ce_required_fields} - missing_fields = required_binary_fields - headers.keys() + missing_fields = required_binary_fields - {k.lower() for k in headers} if len(missing_fields) > 0: raise cloud_exceptions.MissingRequiredFields( diff --git a/tests/test_v1_compat/test_base_events.py b/tests/test_v1_compat/test_base_events.py index ff10abff..c4aaf743 100644 --- a/tests/test_v1_compat/test_base_events.py +++ b/tests/test_v1_compat/test_base_events.py @@ -32,3 +32,21 @@ def test_get_nonexistent_optional(event_class): event.SetExtensions({"ext1": "val"}) res = event.Get("ext1") assert res[0] == "val" and res[1] is True + + +@pytest.mark.parametrize("event_class", [v1.Event, v03.Event]) +def test_unmarshal_binary_case_insensitive_headers(event_class): + event = event_class() + headers = { + "Ce-Specversion": event.CloudEventVersion(), + "Ce-Source": "/source", + "Ce-Type": "com.example.test", + "Ce-Id": "id-123", + "Content-Type": "application/json", + } + event.UnmarshalBinary(headers, b'{"k":"v"}', lambda b: b.decode("utf-8")) + assert event.EventID() == "id-123" + assert event.Source() == "/source" + assert event.EventType() == "com.example.test" + assert event.ContentType() == "application/json" + assert event.Data() == '{"k":"v"}' diff --git a/tests/test_v1_compat/test_converters.py b/tests/test_v1_compat/test_converters.py index 78f7aa2f..964045dd 100644 --- a/tests/test_v1_compat/test_converters.py +++ b/tests/test_v1_compat/test_converters.py @@ -15,7 +15,7 @@ import pytest from cloudevents.v1.sdk import exceptions -from cloudevents.v1.sdk.converters import base, binary +from cloudevents.v1.sdk.converters import base, binary, structured def test_binary_converter_raise_unsupported(): @@ -40,3 +40,27 @@ def test_base_converters_raise_exceptions(): with pytest.raises(Exception): cnvtr = base.Converter() cnvtr.read(None, None, None, None) # type: ignore[arg-type] # intentionally wrong type # noqa: E501 + + +def test_converters_case_insensitive_headers_and_content_type(): + mixed_headers = { + "Ce-Specversion": "1.0", + "Ce-Source": "/source", + "Ce-Type": "com.example.test", + "Ce-Id": "id-123", + } + assert binary.BinaryHTTPCloudEventConverter().can_read(headers=mixed_headers) + assert not structured.JSONHTTPCloudEventConverter().can_read( + content_type="application/json", headers=mixed_headers + ) + assert structured.JSONHTTPCloudEventConverter().can_read( + content_type="APPLICATION/CLOUDEVENTS+JSON; charset=utf-8", + headers=mixed_headers, + ) + assert not structured.JSONHTTPCloudEventConverter().can_read( + content_type="application/cloudevents+jsonl", headers=mixed_headers + ) + assert not structured.JSONHTTPCloudEventConverter().can_read( + content_type="application/cloudevents+jsonl; charset=utf-8", + headers=mixed_headers, + )