def __registerMiddleWares(self): application = loadCordsMiddleware(self.applicationObject) '''Load middlewares''' if self.__startLogSystem(): application.middleware('http')(registerLoggerMiddleware(self.applicationObject)) application.middleware('http')(registerConfigMiddleware(self.applicationObject)) application.add_middleware(ContextMiddleware.with_plugins( plugins.CorrelationIdPlugin(), plugins.RequestIdPlugin(), plugins.DateHeaderPlugin(), plugins.ForwardedForPlugin(), plugins.UserAgentPlugin(), ))
from starlette import status from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.requests import Request from starlette.responses import JSONResponse, Response from starlette.testclient import TestClient from starlette_context import plugins from starlette_context.header_keys import HeaderKeys from starlette_context.middleware import ContextMiddleware from tests.conftest import dummy_forwarded_for middleware = [ Middleware( ContextMiddleware, plugins=(plugins.ForwardedForPlugin(), ), ) ] app = Starlette(middleware=middleware) client = TestClient(app) headers = {HeaderKeys.forwarded_for: dummy_forwarded_for} @app.route("/") async def index(request: Request) -> Response: return JSONResponse( {"headers": str(request.headers.get(HeaderKeys.forwarded_for))}) def test_valid_request_returns_proper_response(): response = client.get("/", headers=headers)
from starlette import status from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.requests import Request from starlette.responses import JSONResponse from starlette.testclient import TestClient from starlette_context import context, plugins from starlette_context.header_keys import HeaderKeys from starlette_context.middleware import RawContextMiddleware plugins_to_use = ( plugins.CorrelationIdPlugin(), plugins.RequestIdPlugin(), plugins.UserAgentPlugin(), plugins.ForwardedForPlugin(), plugins.DateHeaderPlugin(), ) middleware = [Middleware( RawContextMiddleware, plugins=plugins_to_use, )] app = Starlette(middleware=middleware) client = TestClient(app) @app.route("/") async def index(request: Request) -> JSONResponse: return JSONResponse(content=context.data)
def plugin(): return plugins.ForwardedForPlugin()