def on_event(self, event_type: str) -> typing.Callable: # pragma: nocover
return self.router.on_event(event_type)
- def mount(self, path: str, app: ASGIApp, name: str = None) -> None:
+ def mount(
+ self, path: str, app: ASGIApp, name: str = None
+ ) -> None: # pragma: nocover
"""
We no longer document this API, and its usage is discouraged.
Instead you should use the following approach:
# The following usages are now discouraged in favour of configuration
# during Router.__init__(...)
- def mount(self, path: str, app: ASGIApp, name: str = None) -> None:
+ def mount(
+ self, path: str, app: ASGIApp, name: str = None
+ ) -> None: # pragma: nocover
"""
We no longer document this API, and its usage is discouraged.
Instead you should use the following approach:
methods: typing.List[str] = None,
name: str = None,
include_in_schema: bool = True,
- ) -> None:
+ ) -> None: # pragma: nocover
route = Route(
path,
endpoint=endpoint,
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import PlainTextResponse, StreamingResponse
-from starlette.routing import Route, WebSocketRoute
+from starlette.routing import Mount, Route, WebSocketRoute
class CustomMiddleware(BaseHTTPMiddleware):
assert text == "Hello, world!"
-def test_middleware_decorator(test_client_factory):
- app = Starlette()
-
- @app.route("/homepage")
- def homepage(request):
- return PlainTextResponse("Homepage")
-
- @app.middleware("http")
- async def plaintext(request, call_next):
- if request.url.path == "/":
- return PlainTextResponse("OK")
- response = await call_next(request)
- response.headers["Custom"] = "Example"
- return response
-
- client = test_client_factory(app)
- response = client.get("/")
- assert response.text == "OK"
-
- response = client.get("/homepage")
- assert response.text == "Homepage"
- assert response.headers["Custom"] == "Example"
-
-
def test_state_data_across_multiple_middlewares(test_client_factory):
expected_value1 = "foo"
expected_value2 = "bar"
response.headers["X-State-Bar"] = request.state.bar
return response
- app = Starlette()
- app.add_middleware(aMiddleware)
- app.add_middleware(bMiddleware)
- app.add_middleware(cMiddleware)
-
- @app.route("/")
def homepage(request):
return PlainTextResponse("OK")
+ app = Starlette(
+ routes=[Route("/", homepage)],
+ middleware=[
+ Middleware(aMiddleware),
+ Middleware(bMiddleware),
+ Middleware(cMiddleware),
+ ],
+ )
+
client = test_client_factory(app)
response = client.get("/")
assert response.text == "OK"
await call_next(request)
return PlainTextResponse("Custom")
- app = Starlette()
- app.add_middleware(CustomMiddleware)
+ app = Starlette(middleware=[Middleware(CustomMiddleware)])
client = test_client_factory(app)
response = client.get("/does_not_exist")
def test_exception_on_mounted_apps(test_client_factory):
sub_app = Starlette(routes=[Route("/", exc)])
- app.mount("/sub", sub_app)
+ app = Starlette(routes=[Mount("/sub", app=sub_app)])
client = test_client_factory(app)
with pytest.raises(Exception) as ctx: