Skip to content

fix(event_handlers): handle lack of headers when using auto-compression feature #1325

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

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
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