2022-06-15 13:22:18 +00:00
|
|
|
from starlette.middleware import Middleware
|
2023-12-20 21:09:17 +00:00
|
|
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
2022-06-15 13:22:18 +00:00
|
|
|
|
|
|
|
|
2023-12-20 21:09:17 +00:00
|
|
|
class CustomMiddleware: # pragma: no cover
|
|
|
|
def __init__(self, app: ASGIApp, foo: str, *, bar: int) -> None:
|
|
|
|
self.app = app
|
|
|
|
self.foo = foo
|
|
|
|
self.bar = bar
|
2022-06-15 13:22:18 +00:00
|
|
|
|
2023-12-20 21:09:17 +00:00
|
|
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
|
|
await self.app(scope, receive, send)
|
2022-06-15 13:22:18 +00:00
|
|
|
|
2023-12-20 21:09:17 +00:00
|
|
|
|
|
|
|
def test_middleware_repr() -> None:
|
|
|
|
middleware = Middleware(CustomMiddleware, "foo", bar=123)
|
|
|
|
assert repr(middleware) == "Middleware(CustomMiddleware, 'foo', bar=123)"
|
|
|
|
|
|
|
|
|
|
|
|
def test_middleware_iter() -> None:
|
|
|
|
cls, args, kwargs = Middleware(CustomMiddleware, "foo", bar=123)
|
|
|
|
assert (cls, args, kwargs) == (CustomMiddleware, ("foo",), {"bar": 123})
|