From 95692555a5467239eb0e011e89c13aff46f258f5 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 9 Jan 2020 22:55:02 +0200 Subject: [PATCH 1/2] Add version parameter to websockets --- sanic/app.py | 11 ++++++++++- tests/test_routes.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index 65e8480916..461e79dfde 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -451,7 +451,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 @@ -522,6 +528,7 @@ async def websocket_handler(request, *args, **kwargs): methods=frozenset({"GET"}), host=host, strict_slashes=strict_slashes, + version=version, name=name, ) return routes, handler @@ -535,6 +542,7 @@ def add_websocket_route( host=None, strict_slashes=None, subprotocols=None, + version=None, name=None, ): """ @@ -562,6 +570,7 @@ def add_websocket_route( host=host, strict_slashes=strict_slashes, subprotocols=subprotocols, + version=version, name=name, )(handler) diff --git a/tests/test_routes.py b/tests/test_routes.py index 3b24389ff0..c4876f6034 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -528,6 +528,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): From 7b5d9dd77cc09efcae63f30ce53a25890a00763a Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 9 Jan 2020 22:55:56 +0200 Subject: [PATCH 2/2] Run black and cleanup code --- tests/test_response.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_response.py b/tests/test_response.py index 87bda1bf5c..c6e16dd29c 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -15,13 +15,13 @@ from sanic.response import ( HTTPResponse, StreamingHTTPResponse, + empty, file, file_stream, json, raw, stream, ) -from sanic.response import empty from sanic.server import HttpProtocol from sanic.testing import HOST, PORT @@ -240,7 +240,7 @@ def test_non_chunked_streaming_adds_correct_headers(non_chunked_streaming_app): def test_non_chunked_streaming_returns_correct_content( - non_chunked_streaming_app + non_chunked_streaming_app, ): request, response = non_chunked_streaming_app.test_client.get("/") assert response.text == "foo,bar" @@ -255,7 +255,7 @@ def test_stream_response_status_returns_correct_headers(status): @pytest.mark.parametrize("keep_alive_timeout", [10, 20, 30]) def test_stream_response_keep_alive_returns_correct_headers( - keep_alive_timeout + keep_alive_timeout, ): response = StreamingHTTPResponse(sample_streaming_fn) headers = response.get_headers( @@ -284,7 +284,7 @@ def test_stream_response_does_not_include_chunked_header_if_disabled(): def test_stream_response_writes_correct_content_to_transport_when_chunked( - streaming_app + streaming_app, ): response = StreamingHTTPResponse(sample_streaming_fn) response.protocol = MagicMock(HttpProtocol)