diff --git a/pyproject.toml b/pyproject.toml index 84364ff..c3aa7a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,9 @@ dynamic = ["version"] [project.optional-dependencies] docs = [] test = [ + "asgi-lifespan", "httpx", + "httpx-ws", "pytest>=6", ] diff --git a/tests/test_application.py b/tests/test_application.py index 6919d9a..c918a6b 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -3,7 +3,12 @@ import shutil from pathlib import Path -from starlette.testclient import TestClient +import anyio +import httpx +import pytest +from asgi_lifespan import LifespanManager +from httpx_ws import aconnect_ws +from httpx_ws.transport import ASGIWebSocketTransport from sphinx_autobuild.__main__ import _create_app from sphinx_autobuild.build import Builder @@ -12,11 +17,18 @@ ROOT = Path(__file__).parent.parent -def test_application(tmp_path): +@pytest.fixture +def anyio_backend(): + return "asyncio" + + +async def test_application(tmp_path, anyio_backend): # noqa: ARG001 src_dir = tmp_path / "docs" out_dir = tmp_path / "build" shutil.copytree(ROOT / "docs", tmp_path / "docs") out_dir.mkdir(parents=True, exist_ok=True) + index_file = anyio.Path(src_dir / "index.rst") + await index_file.write_text("hello") url_host = "127.0.0.1:7777" ignore_handler = IgnoreFilter([out_dir], []) @@ -27,9 +39,20 @@ def test_application(tmp_path): post_build_commands=[], ) app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host) - client = TestClient(app) - builder(changed_paths=()) + async with ( + LifespanManager(app) as manager, + httpx.AsyncClient( + transport=ASGIWebSocketTransport(manager.app), base_url="http://testserver" + ) as client, + ): + builder(changed_paths=()) + + response = await client.get("/") + assert response.status_code == 200 + + async with aconnect_ws("/websocket-reload", client) as websocket: + await index_file.write_text("world") - response = client.get("/") - assert response.status_code == 200 + data = await websocket.receive_text() + assert data == "refresh"