Skip to content

Commit e2ebade

Browse files
lesnik512claude
andcommitted
test(story-2.1): chain ordering, request/response transformation
Adds three tests verifying the onion-execution order (outer→inner→ transport→inner→outer), request mutation via with_header propagates to the inner middleware, and outer middleware can return a modified Response after awaiting next. No production code changes; the existing compose() implementation handles all three cases. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1b4d4af commit e2ebade

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

‎tests/test_middleware.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,69 @@ async def __call__(self, request: Request, next: Next) -> Response: # noqa: A00
8989

9090
assert seen == [request]
9191
assert response.content == b"transport"
92+
93+
94+
async def test_chain_runs_outer_to_inner() -> None:
95+
"""Three middlewares form an onion: outer→inner→transport→inner→outer."""
96+
log: list[str] = []
97+
98+
def labeled(name: str) -> Middleware:
99+
class Labeled:
100+
async def __call__(self, request: Request, next: Next) -> Response: # noqa: A002
101+
log.append(f"{name}:before")
102+
response = await next(request)
103+
log.append(f"{name}:after")
104+
return response
105+
106+
return Labeled()
107+
108+
dispatch = compose([labeled("A"), labeled("B"), labeled("C")], _OkTransport())
109+
await dispatch(_make_request())
110+
111+
assert log == [
112+
"A:before",
113+
"B:before",
114+
"C:before",
115+
"C:after",
116+
"B:after",
117+
"A:after",
118+
]
119+
120+
121+
async def test_middleware_can_transform_request_before_forwarding() -> None:
122+
"""An outer middleware mutates the request via with_header; the inner sees the mutation."""
123+
seen: list[Request] = []
124+
125+
class Stamp:
126+
async def __call__(self, request: Request, next: Next) -> Response: # noqa: A002
127+
stamped = request.with_header("x-trace", "abc123")
128+
return await next(stamped)
129+
130+
class Inspect:
131+
async def __call__(self, request: Request, next: Next) -> Response: # noqa: A002
132+
seen.append(request)
133+
return await next(request)
134+
135+
await compose([Stamp(), Inspect()], _OkTransport())(_make_request())
136+
137+
assert seen[0].headers["x-trace"] == "abc123"
138+
139+
140+
async def test_middleware_can_transform_response_before_returning() -> None:
141+
"""An outer middleware awaits next, then returns a modified Response; caller sees it."""
142+
143+
class AddHeader:
144+
async def __call__(self, request: Request, next: Next) -> Response: # noqa: A002
145+
response = await next(request)
146+
return Response(
147+
status=response.status,
148+
headers={**response.headers, "x-trace": "abc123"},
149+
content=response.content,
150+
url=response.url,
151+
elapsed=response.elapsed,
152+
)
153+
154+
response = await compose([AddHeader()], _OkTransport())(_make_request())
155+
156+
assert response.headers["x-trace"] == "abc123"
157+
assert response.headers["x-from"] == "transport" # original still present

0 commit comments

Comments
 (0)