vinissimus / async-asgi-testclient

A framework-agnostic library for testing ASGI web applications
MIT License
159 stars 20 forks source link

WebSocket connection with query params - failed #14

Closed grubberr closed 4 years ago

grubberr commented 4 years ago

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())
grubberr commented 4 years ago

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'}
masipcat commented 4 years ago

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!

grubberr commented 4 years ago

I have created PR https://github.com/vinissimus/async-asgi-testclient/pull/15 but use it please only as reference :)

masipcat commented 4 years ago

Version 1.1.1 released