Skip to content
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

🐛 Fix support for prefix on APIRouter WebSockets #2640

Merged
merged 3 commits into from Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi/routing.py
Expand Up @@ -649,7 +649,7 @@ def add_api_websocket_route(
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,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ws_router.py
Expand Up @@ -3,6 +3,7 @@

router = APIRouter()
prefix_router = APIRouter()
native_prefix_route = APIRouter(prefix="/native")
app = FastAPI()


Expand Down Expand Up @@ -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():
Expand All @@ -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:
Expand Down