Skip to content

Commit

Permalink
Test abnormal closure when client does not respond to close
Browse files Browse the repository at this point in the history
  • Loading branch information
lenard-mosys committed Oct 21, 2024
1 parent 476e37f commit 4e3b283
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,30 @@ async def handler(request: web.Request) -> web.WebSocketResponse:
msg = await resp.receive()
assert msg.type is aiohttp.WSMsgType.CLOSE
assert resp.close_code == WSCloseCode.ABNORMAL_CLOSURE


async def test_abnormal_closure_when_client_does_not_close(
aiohttp_client: AiohttpClient,
) -> None:
"""Test abnormal closure when the server closes and the client doesn't respond."""

close_code: Optional[WSCloseCode] = None

async def handler(request: web.Request) -> web.WebSocketResponse:
# Setting a short close timeout
ws = web.WebSocketResponse(timeout=0.1)
await ws.prepare(request)
await ws.close()

nonlocal close_code
close_code = WSCloseCode(ws.close_code)

return ws

app = web.Application()
app.router.add_route("GET", "/", handler)
client = await aiohttp_client(app)
async with client.ws_connect("/", autoclose=False):
await asyncio.sleep(0.2)
await client.server.close()
assert close_code == WSCloseCode.ABNORMAL_CLOSURE

0 comments on commit 4e3b283

Please sign in to comment.