Skip to content

Commit

Permalink
fix(event_handlers): handle lack of headers when using auto-compressi…
Browse files Browse the repository at this point in the history
…on feature (#1325)

Co-authored-by: Heitor Lessa <lessa@amazon.com>
  • Loading branch information
tbuatois and heitorlessa authored Jul 25, 2022
1 parent 33999f4 commit 7414df7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion aws_lambda_powertools/utilities/data_classes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def get_header_value(
headers: Dict[str, str], name: str, default_value: Optional[str], case_sensitive: Optional[bool]
) -> Optional[str]:
"""Get header value by name"""
# If headers is NoneType, return default value
if not headers:
return default_value

if case_sensitive:
return headers.get(name, default_value)

name_lower = name.lower()

return next(
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ def return_text() -> Response:
assert result["body"] == expected_value


def test_compress_no_accept_encoding_null_headers():
# GIVEN a function with compress=True
# AND the request has no headers
app = ApiGatewayResolver()
expected_value = "Foo"

@app.get("/my/path", compress=True)
def return_text() -> Response:
return Response(200, content_types.TEXT_PLAIN, expected_value)

# WHEN calling the event handler
result = app({"path": "/my/path", "httpMethod": "GET", "headers": None}, None)

# THEN don't perform any gzip compression
assert result["isBase64Encoded"] is False
assert result["body"] == expected_value


def test_cache_control_200():
# GIVEN a function with cache_control set
app = ApiGatewayResolver()
Expand Down

0 comments on commit 7414df7

Please sign in to comment.