Skip to content

Commit

Permalink
add multi-body response test
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Mar 19, 2023
1 parent 309a675 commit 101c2bf
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,10 @@ async def websocket_session(url):
async with run_server(config):
await websocket_session(f"ws://127.0.0.1:{unused_tcp_port}")


from tests.response import Response


@pytest.mark.anyio
@pytest.mark.parametrize("ws_protocol_cls", WS_PROTOCOLS)
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
Expand All @@ -965,6 +967,58 @@ async def websocket_session(url):
async with websockets.client.connect(url):
pass # pragma: no cover
assert exc_info.value.status_code == 400
# Websockets module currently does not read the response body from the socket.

config = Config(
app=app,
ws=ws_protocol_cls,
http=http_protocol_cls,
lifespan="off",
port=unused_tcp_port,
)
async with run_server(config):
await websocket_session(f"ws://127.0.0.1:{unused_tcp_port}")


@pytest.mark.anyio
@pytest.mark.parametrize("ws_protocol_cls", WS_PROTOCOLS)
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
async def test_server_reject_connection_with_multibody_response(
ws_protocol_cls, http_protocol_cls, unused_tcp_port: int
):
async def app(scope, receive, send):
assert scope["type"] == "websocket"
assert "websocket.http.response" in scope["extensions"]

# Pull up first recv message.
message = await receive()
assert message["type"] == "websocket.connect"
message = {
"type": "websocket.http.response.start",
"status": 400,
"headers": [(b"Content-Length", b"20"), (b"Content-Type", b"text/plain")],
}
await send(message)
message = {
"type": "websocket.http.response.body",
"body": b"x" * 10,
"more_body": True,
}
await send(message)
message = {
"type": "websocket.http.response.body",
"body": b"y" * 10,
}
await send(message)
message = await receive()
assert message["type"] == "websocket.disconnect"

async def websocket_session(url):
with pytest.raises(websockets.exceptions.InvalidStatusCode) as exc_info:
async with websockets.client.connect(url):
pass # pragma: no cover
assert exc_info.value.status_code == 400
# Websockets module currently does not read the response body from the socket.

config = Config(
app=app,
Expand Down

0 comments on commit 101c2bf

Please sign in to comment.