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

attempt at sse e2e #3183

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 88 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,28 @@ include = [
'/CHANGELOG.rst',
]


[tool.pdm]
ignore_package_warnings = ["sphinx", "slotscheck"]

[tool.pdm.dev-dependencies]
dev = [
"beanie>=1.21.0",
"beautifulsoup4",
"fsspec",
"greenlet",
"hypothesis",
"python-dotenv",
"starlette",
"trio",
"aiosqlite",
"asyncpg>=0.29.0",
"psycopg[pool,binary]>=3.1.10",
"psycopg2-binary",
"psutil>=5.9.8",
"hypercorn>=0.16.0",
"daphne>=4.0.0",
"beanie>=1.21.0",
"beautifulsoup4",
"fsspec",
"greenlet",
"hypothesis",
"python-dotenv",
"starlette",
"trio",
"aiosqlite",
"asyncpg>=0.29.0",
"psycopg[pool,binary]>=3.1.10",
"psycopg2-binary",
"psutil>=5.9.8",
"hypercorn>=0.16.0",
"daphne>=4.0.0",
"pytest-playwright>=0.4.4",
]
dev-contrib = ["opentelemetry-sdk", "httpx-sse"]
docs = [
Expand Down
81 changes: 81 additions & 0 deletions tests/e2e/test_sse_js.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import asyncio
from contextlib import asynccontextmanager
from pathlib import Path
from typing import AsyncIterator

from playwright.async_api import async_playwright
from uvicorn.config import Config
from uvicorn.server import Server

from litestar import Litestar, get
from litestar.config.cors import CORSConfig
from litestar.response import ServerSentEvent, ServerSentEventMessage
from litestar.types import SSEData


@get("/test_sse_empty")
async def handler() -> ServerSentEvent:
async def generate() -> AsyncIterator[SSEData]:
event = ServerSentEventMessage(event="empty")
yield event

return ServerSentEvent(generate())


html = """<!DOCTYPE html>
<html lang="en">
<body>
<h2>Server-Sent Events</h2>
<div id="output"></div>
<script>
if (typeof EventSource !== "undefined") {
var eventSource = new EventSource("http://localhost:8765/test_sse_empty");
eventSource.onmessage = function (event) {
// console.log(event.data);
document.getElementById("output").innerHTML += event.data + "<br>";
};
eventSource.addEventListener("empty", (ev) => {
// console.log("empty arrived");
document.getElementById("output").innerHTML += "endx arrived" + "<br>";
eventSource.close();
});

eventSource.addEventListener("error", (err) => {
// console.error(err)
document.getElementById("output").innerHTML += "error: " + err + "<br>";
})
} else {
document.getElementById("output").innerHTML =
// If browser does not support SSE
"Sorry, the browser does not support SSE";
}
</script>
</body>
</html>"""


@asynccontextmanager
async def run_server(config: Config, sockets=None):
server = Server(config=config)
task = asyncio.create_task(server.serve(sockets=sockets))
await asyncio.sleep(0.1)
try:
yield server
finally:
await server.shutdown()
task.cancel()


async def test_get_started_link(tmp_path: Path) -> None:
app = Litestar(route_handlers=[handler], cors_config=CORSConfig(allow_origins=["*"]))
index = tmp_path / "index.html"
index.write_text(html)
async with run_server(config=Config(app=app, port=8765, lifespan="off")):
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto(f"file://{index.as_posix()}")
# Click the get started link.#
output_div_content = await page.query_selector("div#output")
print(await output_div_content.inner_text())
await browser.close()
Loading