Skip to content

Commit ba366e3

Browse files
committed
test
1 parent b0fe041 commit ba366e3

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

tests/shared/test_sse.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,29 +157,35 @@ async def http_client(server, server_url) -> AsyncGenerator[httpx.AsyncClient, N
157157
yield client
158158

159159

160-
# Tests
161160
@pytest.mark.anyio
162161
async def test_raw_sse_connection(http_client: httpx.AsyncClient) -> None:
163162
"""Test the SSE connection establishment simply with an HTTP client."""
164163
try:
165-
async with http_client.stream("GET", "/sse", timeout=5, follow_redirects=True) as response:
166-
assert response.status_code == 200
167-
assert (
168-
response.headers["content-type"]
169-
== "text/event-stream; charset=utf-8"
170-
)
171-
172-
line_number = 0
173-
async for line in response.aiter_lines():
174-
if line_number == 0:
175-
assert line == "event: endpoint"
176-
elif line_number == 1:
177-
assert line.startswith("data: /messages/?session_id=")
178-
else:
179-
return
180-
line_number += 1
181-
except httpx.HTTPStatusError as e:
182-
assert False, f"HTTP error occurred: {e}"
164+
async with anyio.create_task_group():
165+
166+
async def connection_test() -> None:
167+
async with http_client.stream("GET", "/sse") as response:
168+
assert response.status_code == 200
169+
assert (
170+
response.headers["content-type"]
171+
== "text/event-stream; charset=utf-8"
172+
)
173+
174+
line_number = 0
175+
async for line in response.aiter_lines():
176+
if line_number == 0:
177+
assert line == "event: endpoint"
178+
elif line_number == 1:
179+
assert line.startswith("data: /messages/?session_id=")
180+
else:
181+
return
182+
line_number += 1
183+
184+
# Add timeout to prevent test from hanging if it fails
185+
with anyio.fail_after(3):
186+
await connection_test()
187+
except Exception as e:
188+
pytest.fail(f"HTTP error occurred:{e}")
183189

184190

185191
@pytest.mark.anyio

0 commit comments

Comments
 (0)