Skip to content
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
26 changes: 16 additions & 10 deletions mangum/handlers/aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ def __init__(
def request(self) -> Request:
event = self.trigger_event

# multiValue versions of headers take precedence over their plain versions
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
# See this for more info on headers:
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#apigateway-multivalue-headers-and-parameters
headers = {}
# Read headers
if event.get("headers"):
headers.update({k.lower(): v for k, v in event.get("headers", {}).items()})
# Read multiValueHeaders
# This overrides headers that have the same name
# That means that multiValue versions of headers take precedence
# over their plain versions
if event.get("multiValueHeaders"):
headers = {
k.lower(): ", ".join(v) if isinstance(v, list) else ""
for k, v in event.get("multiValueHeaders", {}).items()
}
elif event.get("headers"):
headers = {k.lower(): v for k, v in event.get("headers", {}).items()}
else:
headers = {}
headers.update(
{
k.lower(): ", ".join(v) if isinstance(v, list) else ""
for k, v in event.get("multiValueHeaders", {}).items()
}
)

request_context = event["requestContext"]

Expand Down
31 changes: 24 additions & 7 deletions tests/handlers/test_aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def get_mock_aws_api_gateway_event(
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48", # noqa: E501
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"user": "",
},
"resourcePath": "/{proxy+}",
Expand Down Expand Up @@ -75,15 +77,21 @@ def test_aws_api_gateway_scope_basic():
"httpMethod": "GET",
"requestContext": {"resourcePath": "/", "httpMethod": "GET", "path": "/Prod/"},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", # noqa: E501
"accept": "text/html,application/xhtml+xml,application/xml;"
"q=0.9,image/webp,image/apng,*/*;q=0.8,"
"application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36", # noqa: E501
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" # noqa: E501
"text/html,application/xhtml+xml,application/xml;"
"q=0.9,image/webp,image/apng,*/*;q=0.8,"
"application/signed-exchange;v=b3;q=0.9"
],
"accept-encoding": ["gzip, deflate, br"],
},
Expand All @@ -107,11 +115,20 @@ def test_aws_api_gateway_scope_basic():
"headers": [
[
b"accept",
b"text/html,application/xhtml+xml,application/xml;q=0.9,"
b"image/webp,image/apng,*/*;q=0.8,"
b"text/html,application/xhtml+xml,application/xml;"
b"q=0.9,image/webp,image/apng,*/*;q=0.8,"
b"application/signed-exchange;v=b3;q=0.9",
],
[b"accept-encoding", b"gzip, deflate, br"],
[b"host", b"70ixmpl4fl.execute-api.us-east-2.amazonaws.com"],
[
b"user-agent",
b"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
b"AppleWebKit/537.36 (KHTML, like Gecko) "
b"Chrome/80.0.3987.132 "
b"Safari/537.36",
],
[b"x-amzn-trace-id", b"Root=1-5e66d96f-7491f09xmpl79d18acf3d050"],
],
"http_version": "1.1",
"method": "GET",
Expand All @@ -120,7 +137,7 @@ def test_aws_api_gateway_scope_basic():
"raw_path": None,
"root_path": "",
"scheme": "https",
"server": ("mangum", 80),
"server": ("70ixmpl4fl.execute-api.us-east-2.amazonaws.com", 80),
"type": "http",
}

Expand Down