Skip to content

Instantly share code, notes, and snippets.

@jordic
Created May 7, 2018 20:02
Show Gist options
  • Save jordic/f57039d9002b76eb61c8e78f768ba2e2 to your computer and use it in GitHub Desktop.
Save jordic/f57039d9002b76eb61c8e78f768ba2e2 to your computer and use it in GitHub Desktop.
ws test
async def test_websocket_receive_json(loop, aiohttp_client):
async def handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
data = await ws.receive_json()
answer = data['test']
await ws.send_str(answer)
await ws.close()
return ws
app = web.Application()
app.router.add_route('GET', '/', handler)
client = await aiohttp_client(app)
ws = await client.ws_connect('/')
expected_value = 'value'
payload = '{"test": "%s"}' % expected_value
await ws.send_str(payload)
resp = await ws.receive()
assert resp.data == expected_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment