Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #9071/a33270bc backport][3.11] Add tests for websocket close #9076

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,10 +1121,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 @@ -1137,5 +1158,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
Loading