mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix support for path parameters in WebSockets (#3879)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
parent
0bb8920ae1
commit
d8b6aa630c
|
|
@ -297,14 +297,14 @@ class APIWebSocketRoute(routing.WebSocketRoute):
|
|||
self.path = path
|
||||
self.endpoint = endpoint
|
||||
self.name = get_name(endpoint) if name is None else name
|
||||
self.dependant = get_dependant(path=path, call=self.endpoint)
|
||||
self.path_regex, self.path_format, self.param_convertors = compile_path(path)
|
||||
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
|
||||
self.app = websocket_session(
|
||||
get_websocket_app(
|
||||
dependant=self.dependant,
|
||||
dependency_overrides_provider=dependency_overrides_provider,
|
||||
)
|
||||
)
|
||||
self.path_regex, self.path_format, self.param_convertors = compile_path(path)
|
||||
|
||||
def matches(self, scope: Scope) -> Tuple[Match, Scope]:
|
||||
match, child_scope = super().matches(scope)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,14 @@ async def routerindex2(websocket: WebSocket):
|
|||
await websocket.close()
|
||||
|
||||
|
||||
@router.websocket("/router/{pathparam:path}")
|
||||
async def routerindexparams(websocket: WebSocket, pathparam: str, queryparam: str):
|
||||
await websocket.accept()
|
||||
await websocket.send_text(pathparam)
|
||||
await websocket.send_text(queryparam)
|
||||
await websocket.close()
|
||||
|
||||
|
||||
async def ws_dependency():
|
||||
return "Socket Dependency"
|
||||
|
||||
|
|
@ -106,3 +114,14 @@ def test_router_ws_depends_with_override():
|
|||
app.dependency_overrides[ws_dependency] = lambda: "Override"
|
||||
with client.websocket_connect("/router-ws-depends/") as websocket:
|
||||
assert websocket.receive_text() == "Override"
|
||||
|
||||
|
||||
def test_router_with_params():
|
||||
client = TestClient(app)
|
||||
with client.websocket_connect(
|
||||
"/router/path/to/file?queryparam=a_query_param"
|
||||
) as websocket:
|
||||
data = websocket.receive_text()
|
||||
assert data == "path/to/file"
|
||||
data = websocket.receive_text()
|
||||
assert data == "a_query_param"
|
||||
|
|
|
|||
Loading…
Reference in New Issue