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

Add version parameter to websocket routes #1760

Merged
merged 5 commits into from
Jun 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,13 @@ def add_route(

# Decorator
def websocket(
self, uri, host=None, strict_slashes=None, subprotocols=None, name=None
self,
uri,
host=None,
strict_slashes=None,
subprotocols=None,
version=None,
ahopkins marked this conversation as resolved.
Show resolved Hide resolved
name=None,
):
"""
Decorate a function to be registered as a websocket route
Expand Down Expand Up @@ -536,6 +542,7 @@ async def websocket_handler(request, *args, **kwargs):
methods=frozenset({"GET"}),
host=host,
strict_slashes=strict_slashes,
version=version,
name=name,
)
)
Expand All @@ -550,6 +557,7 @@ def add_websocket_route(
host=None,
strict_slashes=None,
subprotocols=None,
version=None,
ahopkins marked this conversation as resolved.
Show resolved Hide resolved
name=None,
):
"""
Expand Down Expand Up @@ -577,6 +585,7 @@ def add_websocket_route(
host=host,
strict_slashes=strict_slashes,
subprotocols=subprotocols,
version=version,
ahopkins marked this conversation as resolved.
Show resolved Hide resolved
name=name,
)(handler)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,19 @@ async def handler(request, ws):
assert ev.is_set()


def test_add_webscoket_route_with_version(app):
ev = asyncio.Event()

async def handler(request, ws):
assert ws.subprotocol is None
ev.set()

app.add_websocket_route(handler, "/ws", version=1)
request, response = app.test_client.websocket("/v1/ws")
assert response.opened is True
assert ev.is_set()


def test_route_duplicate(app):

with pytest.raises(RouteExists):
Expand Down