mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-10 04:19:37 +00:00
Add routing middleware (#1497)
* Add routing middleware Factor out starlette BaseHTTPMiddleware Fix exceptions for starlette < 0.19 Fix docstring formatting Rename middleware/base.py to abstract.py Rework routing middleware * Clean up abstract API docstrings * Move connexion context into extensions * Allow empty middleware list
This commit is contained in:
44
tests/test_middleware.py
Normal file
44
tests/test_middleware.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
from connexion.middleware import ConnexionMiddleware
|
||||
from connexion.middleware.routing import CONNEXION_CONTEXT
|
||||
from starlette.datastructures import MutableHeaders
|
||||
|
||||
from conftest import SPECS, build_app_from_fixture
|
||||
|
||||
|
||||
class TestMiddleware:
|
||||
"""Middleware to check if operation is accessible on scope."""
|
||||
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
operation_id = scope['extensions'][CONNEXION_CONTEXT]['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", params=SPECS)
|
||||
def middleware_app(request):
|
||||
middlewares = ConnexionMiddleware.default_middlewares + [TestMiddleware]
|
||||
return build_app_from_fixture('simple', request.param, middlewares=middlewares)
|
||||
|
||||
|
||||
def test_routing_middleware(middleware_app):
|
||||
app_client = middleware_app.app.test_client()
|
||||
|
||||
response = app_client.post("/v1.0/greeting/robbe")
|
||||
|
||||
assert response.headers.get('operation_id') == 'fakeapi.hello.post_greeting', \
|
||||
response.status_code
|
||||
Reference in New Issue
Block a user