Skip to content

Commit c5b3d76

Browse files
Feriixukhamaileon
authored andcommitted
Fix aws_api_gateway handler not accepting combined headers and multiValueHeaders (Kludex#229)
* Fix aws_api_gateway handler not accepting combined headers and multiValueHeaders * Fix test_aws_api_gateway.py::test_aws_api_gateway_scope_basic * Formatting * Fix test_aws_api_gateway_scope_basic again There was a single space where there shouldn't have been one, so the assert failed. * More reformatting Lines were too long * Okay now the linters should be happy
1 parent 0b1750c commit c5b3d76

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

mangum/handlers/aws_api_gateway.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ def __init__(
3535
def request(self) -> Request:
3636
event = self.trigger_event
3737

38-
# multiValue versions of headers take precedence over their plain versions
39-
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
38+
# See this for more info on headers:
39+
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#apigateway-multivalue-headers-and-parameters
40+
headers = {}
41+
# Read headers
42+
if event.get("headers"):
43+
headers.update({k.lower(): v for k, v in event.get("headers", {}).items()})
44+
# Read multiValueHeaders
45+
# This overrides headers that have the same name
46+
# That means that multiValue versions of headers take precedence
47+
# over their plain versions
4048
if event.get("multiValueHeaders"):
41-
headers = {
42-
k.lower(): ", ".join(v) if isinstance(v, list) else ""
43-
for k, v in event.get("multiValueHeaders", {}).items()
44-
}
45-
elif event.get("headers"):
46-
headers = {k.lower(): v for k, v in event.get("headers", {}).items()}
47-
else:
48-
headers = {}
49+
headers.update(
50+
{
51+
k.lower(): ", ".join(v) if isinstance(v, list) else ""
52+
for k, v in event.get("multiValueHeaders", {}).items()
53+
}
54+
)
4955

5056
request_context = event["requestContext"]
5157

tests/handlers/test_aws_api_gateway.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def get_mock_aws_api_gateway_event(
4747
"cognitoAuthenticationType": "",
4848
"cognitoAuthenticationProvider": "",
4949
"userArn": "",
50-
"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
50+
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) "
51+
"AppleWebKit/537.36 (KHTML, like Gecko) "
52+
"Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
5153
"user": "",
5254
},
5355
"resourcePath": "/{proxy+}",
@@ -75,15 +77,21 @@ def test_aws_api_gateway_scope_basic():
7577
"httpMethod": "GET",
7678
"requestContext": {"resourcePath": "/", "httpMethod": "GET", "path": "/Prod/"},
7779
"headers": {
78-
"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
80+
"accept": "text/html,application/xhtml+xml,application/xml;"
81+
"q=0.9,image/webp,image/apng,*/*;q=0.8,"
82+
"application/signed-exchange;v=b3;q=0.9",
7983
"accept-encoding": "gzip, deflate, br",
8084
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
81-
"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
85+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
86+
"AppleWebKit/537.36 (KHTML, like Gecko) "
87+
"Chrome/80.0.3987.132 Safari/537.36",
8288
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
8389
},
8490
"multiValueHeaders": {
8591
"accept": [
86-
"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
92+
"text/html,application/xhtml+xml,application/xml;"
93+
"q=0.9,image/webp,image/apng,*/*;q=0.8,"
94+
"application/signed-exchange;v=b3;q=0.9"
8795
],
8896
"accept-encoding": ["gzip, deflate, br"],
8997
},
@@ -107,11 +115,20 @@ def test_aws_api_gateway_scope_basic():
107115
"headers": [
108116
[
109117
b"accept",
110-
b"text/html,application/xhtml+xml,application/xml;q=0.9,"
111-
b"image/webp,image/apng,*/*;q=0.8,"
118+
b"text/html,application/xhtml+xml,application/xml;"
119+
b"q=0.9,image/webp,image/apng,*/*;q=0.8,"
112120
b"application/signed-exchange;v=b3;q=0.9",
113121
],
114122
[b"accept-encoding", b"gzip, deflate, br"],
123+
[b"host", b"70ixmpl4fl.execute-api.us-east-2.amazonaws.com"],
124+
[
125+
b"user-agent",
126+
b"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
127+
b"AppleWebKit/537.36 (KHTML, like Gecko) "
128+
b"Chrome/80.0.3987.132 "
129+
b"Safari/537.36",
130+
],
131+
[b"x-amzn-trace-id", b"Root=1-5e66d96f-7491f09xmpl79d18acf3d050"],
115132
],
116133
"http_version": "1.1",
117134
"method": "GET",
@@ -120,7 +137,7 @@ def test_aws_api_gateway_scope_basic():
120137
"raw_path": None,
121138
"root_path": "",
122139
"scheme": "https",
123-
"server": ("mangum", 80),
140+
"server": ("70ixmpl4fl.execute-api.us-east-2.amazonaws.com", 80),
124141
"type": "http",
125142
}
126143

0 commit comments

Comments
 (0)