Skip to content

Commit

Permalink
Add tests for websocket close (#9071)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 9, 2024
1 parent 841d00e commit a33270b
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,31 @@ async def on_shutdown(app: web.Application) -> None:
assert websocket.closed is True


@pytest.mark.xfail(reason="close never reaches client per issue #5180")
async def test_ws_close_return_code(aiohttp_client: AiohttpClient) -> None:
"""Test that the close code is returned when the server closes the connection."""

async def handler(request: web.Request) -> web.WebSocketResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
await ws.receive()
await ws.close()
return ws

app = web.Application()
app.router.add_route("GET", "/", handler)
client = await aiohttp_client(app)
resp = await client.ws_connect("/")
await resp.send_str("some data")
msg = await resp.receive()
assert msg.type is aiohttp.WSMsgType.CLOSE
assert resp.close_code == WSCloseCode.OK


async def test_abnormal_closure_when_server_does_not_receive(
aiohttp_client: AiohttpClient,
) -> None:
"""Test abnormal closure when the server closes and a message is pending."""

async def handler(request: web.Request) -> web.WebSocketResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
Expand All @@ -1168,5 +1189,6 @@ async def handler(request: web.Request) -> web.WebSocketResponse:
resp = await client.ws_connect("/")
await resp.send_str("some data")
await asyncio.sleep(0.1)
await resp.receive()
assert resp.close_code is WSCloseCode.OK
msg = await resp.receive()
assert msg.type is aiohttp.WSMsgType.CLOSE
assert resp.close_code == WSCloseCode.ABNORMAL_CLOSURE

0 comments on commit a33270b

Please sign in to comment.