klen / asgi-tools

Tools to build ASGI apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ASGITestClient websocket connection scope missing "subprotocols"

villekr opened this issue · comments

ASGITestClient build_scope is missing "subprotocols".

https://asgi.readthedocs.io/en/latest/specs/www.html#websocket-connection-scope
...
subprotocols (Iterable[Unicode string]) – Subprotocols the client advertised. Optional; if missing defaults to empty list.

Also, test client websocket example in the documentation is incorrect (it's missing websocket-context manager). In my case that would be something like this. For example code, the headers are not that important.

async def test_app(app):
    client = ASGITestClient(app)
    headers = {"Sec-WebSocket-Protocol": "ocpp1.6, ocpp2.0.1"}
    async with client.websocket(path="/123", headers=headers) as ws:
        await ws.send("ping")
        msg = await ws.receive()
        assert msg == "pong"

Anyway, nice library.

@villekr Thank you for the feedback. I've fixed the example.

Although ASGI subprotocols is optional, I added the option for WebSockets call (asgi-tools 0.61.2).

Thanks for quick action!

"subprotocols" key for asgi websocket connection scope should be build from "Sec-WebSocket-Protocol" header's values (https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism). Now it's allowed but always empty.

Oh, I didn't know it. Have made some changes to this, please check (asgi-tools 0.61.4).

Excellent, works for me now!