-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Mount(..., middleware=[...]) #1649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
42548cf
01bbcdc
d8b626d
73dc39e
14d3005
8eb2699
baab334
bbca389
a75a523
578f618
ba59a35
3cadfb2
1eace2b
ca50340
f65dfc8
5edb100
1ef66e6
fb93ef5
273cc73
4369ee7
10f47ae
feeba5e
0bb54e4
d7c3f2a
e6fad81
adb52ab
0f7a2c4
f6de20f
72fbaa6
eee6a6f
aec580f
93ec37f
5f936e9
0079756
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,20 @@ | |
| import pytest | ||
|
|
||
| from starlette.applications import Starlette | ||
| from starlette.middleware import Middleware | ||
| from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse, PlainTextResponse, Response | ||
| from starlette.routing import Host, Mount, NoMatchFound, Route, Router, WebSocketRoute | ||
| from starlette.routing import ( | ||
| BaseRoute, | ||
| Host, | ||
| Mount, | ||
| NoMatchFound, | ||
| Route, | ||
| Router, | ||
| WebSocketRoute, | ||
| ) | ||
| from starlette.testclient import TestClient | ||
| from starlette.websockets import WebSocket, WebSocketDisconnect | ||
|
|
||
|
|
||
|
|
@@ -746,3 +758,93 @@ def __call__(self, request): | |
| ) | ||
| def test_route_name(endpoint: typing.Callable, expected_name: str): | ||
| assert Route(path="/", endpoint=endpoint).name == expected_name | ||
|
|
||
|
|
||
| def assert_middleware_header_route(request: Request): | ||
| assert getattr(request.state, "middleware_touched") == "Set by middleware" | ||
| return Response() | ||
|
|
||
|
|
||
| class AddHeadersMiddleware(BaseHTTPMiddleware): | ||
florimondmanca marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| async def dispatch( | ||
| self, request: Request, call_next: RequestResponseEndpoint | ||
| ) -> Response: | ||
| setattr(request.state, "middleware_touched", "Set by middleware") | ||
| response: Response = await call_next(request) | ||
| response.headers["X-Test"] = "Set by middleware" | ||
| return response | ||
|
|
||
|
|
||
| mounted_routes_with_middleware = Mount( | ||
| "/http", | ||
| routes=[ | ||
| Route( | ||
| "/", | ||
| endpoint=assert_middleware_header_route, | ||
| methods=["GET"], | ||
| name="route", | ||
| ), | ||
| ], | ||
| middleware=[Middleware(AddHeadersMiddleware)], | ||
| ) | ||
|
|
||
|
|
||
| mounted_app_with_middleware = Mount( | ||
| "/http", | ||
| app=Route( | ||
| "/", | ||
| endpoint=assert_middleware_header_route, | ||
| methods=["GET"], | ||
| name="route", | ||
| ), | ||
| middleware=[Middleware(AddHeadersMiddleware)], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "route", | ||
| [ | ||
| mounted_routes_with_middleware, | ||
| mounted_routes_with_middleware, | ||
Kludex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mounted_app_with_middleware, | ||
| ], | ||
| ) | ||
| def test_mount_middleware( | ||
| test_client_factory: typing.Callable[..., TestClient], | ||
| route: BaseRoute, | ||
| ) -> None: | ||
| test_client = test_client_factory(Router([route])) | ||
| response = test_client.get("/http") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in eee6a6f |
||
| assert response.status_code == 200 | ||
| assert response.headers["X-Test"] == "Set by middleware" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "route", | ||
| [ | ||
| mounted_routes_with_middleware, | ||
| mounted_routes_with_middleware, | ||
|
||
| ], | ||
| ) | ||
| def test_mount_middleware_url_path_for(route: BaseRoute) -> None: | ||
| """Checks that url_path_for still works with middleware on Mounts""" | ||
| router = Router([route]) | ||
| assert router.url_path_for("route") == "/http/" | ||
|
|
||
|
|
||
| def test_add_route_to_app_after_mount( | ||
| test_client_factory: typing.Callable[..., TestClient], | ||
| ) -> None: | ||
| """Checks that Mount will pick up routes | ||
| added to the underlying app after it is mounted | ||
| """ | ||
| inner_app = Router() | ||
| app = Mount("/http", app=inner_app) | ||
| inner_app.add_route( | ||
| "/inner", | ||
| endpoint=lambda request: Response(), | ||
| methods=["GET"], | ||
| ) | ||
| client = test_client_factory(app) | ||
| response = client.get("/http/inner") | ||
| assert response.status_code == 200 | ||
Uh oh!
There was an error while loading. Please reload this page.