swadhinbiswas test(py): add comprehensive test suite for Python bindings · 7/8/2026
f693697
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Integration tests for the @app.websocket() and @app.sse() decorators."""
import multiprocessing
import time
import pytest
import requests
from justapi import JustAPIApp
WEBSOCKETS_AVAILABLE = True
try:
import websockets
except ImportError:
WEBSOCKETS_AVAILABLE = False
PORT = 8091
def run_server():
app = JustAPIApp()
@app.websocket("/ws")
async def ws_echo(ws):
await ws.accept()
msg = await ws.receive_text()
await ws.send_text("echo:" + msg)
await ws.close()
@app.sse("/stream")
async def sse_stream(req):
for i in range(3):
yield f"data: {i}\n\n"
@app.get("/hello")
def hello(req):
return {"message": "hello"}
app.run(f"127.0.0.1:{PORT}")
@pytest.fixture(scope="module")
def server():
p = multiprocessing.Process(target=run_server)
p.start()
time.sleep(1.5)
try:
yield
finally:
p.terminate()
p.join()
def test_sse_stream(server):
with requests.get(f"http://127.0.0.1:{PORT}/stream", stream=True) as r:
assert r.status_code == 200
assert r.headers.get("content-type", "").startswith("text/event-stream")
data = b""
for chunk in r.iter_content(chunk_size=1024):
data += chunk
if b"data: 2" in data:
break
assert b"data: 0" in data
assert b"data: 1" in data
assert b"data: 2" in data
def test_http_still_works(server):
r = requests.get(f"http://127.0.0.1:{PORT}/hello")
assert r.status_code == 200
assert r.json() == {"message": "hello"}
@pytest.mark.skipif(not WEBSOCKETS_AVAILABLE, reason="websockets not installed")
def test_websocket_echo(server):
async def client():
uri = f"ws://127.0.0.1:{PORT}/ws"
async with websockets.connect(uri) as ws:
await ws.send("ping")
reply = await ws.recv()
assert reply == "echo:ping"
import asyncio
asyncio.run(client())