sanic-org / sanic-testing

Test clients for Sanic

Home Page:https://sanic.dev/en/plugins/sanic-testing/getting-started.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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

  1. Create new venv and install packages
  2. 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")
  1. 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