Skip to content

Commit

Permalink
Allow WebSocket close event to receive reason being None from ASGI app (
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Nov 23, 2021
1 parent 8b05af3 commit 1e95a8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ async def connect(url):
@pytest.mark.parametrize("ws_protocol_cls", WS_PROTOCOLS)
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
@pytest.mark.parametrize("code", [None, 1000, 1001])
@pytest.mark.parametrize("reason", [None, "test"])
@pytest.mark.parametrize(
"reason",
[None, "test", False],
ids=["none_as_reason", "normal_reason", "without_reason"],
)
async def test_app_close(ws_protocol_cls, http_protocol_cls, code, reason):
async def app(scope, receive, send):
while True:
Expand All @@ -416,7 +420,7 @@ async def app(scope, receive, send):
if code is not None:
reply["code"] = code

if reason is not None:
if reason is not False:
reply["reason"] = reason

await send(reply)
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async def asgi_send(self, message):

elif message_type == "websocket.close":
code = message.get("code", 1000)
reason = message.get("reason", "")
reason = message.get("reason", "") or ""
await self.close(code, reason)
self.closed_event.set()

Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def send(self, message):
elif message_type == "websocket.close":
self.close_sent = True
code = message.get("code", 1000)
reason = message.get("reason", "")
reason = message.get("reason", "") or ""
self.queue.put_nowait({"type": "websocket.disconnect", "code": code})
output = self.conn.send(
wsproto.events.CloseConnection(code=code, reason=reason)
Expand Down

0 comments on commit 1e95a8f

Please sign in to comment.