Skip to content

Add match_request for custom request matching - #268

Open
TOMASEZARATE wants to merge 1 commit into
Colin-b:developfrom
TOMASEZARATE:custom-matcher
Open

TOMASEZARATE wants to merge 1 commit into
Colin-b:developfrom
TOMASEZARATE:custom-matcher

Conversation

@TOMASEZARATE

Copy link
Copy Markdown

Closes #177

This adds a match_request parameter to add_response, add_callback, add_exception, get_request and get_requests. It takes a callable that receives the httpx.Request and returns a bool, and it is combined with the other matchers, so all of them have to match.

Instead of adding a negation for each matcher, this covers the cases from the issue with a plain function:

httpx_mock.add_response(match_request=lambda request: "X-Foo" not in request.headers)

When nothing matches, or a response is left unused, the error message lists it as custom matcher <name>, using the function's __name__ (or the class name for callable objects).

README and CHANGELOG are updated, and the new tests cover sync and async clients. Coverage stays at 100%.

I used an AI assistant (Cursor Agent with Grok 4.7) while writing this change. I reviewed the code myself, and the commit has an Assisted-by trailer.

Assisted-by: Cursor Agent with Grok 4.7
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

Negative match

1 participant