Skip to content

Commit 7414df7

Browse files
fix(event_handlers): handle lack of headers when using auto-compression feature (#1325)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 33999f4 commit 7414df7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

aws_lambda_powertools/utilities/data_classes/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ def get_header_value(
3232
headers: Dict[str, str], name: str, default_value: Optional[str], case_sensitive: Optional[bool]
3333
) -> Optional[str]:
3434
"""Get header value by name"""
35+
# If headers is NoneType, return default value
36+
if not headers:
37+
return default_value
38+
3539
if case_sensitive:
3640
return headers.get(name, default_value)
37-
3841
name_lower = name.lower()
3942

4043
return next(

tests/functional/event_handler/test_api_gateway.py

+18
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ def return_text() -> Response:
314314
assert result["body"] == expected_value
315315

316316

317+
def test_compress_no_accept_encoding_null_headers():
318+
# GIVEN a function with compress=True
319+
# AND the request has no headers
320+
app = ApiGatewayResolver()
321+
expected_value = "Foo"
322+
323+
@app.get("/my/path", compress=True)
324+
def return_text() -> Response:
325+
return Response(200, content_types.TEXT_PLAIN, expected_value)
326+
327+
# WHEN calling the event handler
328+
result = app({"path": "/my/path", "httpMethod": "GET", "headers": None}, None)
329+
330+
# THEN don't perform any gzip compression
331+
assert result["isBase64Encoded"] is False
332+
assert result["body"] == expected_value
333+
334+
317335
def test_cache_control_200():
318336
# GIVEN a function with cache_control set
319337
app = ApiGatewayResolver()

0 commit comments

Comments
 (0)