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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `from_http` now names the required binary headers that are actually missing,
instead of always reporting a missing `specversion`. ([#139])

## [2.2.0]

### Changed
Expand Down Expand Up @@ -368,3 +373,4 @@ CloudEvents v2 is a rewrite with ongoing development ([#271])
[#279]: https://github.com/cloudevents/sdk-python/pull/279
[#284]: https://github.com/cloudevents/sdk-python/pull/284
[#291]: https://github.com/cloudevents/sdk-python/pull/291
[#139]: https://github.com/cloudevents/sdk-python/issues/139
27 changes: 27 additions & 0 deletions src/cloudevents/v1/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@
from cloudevents.v1.sdk.converters import is_binary
from cloudevents.v1.sdk.event import v03, v1

_REQUIRED_BINARY_HEADERS = ("ce-specversion", "ce-id", "ce-source", "ce-type")


def _missing_binary_headers(
headers: typing.Mapping[str, str],
) -> typing.List[str]:
"""
Lists the required binary-mode headers missing from a partially populated
binary HTTP request.

Returns an empty list when no binary header is present at all, since such a
request is not an incomplete binary one.

:param headers: The lower-cased HTTP request headers.
:returns: The missing required binary headers.
"""
missing = [header for header in _REQUIRED_BINARY_HEADERS if header not in headers]
if len(missing) == len(_REQUIRED_BINARY_HEADERS):
return []
return missing


def _best_effort_serialize_to_json( # type: ignore[no-untyped-def]
value: typing.Any, *args, **kwargs
Expand Down Expand Up @@ -146,6 +167,12 @@ def from_http(
)

if specversion is None:
missing_binary_headers = _missing_binary_headers(headers)
if missing_binary_headers:
raise cloud_exceptions.MissingRequiredFields(
"Failed to find the following required headers in the binary "
"HTTP request: {}".format(", ".join(missing_binary_headers))
)
raise cloud_exceptions.MissingRequiredFields(
"Failed to find specversion in HTTP request"
)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_v1_compat/test_http_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ def test_missing_required_fields_empty_data_binary(headers):
_ = from_http(headers, None)


@pytest.mark.parametrize(
"headers, missing_header",
list(
zip(invalid_test_headers, ["ce-id", "ce-source", "ce-type", "ce-specversion"])
),
)
def test_missing_required_fields_binary_names_missing_header(headers, missing_header):
# Test for issue #139
with pytest.raises(cloud_exceptions.MissingRequiredFields) as e:
_ = from_http(headers, json.dumps(test_data))
assert missing_header in str(e.value)


@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
def test_emit_binary_event(specversion):
headers = {
Expand Down