-
-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21766f1
commit eb0b8a9
Showing
11 changed files
with
93 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import typing as t | ||
|
||
from starlette.routing import Router | ||
from starlette.types import ASGIApp, Receive, Scope, Send | ||
|
||
Lifespan = t.Callable[[t.Any], t.AsyncContextManager] | ||
|
||
|
||
class LifespanMiddleware: | ||
""" | ||
Middleware that adds support for Starlette lifespan handlers | ||
(https://www.starlette.io/lifespan/). | ||
""" | ||
|
||
def __init__( | ||
self, next_app: ASGIApp, *, lifespan: t.Optional[Lifespan], **kwargs | ||
) -> None: | ||
self.next_app = next_app | ||
self._lifespan = lifespan | ||
# Leverage a Starlette Router for lifespan handling only | ||
self.router = Router(lifespan=lifespan) | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
# If no lifespan is registered, pass to next app so it can be handled downstream. | ||
if scope["type"] == "lifespan" and self._lifespan: | ||
await self.router(scope, receive, send) | ||
else: | ||
await self.next_app(scope, receive, send) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import contextlib | ||
import sys | ||
from unittest import mock | ||
|
||
import pytest | ||
from connexion import AsyncApp, ConnexionMiddleware | ||
|
||
|
||
def test_lifespan_handler(app_class): | ||
m = mock.MagicMock() | ||
|
||
@contextlib.asynccontextmanager | ||
async def lifespan(app): | ||
m.startup() | ||
yield | ||
m.shutdown() | ||
|
||
app = AsyncApp(__name__, lifespan=lifespan) | ||
with app.test_client(): | ||
m.startup.assert_called() | ||
m.shutdown.assert_not_called() | ||
m.shutdown.assert_called() | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.version_info < (3, 8), reason="AsyncMock only available from 3.8." | ||
) | ||
async def test_lifespan(): | ||
"""Test that lifespan events are passed through if no handler is registered.""" | ||
lifecycle_handler = mock.Mock() | ||
|
||
async def check_lifecycle(scope, receive, send): | ||
if scope["type"] == "lifespan": | ||
lifecycle_handler.handle() | ||
|
||
test_app = ConnexionMiddleware(check_lifecycle) | ||
await test_app({"type": "lifespan"}, mock.AsyncMock(), mock.AsyncMock()) | ||
lifecycle_handler.handle.assert_called() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters