BlackSheep icon indicating copy to clipboard operation
BlackSheep copied to clipboard

feat: Added support for WebSocket in TestClient

Open Randomneo opened this issue 5 months ago • 0 comments

Great package. TY for your hard work. I'm really enjoying blacksheep so far. But it feels like its missing a bit of WebSocket testing support.

Added starlette like WebSocket testing.

from blacksheep import Application, WebSocket, ws
from blacksheep.testing import TestClient

app = Application()

@ws('/ws')
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    await websocket.send_text('hello world')

@pytest.mark.asyncio
async def test_test_websocket():
    await app.start()
    client = TestClient(app)
    async with client.websocket_connect('/ws') as websocket:
        received = await websocket.receive_text()
    assert received == 'hello world'

Randomneo avatar Mar 15 '24 20:03 Randomneo