Skip to content

Validate empty match_params consistently - #252

Open
zombieli wants to merge 2 commits into
Colin-b:developfrom
zombieli:fix/empty-query-validation
Open

zombieli wants to merge 2 commits into
Colin-b:developfrom
zombieli:fix/empty-query-validation

Conversation

@zombieli

Copy link
Copy Markdown
Contributor

An empty match_params dictionary bypasses the URL validation because the checks use truthiness. With no URL or with a regex URL, those parameters are then silently ignored.

Check whether match_params was supplied (is not None) so empty and non-empty dictionaries follow the same validation. Extend both invalid-configuration tests and verify that a concrete URL with {} still matches only requests without query parameters. Add an Unreleased changelog entry.

Validation on Windows / Python 3.13.1:

  • The two empty-dictionary rejection cases fail on the upstream implementation; the existing non-empty cases and valid empty-query control pass.
  • pytest -q --cov-report=term-missing in a dedicated editable-install environment: 349 passed, 100% coverage.
  • Black, Ruff, flake8 and mypy with the repository hook's isolated dependencies/arguments pass.

Prepared with OpenAI Codex assistance.

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (39ccbe0) to head (20827c6).
⚠️ Report is 2 commits behind head on develop.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #252   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          366       368    +2     
  Branches        69        70    +1     
=========================================
+ Hits           366       368    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Colin-b Colin-b left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this, why would you even provide it as empty? The default behavior is to check the full URL with params included. So it covers it. The regex case look valid but again, this is user provided regex, so they should include the query parameters as well if this is what they want to assert

@zombieli

Copy link
Copy Markdown
Contributor Author

You're right that normal URL matching already checks the query parameters. My intent here was only to make configuration validation consistent: with no URL or a regex URL, match_params={} is currently ignored, while a non-empty dict raises.

I found this through code inspection rather than a real-world failing use case. Since this would make previously accepted configurations raise, consistency alone may not justify the change. Would you prefer to keep ignoring an empty dict in these cases?

@Colin-b

Colin-b commented Oct 20, 2026

Copy link
Copy Markdown
Owner

You are right, this might be misleading to some people, even if this is an edge case. Better be safe than sorry, I'm fine with the change

@Colin-b Colin-b left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments and we also lack the same changes on the async test file

Comment thread CHANGELOG.md Outdated

### Fixed
- `match_params` now preserves `unittest.mock.ANY` inside tuple values, as it already does for lists.
- Empty `match_params` now requires a non-regex URL, just like non-empty `match_params`.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Empty `match_params` now requires a non-regex URL, just like non-empty `match_params`.
- Providing an empty `match_params` will now assert that no query parameters was provided. Note that you do not need to provide it if you already use the `url` parameter and this value does not contains query parameters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the changelog to describe the empty-query assertion, including when url is omitted, and to note that a concrete URL without query parameters already provides this behavior.

Comment thread tests/test_httpx_sync.py

@pytest.mark.httpx_mock(assert_all_requests_were_expected=False)
def test_empty_match_params_requires_no_query(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(url="https://test_url", match_params={}, is_reusable=True)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
httpx_mock.add_response(url="https://test_url", match_params={}, is_reusable=True)
httpx_mock.add_response(match_params={}, is_reusable=True)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change we actually test a more realistic use case of empty match_params

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test to omit url and added matching async coverage. I also adjusted the matcher so match_params={} is accepted without a URL and only matches requests without query parameters. The tests cover different URLs, query mismatches, the default None behavior, and invalid configurations. Non-empty match_params still requires a URL; combining match_params with a regex URL remains unsupported.

Local validation on Windows / Python 3.13.1: 359 tests passed with 100% coverage. Formatting, lint/type checks, the remaining configured hooks, and sdist/wheel builds also passed.

@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.

2 participants