swadhinbiswas test: align with {"detail": ...} error envelope · 7/15/2026
503c9b7
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Integration test for JustAPITestClient — the no-TCP test client.
"""
import json
from justapi import JustAPIApp, JustAPITestClient
def test_test_client_get():
app = JustAPIApp()
async def hello_handler(request):
name = request["path_params"]["name"]
return {"message": f"Hello {name}!"}
app.get("/hello/{name}", hello_handler)
client = JustAPITestClient(app)
resp = client.get("/hello/world")
assert resp["status"] == 200
data = json.loads(bytes(resp["body"]))
assert data == {"message": "Hello world!"}
resp = client.get("/hello/JustAPI")
assert resp["status"] == 200
data = json.loads(bytes(resp["body"]))
assert data == {"message": "Hello JustAPI!"}
def test_test_client_post():
app = JustAPIApp()
async def echo_handler(request):
return {
"status": 200,
"headers": [(b"content-type", b"application/json")],
"body": request["body"],
}
app.post("/echo", echo_handler)
client = JustAPITestClient(app)
payload = json.dumps({"hello": "world"}).encode()
resp = client.post("/echo", payload)
assert resp["status"] == 200
assert bytes(resp["body"]) == payload
def test_test_client_put():
app = JustAPIApp()
async def echo_handler(request):
return {
"status": 200,
"headers": [(b"content-type", b"application/json")],
"body": request["body"],
}
app.put("/echo", echo_handler)
client = JustAPITestClient(app)
payload = json.dumps({"updated": True}).encode()
resp = client.put("/echo", payload)
assert resp["status"] == 200
assert bytes(resp["body"]) == payload
def test_test_client_delete():
app = JustAPIApp()
async def delete_handler(request):
return {"status": 204}
app.delete("/resource/{id}", delete_handler)
client = JustAPITestClient(app)
resp = client.delete("/resource/42")
assert resp["status"] == 204
def test_test_client_404():
app = JustAPIApp()
async def hello_handler(request):
return {"message": "hello"}
app.get("/hello", hello_handler)
client = JustAPITestClient(app)
resp = client.get("/nonexistent")
assert resp["status"] == 404
def test_test_client_wrong_method():
app = JustAPIApp()
async def echo_handler(request):
return {"body": request["body"]}
app.post("/echo", echo_handler)
client = JustAPITestClient(app)
resp = client.get("/echo")
assert resp["status"] == 405
def test_test_client_schema_validation():
"""Rust-side JSON Schema validation works through the test client."""
from justapi import Schema
class UserSchema(Schema):
name: str
email: str
age: int | None = None
app = JustAPIApp()
async def create_user(request):
body = json.loads(request["body"])
return {
"status": 201,
"body": json.dumps({"id": 1, **body}).encode(),
"headers": [(b"content-type", b"application/json")],
}
app.post("/users", create_user, schema=UserSchema)
client = JustAPITestClient(app)
# Valid payload
payload = json.dumps({"name": "Alice", "email": "[email protected]", "age": 30}).encode()
resp = client.post("/users", payload)
assert resp["status"] == 201
data = json.loads(bytes(resp["body"]))
assert data["id"] == 1
assert data["name"] == "Alice"
# Missing required field
payload = json.dumps({"name": "Alice"}).encode()
resp = client.post("/users", payload)
assert resp["status"] == 422
error_data = json.loads(bytes(resp["body"]))
assert isinstance(error_data.get("detail"), str) and len(error_data["detail"]) > 0