Skip to content
Open
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
6 changes: 6 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class APIGatewayProxyEventModel(BaseModel):
isBase64Encoded: bool | None = None
body: str | type[BaseModel] | None = None

@field_validator("headers", "multiValueHeaders", mode="before")
@classmethod
def _null_headers_as_empty(cls, value):
# The Test tab in the API Gateway console sends null headers when the test has none
return {} if value is None else value


class ApiGatewayAuthorizerToken(BaseModel):
type: Literal["TOKEN"]
Expand Down
45 changes: 45 additions & 0 deletions tests/events/apiGatewayProxyEventConsoleTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"resource": "/no-auth",
"path": "/no-auth",
"httpMethod": "POST",
"headers": null,
"multiValueHeaders": null,
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"resourceId": "3nq1cq",
"resourcePath": "/no-auth",
"httpMethod": "POST",
"extendedRequestId": "ZhGnYGtqjoEFyzw=",
"requestTime": "17/Jun/2024:15:08:18 +0000",
"path": "/no-auth",
"accountId": "123456789012",
"protocol": "HTTP/1.1",
"stage": "test-invoke-stage",
"domainPrefix": "testPrefix",
"requestTimeEpoch": 1718636898112,
"requestId": "28422e1a-c975-42da-895e-165f882dafef",
"identity": {
"cognitoIdentityPoolId": null,
"cognitoIdentityId": null,
"apiKey": "test-invoke-api-key",
"principalOrgId": null,
"cognitoAuthenticationType": null,
"userArn": "arn:aws:sts::123456789012:assumed-role/Admin/my-user",
"apiKeyId": "test-invoke-api-key-id",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15",
"accountId": "123456789012",
"caller": "ABCAXZWZ5ZDPNIH6C6ODK:my-user",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ABCAXZWZ5ZDPFDRIWYXX",
"cognitoAuthenticationProvider": null,
"user": "ABCAXZWZ5ZDPNIH6C6ODK:my-user"
},
"domainName": "testPrefix.testDomainName",
"apiId": "puhdx84jy9"
},
"body": null,
"isBase64Encoded": false
}
11 changes: 11 additions & 0 deletions tests/unit/parser/_pydantic/test_apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ def test_apigw_event_empty_body():
parse(event=event, model=APIGatewayProxyEventModel)


def test_apigw_event_from_console_test_without_headers():
# The Test tab in the API Gateway console sends null headers when the test has none
raw_event = load_event("apiGatewayProxyEventConsoleTest.json")
parsed_event: APIGatewayProxyEventModel = parse(event=raw_event, model=APIGatewayProxyEventModel)

assert parsed_event.headers == {}
assert parsed_event.multiValueHeaders == {}
assert parsed_event.requestContext.stage == "test-invoke-stage"
assert parsed_event.requestContext.identity.sourceIp == "test-invoke-source-ip"


def test_apigw_event_authorizer_token():
raw_event = load_event("apiGatewayAuthorizerTokenEvent.json")
parsed_event: ApiGatewayAuthorizerToken = ApiGatewayAuthorizerToken(**raw_event)
Expand Down