Skip to content

Commit

Permalink
Typing and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan committed Feb 26, 2021
1 parent 6958ec1 commit 3c5894f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
12 changes: 8 additions & 4 deletions mangum/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def __call__(self, event: dict, context: dict) -> dict:
request_context = event["requestContext"]

if event.get("multiValueHeaders"):
headers = {k.lower(): ", ".join(v) if isinstance(v, list) else ""
for k, v in event.get("multiValueHeaders", {}).items()}
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:
Expand Down Expand Up @@ -122,10 +124,12 @@ def __call__(self, event: dict, context: dict) -> dict:

if event.get("multiValueQueryStringParameters"):
query_string = urllib.parse.urlencode(
event.get("multiValueQueryStringParameters", {}), doseq=True).encode()
event.get("multiValueQueryStringParameters", {}), doseq=True
).encode()
elif event.get("queryStringParameters"):
query_string = urllib.parse.urlencode(
event.get("queryStringParameters", {})).encode()
event.get("queryStringParameters", {})
).encode()
else:
query_string = b""

Expand Down
6 changes: 3 additions & 3 deletions mangum/protocols/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mangum.exceptions import UnexpectedMessage


def all_casings(input_string):
def all_casings(input_string: str) -> typing.Generator:
"""
Permute all casings of a given string.
A pretty algoritm, via @Amber
Expand Down Expand Up @@ -146,8 +146,8 @@ async def send(self, message: Message) -> None:
else:
multi_value_headers[lower_key] = [value.decode()]
if "multiValueHeaders" not in event:
# If there are multiple occurrences of headers, create case-mutated variations
# see: https://github.com/logandk/serverless-wsgi/issues/11
# If there are multiple occurrences of headers, create case-mutated
# variations: https://github.com/logandk/serverless-wsgi/issues/11
for key, values in multi_value_headers.items():
if len(values) > 1:
for value, cased_key in zip(values, all_casings(key)):
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

max-line-length = 88
ignore = E203, W503
per-file-ignores =
tests/conftest.py:E501
tests/test_http.py:E501

[mypy]
disallow_untyped_defs = True
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def mock_http_event(request):
"path": "/test/hello",
"body": body,
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", # noqa: E501
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
Expand Down Expand Up @@ -42,7 +42,7 @@ def mock_http_event(request):
"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 @@ -145,7 +145,7 @@ def mock_http_elb_singlevalue_event(request):
"x-forwarded-proto": "https",
},
"body": body,
"isBase64Encoded": False
"isBase64Encoded": False,
}

return event
Expand Down Expand Up @@ -174,7 +174,7 @@ def mock_http_elb_multivalue_event(request):
"x-forwarded-proto": ["https"],
},
"body": body,
"isBase64Encoded": False
"isBase64Encoded": False,
}

return event
18 changes: 12 additions & 6 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def app(scope, receive, send):
"aws.event": {
"body": None,
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", # noqa: E501
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, " "br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
Expand Down Expand Up @@ -153,7 +153,7 @@ async def app(scope, receive, send):
"aws.event": {
"body": None,
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", # noqa: E501
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, " "br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
Expand Down Expand Up @@ -259,13 +259,17 @@ async def app(scope, receive, send):
"statusCode": 200,
"isBase64Encoded": False,
"headers": {"content-type": "text/plain; charset=utf-8"},
"multiValueHeaders": {"set-cookie": ["cookie1=cookie1; Secure", "cookie2=cookie2; Secure"]},
"multiValueHeaders": {
"set-cookie": ["cookie1=cookie1; Secure", "cookie2=cookie2; Secure"]
},
"body": "Hello, world!",
}


@pytest.mark.parametrize(
"mock_http_elb_singlevalue_event", [["GET", None, {"name": ["me", "you"]}]], indirect=True
"mock_http_elb_singlevalue_event",
[["GET", None, {"name": ["me", "you"]}]],
indirect=True,
)
def test_elb_singlevalue_http_response(mock_http_elb_singlevalue_event) -> None:
async def app(scope, receive, send):
Expand All @@ -281,7 +285,7 @@ async def app(scope, receive, send):
"host": "test.execute-api.us-west-2.amazonaws.com",
"x-forwarded-for": "192.168.100.3, 192.168.100.2, 192.168.100.1",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
"x-forwarded-proto": "https",
},
"httpMethod": "GET",
"path": "/my/path",
Expand Down Expand Up @@ -341,7 +345,9 @@ async def app(scope, receive, send):


@pytest.mark.parametrize(
"mock_http_elb_multivalue_event", [["GET", None, {"name": ["me", "you"]}]], indirect=True
"mock_http_elb_multivalue_event",
[["GET", None, {"name": ["me", "you"]}]],
indirect=True,
)
def test_elb_multivalue_http_response(mock_http_elb_multivalue_event) -> None:
async def app(scope, receive, send):
Expand Down

0 comments on commit 3c5894f

Please sign in to comment.