Pass through lifespan events (#1673)

Fixes #1672

Lifespan events were being intercepted by the swagger UI middleware. We
should let them pass through.
This commit is contained in:
Robbe Sneyders
2023-03-13 23:34:00 +01:00
committed by GitHub
parent cd64611747
commit 8a85a4fe01
2 changed files with 23 additions and 0 deletions

View File

@@ -212,6 +212,10 @@ class SwaggerUIMiddleware(SpecMiddleware):
self.router.mount(api.base_path, app=api.router)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
_original_scope.set(scope.copy()) # type: ignore
await self.router(scope, receive, send)

View File

@@ -1,3 +1,6 @@
import sys
from unittest import mock
import pytest
from connexion.middleware import ConnexionMiddleware
from starlette.datastructures import MutableHeaders
@@ -46,3 +49,19 @@ def test_routing_middleware(middleware_app):
assert (
response.headers.get("operation_id") == "fakeapi.hello.post_greeting"
), response.status_code
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="AsyncMock only available from 3.8."
)
async def test_lifecycle():
"""Test that lifecycle events are passed correctly."""
lifecycle_handler = mock.Mock()
async def check_lifecycle(scope, receive, send):
if scope["type"] == "lifecycle":
lifecycle_handler.handle()
test_app = ConnexionMiddleware(check_lifecycle)
await test_app({"type": "lifecycle"}, mock.AsyncMock, mock.AsyncMock)
lifecycle_handler.handle.assert_called()