Skip to content

Commit 32d0457

Browse files
committed
fix(event_handler): don't be smart about multiple headers
1 parent a8a3401 commit 32d0457

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

aws_lambda_powertools/shared/headers_serializer.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def serialize(self, headers: Dict[str, str], cookies: List[str]) -> Dict[str, An
4949
payload: Dict[str, List[str]] = {}
5050

5151
for key, value in headers.items():
52-
values = value.split(",")
53-
payload[key] = [value.strip() for value in values]
52+
payload[key] = [value]
5453

5554
if cookies:
5655
payload.setdefault("Set-Cookie", [])
@@ -82,14 +81,6 @@ def serialize(self, headers: Dict[str, str], cookies: List[str]) -> Dict[str, An
8281
payload["headers"]["Set-Cookie"] = cookies[-1]
8382

8483
for key, value in headers.items():
85-
values = value.split(",")
86-
if len(values) > 1:
87-
warnings.warn(
88-
"Can't encode more than on header with the same key in the response. "
89-
"Did you enable multiValueHeaders on the ALB Target Group?"
90-
)
91-
92-
# We can only send on header for this key, send the last value
93-
payload["headers"][key] = values[-1].strip()
84+
payload["headers"][key] = value
9485

9586
return payload

tests/functional/event_handler/test_api_gateway.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def handler(event, context):
266266
assert headers["Content-Type"] == [content_types.TEXT_HTML]
267267
assert headers["Access-Control-Allow-Origin"] == ["*"]
268268
assert "Access-Control-Allow-Credentials" not in headers
269-
assert headers["Access-Control-Allow-Headers"] == sorted(CORSConfig._REQUIRED_HEADERS)
269+
assert headers["Access-Control-Allow-Headers"] == [",".join(sorted(CORSConfig._REQUIRED_HEADERS))]
270270

271271
# THEN for routes without cors flag return no cors headers
272272
mock_event = {"path": "/my/request", "httpMethod": "GET"}
@@ -483,9 +483,9 @@ def another_one():
483483
headers = result["multiValueHeaders"]
484484
assert headers["Content-Type"] == [content_types.APPLICATION_JSON]
485485
assert headers["Access-Control-Allow-Origin"] == [cors_config.allow_origin]
486-
expected_allows_headers = sorted(set(allow_header + cors_config._REQUIRED_HEADERS))
486+
expected_allows_headers = [",".join(sorted(set(allow_header + cors_config._REQUIRED_HEADERS)))]
487487
assert headers["Access-Control-Allow-Headers"] == expected_allows_headers
488-
assert headers["Access-Control-Expose-Headers"] == cors_config.expose_headers
488+
assert headers["Access-Control-Expose-Headers"] == [",".join(cors_config.expose_headers)]
489489
assert headers["Access-Control-Max-Age"] == [str(cors_config.max_age)]
490490
assert "Access-Control-Allow-Credentials" in headers
491491
assert headers["Access-Control-Allow-Credentials"] == ["true"]
@@ -558,7 +558,7 @@ def post_no_cors():
558558
headers = result["multiValueHeaders"]
559559
assert "Content-Type" not in headers
560560
assert "Access-Control-Allow-Origin" in result["multiValueHeaders"]
561-
assert headers["Access-Control-Allow-Methods"] == ["DELETE", "GET", "OPTIONS"]
561+
assert headers["Access-Control-Allow-Methods"] == ["DELETE,GET,OPTIONS"]
562562

563563

564564
def test_custom_preflight_response():

tests/functional/event_handler/test_headers_serializer.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_headers_serializer_multi_value_headers():
3636
assert payload == {"multiValueHeaders": {"Set-Cookie": ["UUID=12345"]}}
3737

3838
payload = serializer.serialize(cookies=["UUID=12345", "SSID=0xdeadbeef"], headers={"Foo": "bar,zbr"})
39-
assert payload == {"multiValueHeaders": {"Set-Cookie": ["UUID=12345", "SSID=0xdeadbeef"], "Foo": ["bar", "zbr"]}}
39+
assert payload == {"multiValueHeaders": {"Set-Cookie": ["UUID=12345", "SSID=0xdeadbeef"], "Foo": ["bar,zbr"]}}
4040

4141

4242
def test_headers_serializer_single_value_headers():
@@ -55,14 +55,10 @@ def test_headers_serializer_single_value_headers():
5555
warnings.simplefilter("default")
5656

5757
payload = serializer.serialize(cookies=["UUID=12345", "SSID=0xdeadbeef"], headers={"Foo": "bar,zbr"})
58-
assert payload == {"headers": {"Set-Cookie": "SSID=0xdeadbeef", "Foo": "zbr"}}
58+
assert payload == {"headers": {"Set-Cookie": "SSID=0xdeadbeef", "Foo": "bar,zbr"}}
5959

60-
assert len(w) == 2
61-
assert str(w[-2].message) == (
62-
"Can't encode more than one cookie in the response. "
63-
"Did you enable multiValueHeaders on the ALB Target Group?"
64-
)
60+
assert len(w) == 1
6561
assert str(w[-1].message) == (
66-
"Can't encode more than on header with the same key in the response. "
62+
"Can't encode more than one cookie in the response. "
6763
"Did you enable multiValueHeaders on the ALB Target Group?"
6864
)

0 commit comments

Comments
 (0)