Repository: sanic-org/sanic

Issue:
Websocket doesn't work when using version parameter
**Describe the bug**
When defining a websocket handler using `version=1` a exceptions will be thrown.

**Code snippet**
Relevant source code, make sure to remove what is not necessary.
```
@app.websocket("/changes", version=1)
```

**Expected behavior**
It should work like without `version=1`


**Environment (please complete the following information):**
 - OS: Fedora 31
 - Sanic: 19.12.2



Discussion:
I found out, that the websocket path is still `/changes` without `/v1` as prefix.
It's a routing issue.
`version` is missing here: https://github.com/huge-success/sanic/blob/master/sanic/app.py#L519
@danieldaeschle  Would you be able to submit a PR and perhaps a unit test?
I didn't found the root case yet. I'm not familiar with the sanic code. Do
you have a hint for me?
Otherwise i have to dig deeper an try to submit one.

Adam Hopkins <notifications@github.com> schrieb am Mo., 6. Jan. 2020, 21:12:

> @danieldaeschle <https://github.com/danieldaeschle> Would you be able to
> submit a PR and perhaps a unit test?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/huge-success/sanic/issues/1757?email_source=notifications&email_token=AHKTWHANZ7ML36I6OQIWJHTQ4OGCBA5CNFSM4KC3JRTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIGUK7Y#issuecomment-571295103>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AHKTWHDQTETSXJJN5FV3OQDQ4OGCBANCNFSM4KC3JRTA>
> .
>

I did a quick look at the code you linked to on my phone. I'm fairly certain that you pinpointed the right place. All you should need to do is add the version kwarg to the method and then to the call that you linked. If you want to just do that much I can then take a look and give you some more direction on where and how to add the test.
I already tried adding that kwarg. But that wasn't enough to work.

Adam Hopkins <notifications@github.com> schrieb am Di., 7. Jan. 2020, 07:14:

> I did a quick look at the code you linked to on my phone. I'm fairly
> certain that you pinpointed the right place. All you should need to do is
> add the version kwarg to the method and then to the call that you linked.
> If you want to just do that much I can then take a look and give you some
> more direction on where and how to add the test.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/huge-success/sanic/issues/1757?email_source=notifications&email_token=AHKTWHC43ITNNSLP44XTACLQ4QMUFA5CNFSM4KC3JRTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIH2SQY#issuecomment-571451715>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AHKTWHFJAN3GKFAO2DXRF73Q4QMUFANCNFSM4KC3JRTA>
> .
>

OK. Thanks for taking a shot. I can take this one in conjunction with upgrading websockets dependency.
Thanks!
Is it possible to get this fix soon?

Am Di., 7. Jan. 2020 um 18:04 Uhr schrieb Adam Hopkins <
notifications@github.com>:

> OK. Thanks for taking a shot. I can take this one in conjunction with
> upgrading websockets dependency.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/huge-success/sanic/issues/1757?email_source=notifications&email_token=AHKTWHC2CGRAAT5ZK5GOLI3Q4SYZTA5CNFSM4KC3JRTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIJR7NA#issuecomment-571678644>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AHKTWHF53DZANYMZSMP5MULQ4SYZTANCNFSM4KC3JRTA>
> .
>


Resolving patch:
diff --git a/sanic/app.py b/sanic/app.py
--- a/sanic/app.py
+++ b/sanic/app.py
@@ -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
@@ -536,6 +542,7 @@ async def websocket_handler(request, *args, **kwargs):
                     methods=frozenset({"GET"}),
                     host=host,
                     strict_slashes=strict_slashes,
+                    version=version,
                     name=name,
                 )
             )
@@ -550,6 +557,7 @@ def add_websocket_route(
         host=None,
         strict_slashes=None,
         subprotocols=None,
+        version=None,
         name=None,
     ):
         """
@@ -577,6 +585,7 @@ def add_websocket_route(
             host=host,
             strict_slashes=strict_slashes,
             subprotocols=subprotocols,
+            version=version,
             name=name,
         )(handler)
 


Test patch:
diff --git a/tests/test_routes.py b/tests/test_routes.py
--- a/tests/test_routes.py
+++ b/tests/test_routes.py
@@ -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):