Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event message type on closing #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lulingar
Copy link

@lulingar lulingar commented Feb 13, 2025

Aims to fix #97.

Unit tests pass, and makes the test code provided in the aforementioned issue to run without exceptions:

import asyncio
import logging
import httpx
from httpx_ws import aconnect_ws
from httpx_ws.transport import ASGIWebSocketTransport
from starlette.applications import Starlette
from starlette.routing import WebSocketRoute
from starlette.websockets import WebSocketDisconnect


async def ws_hello(websocket):
    try:
        await websocket.accept()
        await websocket.send_text("Hello World!")
        await websocket.receive_text()
    except WebSocketDisconnect:
        pass
    except Exception as e:
        logging.exception(e)

app = Starlette(
    routes=[
        WebSocketRoute("/ws", ws_hello),
    ],
)


async def main():
    async with httpx.AsyncClient(transport=ASGIWebSocketTransport(app)) as client:
        async with aconnect_ws("http://server/ws", client) as ws:
            message = await ws.receive_text()
            assert message == "Hello World!"

asyncio.run(main())

Unit tests using httpx-ws against Starlette's WebSocket implementation
were failing due to an erroneous message type being received by the
server upon client disconnect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing ASGI wrong message event
1 participant