vinissimus/async-asgi-testclient

WebSocket connection with query params - failed

grubberr opened this issue · 4 comments

Hello,

Thanks for your library but it seems it has some problems which has to be resolved

WebSocket URL with query params does not work

#!/usr/bin/env python3
  
import asyncio
from fastapi import FastAPI
from starlette.websockets import WebSocket
from async_asgi_testclient import TestClient

app = FastAPI()

url1 = '/ws' # works ok
url2 = '/ws?token=token'  # failed


@app.websocket_route('/ws')
async def websocket_endpoint(websocket: WebSocket):

    await websocket.accept()
    await websocket.send_text('Hello')


async def main():
    async with TestClient(app) as client:
        async with client.websocket_connect(url2) as websocket:
            print(await websocket.receive_text())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

as I understand the problem, you build such scope

{'headers': [(b'host', b'localhost')],
 'path': '/ws?token=token',
 'query_string': b'',
 'root_path': '',
 'scheme': 'ws',
 'subprotocols': [],
 'type': 'websocket'}

but it has to be something like

{'headers': [(b'host', b'localhost')],
 'path': '/ws',
 'query_string': b'token=token',
 'root_path': '',
 'scheme': 'ws',
 'subprotocols': [],
 'type': 'websocket'}

Hello,

You are right. We forgot to add the parameter query_string in websocket_connect() (as in the regular open() method).

I encourage you to open a PR and fix it yourself, if you don't mind (I can help you with the fix if you need it).

Thank you!

I have created PR
#15
but use it please only as reference :)

Version 1.1.1 released