Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cloudevents/v1/sdk/converters/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
9 changes: 5 additions & 4 deletions src/cloudevents/v1/sdk/converters/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion src/cloudevents/v1/sdk/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_v1_compat/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
26 changes: 25 additions & 1 deletion tests/test_v1_compat/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
)