Skip to content

Commit 7d59ae0

Browse files
benz0liconsideRatio
authored andcommitted
Set subprotocols=None when there is no subprotocol proposed
- Modify tests to use headers - Remove SubprotocolWebSocket - Add test for empty and no subprotocols - Fix jupyterhub#442 - Fix jupyterhub#445
1 parent 26b59d8 commit 7d59ae0

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

jupyter_server_proxy/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ async def start_websocket_connection():
493493
request=request,
494494
on_message_callback=message_cb,
495495
on_ping_callback=ping_cb,
496-
subprotocols=self.subprotocols,
496+
subprotocols=self.subprotocols if self.subprotocols else None,
497497
resolver=resolver,
498498
)
499499
self._record_activity()

tests/resources/websocket.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def __init__(self):
3636
handlers = [
3737
(r"/", MainHandler),
3838
(r"/echosocket", EchoWebSocket),
39-
(r"/subprotocolsocket", SubprotocolWebSocket),
4039
(r"/headerssocket", HeadersWebSocket),
4140
]
4241
settings = dict(
@@ -63,19 +62,6 @@ def on_message(self, message):
6362
self.write_message(json.dumps(dict(self.request.headers)))
6463

6564

66-
class SubprotocolWebSocket(tornado.websocket.WebSocketHandler):
67-
def __init__(self, *args, **kwargs):
68-
self._subprotocols = None
69-
super().__init__(*args, **kwargs)
70-
71-
def select_subprotocol(self, subprotocols):
72-
self._subprotocols = subprotocols
73-
return None
74-
75-
def on_message(self, message):
76-
self.write_message(json.dumps(self._subprotocols))
77-
78-
7965
def main():
8066
tornado.options.parse_command_line()
8167
app = Application()

tests/test_proxies.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,13 @@ def test_server_proxy_websocket_headers(
374374

375375
async def _websocket_subprotocols(a_server_port_and_token: Tuple[int, str]) -> None:
376376
PORT, TOKEN = a_server_port_and_token
377-
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/subprotocolsocket"
377+
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/headerssocket"
378378
conn = await websocket_connect(url, subprotocols=["protocol_1", "protocol_2"])
379379
await conn.write_message("Hello, world!")
380380
msg = await conn.read_message()
381-
assert json.loads(msg) == ["protocol_1", "protocol_2"]
381+
headers = json.loads(msg)
382+
assert "Sec-Websocket-Protocol" in headers
383+
assert headers["Sec-Websocket-Protocol"] == "protocol_1,protocol_2"
382384

383385

384386
def test_server_proxy_websocket_subprotocols(
@@ -387,6 +389,39 @@ def test_server_proxy_websocket_subprotocols(
387389
event_loop.run_until_complete(_websocket_subprotocols(a_server_port_and_token))
388390

389391

392+
async def _websocket_empty_subprotocols(a_server_port_and_token: Tuple[int, str]) -> None:
393+
PORT, TOKEN = a_server_port_and_token
394+
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/headerssocket"
395+
conn = await websocket_connect(url, subprotocols=[])
396+
await conn.write_message("Hello, world!")
397+
msg = await conn.read_message()
398+
headers = json.loads(msg)
399+
assert "Sec-Websocket-Protocol" in headers
400+
assert headers["Sec-Websocket-Protocol"] == ""
401+
402+
403+
def test_server_proxy_websocket_empty_subprotocols(
404+
event_loop, a_server_port_and_token: Tuple[int, str]
405+
):
406+
event_loop.run_until_complete(_websocket_empty_subprotocols(a_server_port_and_token))
407+
408+
409+
async def _websocket_no_subprotocols(a_server_port_and_token: Tuple[int, str]) -> None:
410+
PORT, TOKEN = a_server_port_and_token
411+
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/headerssocket"
412+
conn = await websocket_connect(url)
413+
await conn.write_message("Hello, world!")
414+
msg = await conn.read_message()
415+
headers = json.loads(msg)
416+
assert "Sec-Websocket-Protocol" not in headers
417+
418+
419+
def test_server_proxy_websocket_no_subprotocols(
420+
event_loop, a_server_port_and_token: Tuple[int, str]
421+
):
422+
event_loop.run_until_complete(_websocket_no_subprotocols(a_server_port_and_token))
423+
424+
390425
@pytest.mark.parametrize(
391426
"proxy_path, status",
392427
[

0 commit comments

Comments
 (0)