Skip to content

Commit c1e8779

Browse files
committed
fix(event_handler): NoneType error for compress feature without headers
1 parent 4bbcc24 commit c1e8779

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

aws_lambda_powertools/utilities/data_classes/common.py

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def get_header_value(
3535
if case_sensitive:
3636
return headers.get(name, default_value)
3737

38+
# If headers is NoneType, return default value
39+
if not headers:
40+
return default_value
41+
3842
name_lower = name.lower()
3943

4044
return next(

tests/functional/event_handler/test_api_gateway.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def read_image() -> Response:
296296
assert headers["Content-Encoding"] == "gzip"
297297

298298

299-
def test_compress_no_accept_encoding():
299+
def test_compress_no_accept_encoding_null_headers():
300300
# GIVEN a function with compress=True
301301
# AND the request has no "Accept-Encoding" set to include gzip
302302
app = ApiGatewayResolver()
@@ -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)