vinissimus / async-asgi-testclient

A framework-agnostic library for testing ASGI web applications
MIT License
159 stars 20 forks source link

Client unable to handle websocket connection rejections #54

Open hollandjake opened 2 years ago

hollandjake commented 2 years ago

In the scenario where a websocket connection is closed before being accepted by the server, the WebSocketSession currently throws an AssertionError due to the received message not being type "websocket.accept" (see assertion)

If a connection request is cancelled before being accepted it should raise the message in someway so tests can process the response.

This should also apply to any message being received first that isn't an accept type since any of these would be a failure of the websocket protocol

hollandjake commented 2 years ago

Example test to show failure

@pytest.mark.asyncio
async def test():
  app = FastAPI()
  @app.websocket("/ws")
  async def ws(websocket: WebSocket):
    await websocket.close() # Reject the connection

  async with TestClient(app) as client:
    async with client.websocket_connect("/ws") as ws:
      pass
ThomasDsantos commented 1 year ago

Hello all ! Any update on this issue (and merge request linked) ? Will be useful for me too 😄

Merge request LGTM but can't do anything