From bda286314a911f5144f4f0f67d27b347506b8c47 Mon Sep 17 00:00:00 2001 From: Peter Volf Date: Tue, 8 Oct 2024 10:39:26 +0200 Subject: [PATCH] add tests for duplicate special dependency bug fix --- tests/test_duplicate_special_dependencies.py | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test_duplicate_special_dependencies.py diff --git a/tests/test_duplicate_special_dependencies.py b/tests/test_duplicate_special_dependencies.py new file mode 100644 index 000000000..82944da1f --- /dev/null +++ b/tests/test_duplicate_special_dependencies.py @@ -0,0 +1,62 @@ +import pytest +from fastapi import BackgroundTasks, FastAPI, Request, Response, WebSocket +from fastapi.security import SecurityScopes +from fastapi.testclient import TestClient + +app = FastAPI() + + +@app.get("/request") +def request(r1: Request, r2: Request) -> str: + assert r1 is not None + assert r1 is r2 + return "success" + + +@app.get("/response") +def response(r1: Response, r2: Response) -> str: + assert r1 is not None + assert r1 is r2 + return "success" + + +@app.get("/background-tasks") +def background_tasks(t1: BackgroundTasks, t2: BackgroundTasks) -> str: + assert t1 is not None + assert t1 is t2 + return "success" + + +@app.get("/websocket") +def websocket(ws1: WebSocket, ws2: WebSocket) -> str: + assert ws1 is not None + assert ws1 is ws2 + return "success" + + +@app.get("/security-scopes") +def security_scopes(sc1: SecurityScopes, sc2: SecurityScopes) -> str: + assert sc1 is not None + assert sc1 is sc2 + return "success" + + +client = TestClient(app) + + +@pytest.mark.parametrize( + "url", + ( + "/request", + "/response", + "/background-tasks", + "/security-scopes", + ), +) +def test_duplicate_special_dependency(url: str) -> None: + assert client.get(url).text == '"success"' + + +def test_duplicate_websocket_dependency() -> None: + # Raises exception if connect fails. + client.websocket_connect("/websocket")