Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(event_handlers): omit explicit None HTTP header values #1793

Merged
Merged
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
16 changes: 13 additions & 3 deletions aws_lambda_powertools/shared/headers_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
# Duplicate headers are combined with commas and included in the headers field.
combined_headers: Dict[str, str] = {}
for key, values in headers.items():
# omit headers with explicit null values
if values is None:
continue

if isinstance(values, str):
combined_headers[key] = values
else:
Expand All @@ -60,13 +64,15 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers-response
"""
payload: Dict[str, List[str]] = defaultdict(list)

for key, values in headers.items():
# omit headers with explicit null values
if values is None:
continue

if isinstance(values, str):
payload[key].append(values)
else:
for value in values:
payload[key].append(value)
payload[key].extend(values)

if cookies:
payload.setdefault("Set-Cookie", [])
Expand Down Expand Up @@ -98,6 +104,10 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
payload["headers"]["Set-Cookie"] = str(cookies[-1])

for key, values in headers.items():
# omit headers with explicit null values
if values is None:
continue

if isinstance(values, str):
payload["headers"][key] = values
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_headers_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ def test_single_value_headers_with_multiple_header_values_warning():
payload = serializer.serialize(cookies=[], headers=headers)

assert payload["headers"]["Foo"] == headers["Foo"][-1]


def test_http_api_headers_serializer_with_null_values():
serializer = HttpApiHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload == {"headers": {}, "cookies": []}


def test_multi_value_headers_serializer_with_null_values():
serializer = MultiValueHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload == {"multiValueHeaders": {}}


def test_single_value_headers_serializer_with_null_values():
serializer = SingleValueHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload["headers"] == {}