Repository navigation
Fix FastAPI deep links with a router fallback #4235
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
Merged
+215
−33
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1f2ac0
fix(fastapi): scope catch-all to its backend
c0debeez 04457e0
docs: link FastAPI fix PR in changelog
c0debeez f1e11c8
Dispatch FastAPI deep links through the router fallback
c0debeez 04a7132
Merge remote-tracking branch 'origin/dev' into HEAD
T4rk1n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| """Verify the public routing behavior of the FastAPI Dash backend.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from dash import Dash, get_app, html | ||
|
|
||
| fastapi = pytest.importorskip("fastapi") | ||
| TestClient = pytest.importorskip("starlette.testclient").TestClient | ||
|
|
||
|
|
||
| def _make_app(title, **kwargs): | ||
| app = Dash(__name__, backend="fastapi", title=title, **kwargs) | ||
| app.layout = html.Div(title) | ||
| return app | ||
|
|
||
|
|
||
| def _assert_index(client, path, app): | ||
| response = client.get(path) | ||
| assert response.status_code == 200 | ||
| assert f"<title>{app.title}</title>" in response.text | ||
|
|
||
|
|
||
| def test_independent_servers(): | ||
| first = _make_app("First") | ||
| second = _make_app("Second") | ||
| for app in (first, second): | ||
| with TestClient(app.server) as client: | ||
| _assert_index(client, "/reports", app) | ||
|
|
||
|
|
||
| def test_without_lifespan(): | ||
| app = _make_app("Reports") | ||
| client = TestClient(app.server) | ||
| try: | ||
| _assert_index(client, "/reports", app) | ||
| finally: | ||
| client.close() | ||
|
|
||
|
|
||
| def test_repeated_lifespan_and_late_route(): | ||
| app = _make_app("Reports") | ||
| with TestClient(app.server) as client: | ||
| _assert_index(client, "/reports", app) | ||
|
|
||
| @app.server.get("/api/status") | ||
| async def status(): | ||
| return {"ok": True} | ||
|
|
||
| assert client.get("/api/status").json() == {"ok": True} | ||
|
|
||
| with TestClient(app.server) as client: | ||
| assert client.get("/api/status").json() == {"ok": True} | ||
| _assert_index(client, "/reports", app) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("prefix", ["/", "/app/"]) | ||
| def test_mounted_app(prefix): | ||
| parent = fastapi.FastAPI() | ||
| child = _make_app( | ||
| "Mounted", | ||
| routes_pathname_prefix=prefix, | ||
| requests_pathname_prefix="/dash" + prefix, | ||
| ) | ||
| parent.mount("/dash", child.server) | ||
| with TestClient(parent) as client: | ||
| _assert_index(client, "/dash" + prefix + "reports", child) | ||
| assert client.get("/dash" + prefix + "_dash-layout").json()["type"] == "Div" | ||
| assert client.get("/reports").status_code == 404 | ||
| if prefix != "/": | ||
| assert client.get("/dash/other").status_code == 404 | ||
|
|
||
|
|
||
| def test_prefix_and_http_methods(): | ||
| app = _make_app("Reports", url_base_pathname="/app/") | ||
| with TestClient(app.server) as client: | ||
| _assert_index(client, "/app/reports", app) | ||
| assert client.get("/other").status_code == 404 | ||
| assert client.get("/application/reports").status_code == 404 | ||
| assert client.post("/app/reports").status_code == 404 | ||
| response = client.head("/app/reports") | ||
| assert response.status_code == 200 | ||
| assert response.content == b"" | ||
| assert int(response.headers["content-length"]) > 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("prefixes", [("/", "/a/"), ("/a/", "/")]) | ||
| def test_shared_server_prefers_longest_prefix(prefixes): | ||
| server = fastapi.FastAPI() | ||
| apps = { | ||
| prefix: _make_app(f"App {prefix}", server=server, url_base_pathname=prefix) | ||
| for prefix in prefixes | ||
| } | ||
| with TestClient(server) as client: | ||
| _assert_index(client, "/reports", apps["/"]) | ||
| _assert_index(client, "/a/reports", apps["/a/"]) | ||
| _assert_index(client, "/ab/reports", apps["/"]) | ||
|
|
||
|
|
||
| def test_preserves_custom_fallback_and_explicit_errors(): | ||
| from starlette.responses import PlainTextResponse | ||
|
|
||
| server = fastapi.FastAPI() | ||
|
|
||
| async def fallback(scope, receive, send): | ||
| await PlainTextResponse("custom fallback", status_code=404)( | ||
| scope, receive, send | ||
| ) | ||
|
|
||
| server.router.default = fallback | ||
| app = _make_app("Reports", server=server, url_base_pathname="/app/") | ||
|
|
||
| @server.get("/app/missing") | ||
| async def missing(): | ||
| raise fastapi.HTTPException(status_code=404, detail="missing record") | ||
|
|
||
| with TestClient(server) as client: | ||
| _assert_index(client, "/app/reports", app) | ||
| assert client.get("/other").text == "custom fallback" | ||
| assert client.get("/app/missing").json() == {"detail": "missing record"} | ||
| assert client.post("/app/missing").status_code == 405 | ||
|
|
||
|
|
||
| def test_deep_link_runs_host_dependencies_and_overrides(): | ||
| calls = [] | ||
|
|
||
| async def record(): | ||
| calls.append("dependency") | ||
|
|
||
| async def override(): | ||
| calls.append("override") | ||
|
|
||
| server = fastapi.FastAPI(dependencies=[fastapi.Depends(record)]) | ||
| app = _make_app("Reports", server=server) | ||
| with TestClient(server) as client: | ||
| _assert_index(client, "/reports", app) | ||
| server.dependency_overrides[record] = override | ||
| _assert_index(client, "/reports", app) | ||
| assert calls == ["dependency", "override"] | ||
|
|
||
|
|
||
| def test_index_renders_with_its_app_context(): | ||
| class ContextDash(Dash): | ||
| def interpolate_index(self, **kwargs): | ||
| kwargs["title"] = get_app().title | ||
| return super().interpolate_index(**kwargs) | ||
|
|
||
| server = fastapi.FastAPI() | ||
| first = ContextDash(__name__, server=server, backend="fastapi", title="First") | ||
| first.layout = html.Div("First") | ||
| _make_app("Second", server=server, url_base_pathname="/a/") | ||
| with TestClient(server) as client: | ||
| _assert_index(client, "/", first) | ||
| _assert_index(client, "/reports", first) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HEAD /reportsnow returns 200 through the fallback, butHEAD /still returns 405 because this route is GET only. Flask answers HEAD on both._index_handleralready handles HEAD, so can you add"HEAD"here too?