sanic-org / sanic-testing

Test clients for Sanic
https://sanic.dev/en/plugins/sanic-testing/getting-started.html
MIT License
31 stars 19 forks source link

[Bug] Websocket testing .recv() #53

Closed jmgirven closed 1 year ago

jmgirven commented 2 years ago

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")
3. 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

ahopkins commented 1 year ago

Resolved by https://github.com/sanic-org/sanic/pull/2640