From: Marcelo Trylesinski Date: Sun, 17 Apr 2022 17:21:53 +0000 (+0200) Subject: 🐛 Fix support for prefix on APIRouter WebSockets (#2640) X-Git-Tag: 0.75.2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d81c9081324758e4dd830b15e7abbb1817322b76;p=thirdparty%2Ffastapi%2Ffastapi.git 🐛 Fix support for prefix on APIRouter WebSockets (#2640) Co-authored-by: Sebastián Ramírez --- diff --git a/fastapi/routing.py b/fastapi/routing.py index 0f416ac42e..7a15f3965f 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -649,7 +649,7 @@ class APIRouter(routing.Router): self, path: str, endpoint: Callable[..., Any], name: Optional[str] = None ) -> None: route = APIWebSocketRoute( - path, + self.prefix + path, endpoint=endpoint, name=name, dependency_overrides_provider=self.dependency_overrides_provider, diff --git a/tests/test_ws_router.py b/tests/test_ws_router.py index bd7c3c53d6..fbca104a23 100644 --- a/tests/test_ws_router.py +++ b/tests/test_ws_router.py @@ -3,6 +3,7 @@ from fastapi.testclient import TestClient router = APIRouter() prefix_router = APIRouter() +native_prefix_route = APIRouter(prefix="/native") app = FastAPI() @@ -47,8 +48,16 @@ async def router_ws_decorator_depends( await websocket.close() +@native_prefix_route.websocket("/") +async def router_native_prefix_ws(websocket: WebSocket): + await websocket.accept() + await websocket.send_text("Hello, router with native prefix!") + await websocket.close() + + app.include_router(router) app.include_router(prefix_router, prefix="/prefix") +app.include_router(native_prefix_route) def test_app(): @@ -72,6 +81,13 @@ def test_prefix_router(): assert data == "Hello, router with prefix!" +def test_native_prefix_router(): + client = TestClient(app) + with client.websocket_connect("/native/") as websocket: + data = websocket.receive_text() + assert data == "Hello, router with native prefix!" + + def test_router2(): client = TestClient(app) with client.websocket_connect("/router2") as websocket: