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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix support for path parameters in WebSockets #3879

Merged
merged 7 commits into from Sep 1, 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
4 changes: 2 additions & 2 deletions fastapi/routing.py
Expand Up @@ -297,14 +297,14 @@ def __init__(
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)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ws_router.py
Expand Up @@ -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"

Expand Down Expand Up @@ -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"