Example #1
0
async def test_set_context_method(
    mocked_request: Request,
    mocked_middleware: ContextMiddleware,
):

    mocked_middleware.plugins = [plugins.DateHeaderPlugin()]
    rfc1123_date = mocked_request.headers[HeaderKeys.date][:25]
    dt_date = plugins.DateHeaderPlugin.rfc1123_to_dt(rfc1123_date)

    assert {
        plugins.DateHeaderPlugin.key: dt_date
    } == await mocked_middleware.set_context(mocked_request)
Example #2
0
 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(),
     ))
Example #3
0
def plugin():
    return plugins.DateHeaderPlugin()
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)
Example #5
0
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 starlette_context.plugins import DateHeaderPlugin

middleware = [
    Middleware(
        ContextMiddleware,
        plugins=(plugins.DateHeaderPlugin(), ),
    )
]
app = Starlette(middleware=middleware)
client = TestClient(app)


@app.route("/")
async def index(request: Request) -> Response:
    return JSONResponse({"headers": str(request.headers.get(HeaderKeys.date))})


@pytest.mark.parametrize(
    "date_header",
    [
        "Wed, 01 Jan 2020 04:27:12 GMT",