@@ -458,6 +458,27 @@ async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable
458458 assert websocket .close_code == 1006
459459
460460
461+ async def test_close_transport_on_asgi_return (
462+ ws_protocol_cls : WSProtocol , http_protocol_cls : HTTPProtocol , unused_tcp_port : int
463+ ):
464+ """The ASGI callable should call the `websocket.close` event.
465+
466+ If it doesn't, the server should still send a close frame to the client.
467+ """
468+
469+ async def app (scope : Scope , receive : ASGIReceiveCallable , send : ASGISendCallable ):
470+ message = await receive ()
471+ if message ["type" ] == "websocket.connect" :
472+ await send ({"type" : "websocket.accept" })
473+
474+ config = Config (app = app , ws = ws_protocol_cls , http = http_protocol_cls , lifespan = "off" , port = unused_tcp_port )
475+ async with run_server (config ):
476+ async with websockets .client .connect (f"ws://127.0.0.1:{ unused_tcp_port } " ) as websocket :
477+ with pytest .raises (websockets .exceptions .ConnectionClosed ):
478+ await websocket .recv ()
479+ assert websocket .close_code == 1006
480+
481+
461482@pytest .mark .parametrize ("code" , [None , 1000 , 1001 ])
462483@pytest .mark .parametrize ("reason" , [None , "test" , False ], ids = ["none_as_reason" , "normal_reason" , "without_reason" ])
463484async def test_app_close (
0 commit comments