Files
connexion/tests/test_middleware.py
Robbe Sneyders 79c0852c93 Add lifespan middleware (#1676)
This PR adds a new middleware to handle lifespan events.

I added this as a separate middleware so it is encapsulated and aligned
for both the `FlaskApp` and `AsyncApp`. It leverages a Starlette
`Router` to register and call the lifespan handler.
2023-03-23 19:11:43 +01:00

52 lines
1.4 KiB
Python

import sys
from unittest import mock
import pytest
from connexion.middleware import ConnexionMiddleware
from starlette.datastructures import MutableHeaders
from conftest import build_app_from_fixture
class TestMiddleware:
"""Middleware to check if operation is accessible on scope."""
__test__ = False
def __init__(self, app, **kwargs):
self.app = app
async def __call__(self, scope, receive, send):
operation_id = scope["extensions"]["connexion_routing"]["operation_id"]
async def patched_send(message):
if message["type"] != "http.response.start":
await send(message)
return
message.setdefault("headers", [])
headers = MutableHeaders(scope=message)
headers["operation_id"] = operation_id
await send(message)
await self.app(scope, receive, patched_send)
@pytest.fixture(scope="session")
def middleware_app(spec, app_class):
middlewares = ConnexionMiddleware.default_middlewares + [TestMiddleware]
return build_app_from_fixture(
"simple", app_class=app_class, spec_file=spec, middlewares=middlewares
)
def test_routing_middleware(middleware_app):
app_client = middleware_app.test_client()
response = app_client.post("/v1.0/greeting/robbe")
assert (
response.headers.get("operation_id") == "fakeapi.hello.post_greeting"
), response.status_code