Skip to content

Consult nested mocks innermost-first on the httpx backend - #3548

Merged
adamtheturtle merged 2 commits into
mainfrom
adamtheturtle/pick-next-issue-v4
Sep 5, 2026
Merged

Consult nested mocks innermost-first on the httpx backend#3548
adamtheturtle merged 2 commits into
mainfrom
adamtheturtle/pick-next-issue-v4

Conversation

@adamtheturtle

Copy link
Copy Markdown
Member

Fixes #3544.

Problem

respx keeps every started router in a single process-wide list and consults them in registration order. Each MockVWS respx router ends with a catch-all which raises httpx.ConnectError (not AllMockedAssertionError), so it propagates out of Mocker.handler's loop and no later router is tried.

The result: with one MockVWS nested inside another, the outer mock answered and the inner mock was unreachable — a request to the URL the inner mock was created for was refused. The requests and httpx2 backends do the opposite, because their patches form a LIFO stack.

requests inner-url    401
requests outer-url    raised ConnectionError
httpx    inner-url    raised ConnectError     <-- the odd one out
httpx    outer-url    401
httpx2   inner-url    401
httpx2   outer-url    raised ConnectError

Fix

start_respx_router now moves each newly started router to the front of respx's router list, so nesting resolves innermost-first on every backend. Unregistering on stop already restores the outer router to the front, so the outer mock answers again once the inner one has stopped. Mocker.async_handler walks the same list, so async nesting is fixed too.

Test

TestVWS::test_nested_mocks in tests/mock_vws/test_respx_mock_usage.py, mirroring the existing httpx2 test of the same name.

🤖 Generated with Claude Code

respx keeps every started router in a single process-wide list and tries
them in registration order. Each MockVWS router ends with a catch-all,
so an outer mock always answered and a nested inner mock was never
consulted -- a request to the inner mock's own URL was refused.

Move each newly started router to the front of respx's list so nesting
resolves innermost-first, as it already does on the requests and httpx2
backends, whose patches form a LIFO stack.

Fixes #3544

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested MockVWS instances: the httpx backend consults the outer mock first, so the inner mock's URL is refused

1 participant