[Bug] Websocket testing .recv()
jmgirven opened this issue · comments
I am trying to test a websocket endpoint that uses websocket.recv()
inside, but this method is throwing an exception.
Error
ERROR sanic.error:handlers.py:183 Exception occurred while handling uri: 'http:///ws'
Traceback (most recent call last):
File "/worker/.local/lib/python3.7/site-packages/sanic/app.py", line 971, in _websocket_handler
await fut
File "/app/application_tests/test_sanic_testing.py", line 18, in handler
await ws.recv()
File "/worker/.local/lib/python3.7/site-packages/sanic/server/websockets/connection.py", line 49, in recv
if message["type"] == "websocket.receive":
KeyError: 'type'
How to reproduce
- Create new venv and install packages
- Create test_test.py file with this contents
import pytest
from sanic import Sanic
from sanic_testing import TestManager
@pytest.fixture
def app():
sanic_app = Sanic("test")
TestManager(sanic_app)
return sanic_app
@pytest.mark.asyncio
async def test_websocket_route(app):
@app.websocket("/ws")
async def handler(request, ws):
await ws.recv()
await app.asgi_client.websocket("/ws")
- Run
pytest
and see the error message
Requirements
Python 3.7
pytest-asyncio==0.19.0
sanic==22.6.1
sanic-testing==22.6.0
Resolved by sanic-org/sanic#2640