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

Add functionality to mimic a websocket client against an endpoint #50

Merged
merged 3 commits into from
Jul 26, 2022

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Jul 24, 2022

This PR is meant to introduce a new API to test websocket endpoints. To do this, the developer would pass an async function that can send and recv messages from the endpoint. To test, these values will be stored and accessible to assert against.

def test_websocket_route_queue(app: Sanic):
    async def client_mimic(websocket: WebSocketClientProtocol):
        await websocket.send("foo")
        await websocket.recv()

    @app.websocket("/ws")
    async def handler(request, ws: Websocket):
        while True:
            await ws.send("hello!")
            if not await ws.recv():
                break

    _, response = app.test_client.websocket("/ws", mimic=client_mimic)
    assert response.server_sent == ["hello!"]
    assert response.server_received == ["foo", ""]

@ahopkins ahopkins marked this pull request as ready for review July 25, 2022 21:36
@ahopkins ahopkins changed the title Initial implementation of websocket mimic Add functionality to mimic a websocket client against an endpoint Jul 25, 2022
@ahopkins ahopkins merged commit c460a86 into main Jul 26, 2022
ChihweiLHBird pushed a commit to ChihweiLHBird/sanic-testing that referenced this pull request Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants