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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ source_pkgs = ["mangum", "tests"]
omit = ["mangum/_compat.py"]

[tool.coverage.report]
skip_covered = true
exclude_lines = [
"pragma: no cover",
"pragma: nocover",
Expand Down
70 changes: 0 additions & 70 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,76 +176,6 @@ def mock_http_api_event_v1(request: pytest.FixtureRequest):
return event


@pytest.fixture
def mock_lambda_at_edge_event(request: pytest.FixtureRequest):
method = request.param[0]
path = request.param[1]
query_string = request.param[2]
body = request.param[3]

headers_raw = {
"accept-encoding": "gzip,deflate",
"x-forwarded-port": "443",
"x-forwarded-for": "192.168.100.1",
"x-forwarded-proto": "https",
"host": "test.execute-api.us-west-2.amazonaws.com",
}
headers = {}
for key, value in headers_raw.items():
headers[key.lower()] = [{"key": key, "value": value}]

event = {
"Records": [
{
"cf": {
"config": {
"distributionDomainName": "mock-distribution.local.localhost",
"distributionId": "ABC123DEF456G",
"eventType": "origin-request",
"requestId": "lBEBo2N0JKYUP2JXwn_4am2xAXB2GzcL2FlwXI8G59PA8wghF2ImFQ==",
},
"request": {
"clientIp": "192.168.100.1",
"headers": headers,
"method": method,
"origin": {
"custom": {
"customHeaders": {
"x-lae-env-custom-var": [
{
"key": "x-lae-env-custom-var",
"value": "environment variable",
}
],
},
"domainName": "www.example.com",
"keepaliveTimeout": 5,
"path": "",
"port": 80,
"protocol": "http",
"readTimeout": 30,
"sslProtocols": ["TLSv1", "TLSv1.1", "TLSv1.2"],
}
},
"querystring": query_string,
"uri": path,
},
}
}
]
}

if body is not None:
event["Records"][0]["cf"]["request"]["body"] = {
"inputTruncated": False,
"action": "read-only",
"encoding": "text",
"data": body,
}

return dict(method=method, path=path, query_string=query_string, body=body, event=event)


@pytest.fixture(scope="session", autouse=True)
def aws_credentials():
"""Mocked AWS Credentials for moto."""
Expand Down
18 changes: 18 additions & 0 deletions tests/handlers/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ def test_custom_handler():
"server": ("mangum", 8080),
"type": "http",
}


def test_custom_handler_infer():
"""Test the infer method of CustomHandler."""
event_with_key = {"my-custom-key": 1}
event_without_key = {"other-key": 1}

assert CustomHandler.infer(event_with_key, {}, {"api_gateway_base_path": "/"}) is True
assert CustomHandler.infer(event_without_key, {}, {"api_gateway_base_path": "/"}) is False


def test_custom_handler_call():
"""Test the __call__ method of CustomHandler."""
event = {"my-custom-key": 1}
handler = CustomHandler(event, {}, {"api_gateway_base_path": "/"})

result = handler(status=200, headers=[], body=b"Hello, World!")
assert result == {"statusCode": 200, "headers": {}, "body": "Hello, World!"}
6 changes: 4 additions & 2 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ async def app(scope, receive, send):
await send({"type": "lifespan.startup.failed", "message": "Failed."})
else:
await send({"type": "lifespan.startup.complete"})
if message["type"] == "lifespan.shutdown":
elif message["type"] == "lifespan.shutdown":
if failure_type == "shutdown":
await send({"type": "lifespan.shutdown.failed", "message": "Failed."})
await send({"type": "lifespan.shutdown.complete"})
else: # pragma: no cover
await send({"type": "lifespan.shutdown.complete"})
return # pragma: no cover

handler = Mangum(app, lifespan=lifespan)

Expand Down