Browse Source

Support and test app.websocket_route

pull/12145/head
Synrom 7 months ago
parent
commit
4fb5d22a73
  1. 10
      fastapi/routing.py
  2. 18
      tests/test_ignore_trailing_slash.py

10
fastapi/routing.py

@ -1040,6 +1040,16 @@ class APIRouter(routing.Router):
return decorator
def add_websocket_route(
self,
path: str,
endpoint: Callable[..., Any],
name: Optional[str] = None,
) -> None:
if self.ignore_trailing_slash:
path = path.rstrip("/")
super().add_websocket_route(path, endpoint, name)
def add_api_websocket_route(
self,
path: str,

18
tests/test_ignore_trailing_slash.py

@ -29,6 +29,20 @@ async def websocket_endpoint_with_slash(websocket: WebSocket):
await websocket.close()
@app.websocket_route("/websocket_route")
async def websocket_route_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket route")
await websocket.close()
@app.websocket_route("/websocket_route_2/")
async def websocket_route_endpoint_with_slash(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket route 2")
await websocket.close()
@router.get("/example")
def route_endpoint():
return {"msg": "Routing Example"}
@ -58,6 +72,10 @@ def test_ignoring_trailing_shlash_ws():
assert websocket.receive_text() == "Websocket"
with client.websocket_connect("/websocket2") as websocket:
assert websocket.receive_text() == "Websocket 2"
with client.websocket_connect("/websocket_route/") as websocket:
assert websocket.receive_text() == "Websocket route"
with client.websocket_connect("/websocket_route_2/") as websocket:
assert websocket.receive_text() == "Websocket route 2"
def test_ignoring_trailing_routing():

Loading…
Cancel
Save