Skip to content

Commit

Permalink
Add version parameter to websocket routes (#1760)
Browse files Browse the repository at this point in the history
* Add version parameter to websockets

* Run black and cleanup code
  • Loading branch information
ahopkins authored Jun 28, 2020
1 parent 6239fa4 commit 6c8e20a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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,
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,
name=None,
):
"""
Expand Down Expand Up @@ -577,6 +585,7 @@ def add_websocket_route(
host=host,
strict_slashes=strict_slashes,
subprotocols=subprotocols,
version=version,
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

0 comments on commit 6c8e20a

Please sign in to comment.