mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-09 12:27:46 +00:00
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:
@@ -212,6 +212,10 @@ class SwaggerUIMiddleware(SpecMiddleware):
|
|||||||
self.router.mount(api.base_path, app=api.router)
|
self.router.mount(api.base_path, app=api.router)
|
||||||
|
|
||||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
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
|
_original_scope.set(scope.copy()) # type: ignore
|
||||||
await self.router(scope, receive, send)
|
await self.router(scope, receive, send)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import sys
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from connexion.middleware import ConnexionMiddleware
|
from connexion.middleware import ConnexionMiddleware
|
||||||
from starlette.datastructures import MutableHeaders
|
from starlette.datastructures import MutableHeaders
|
||||||
@@ -46,3 +49,19 @@ def test_routing_middleware(middleware_app):
|
|||||||
assert (
|
assert (
|
||||||
response.headers.get("operation_id") == "fakeapi.hello.post_greeting"
|
response.headers.get("operation_id") == "fakeapi.hello.post_greeting"
|
||||||
), response.status_code
|
), 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()
|
||||||
|
|||||||
Reference in New Issue
Block a user