From 450e9a3dcfc1888eb4b5fb59783400cc9c4add90 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:33:26 +0100 Subject: [PATCH 1/9] Bump black to latest version --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2e19564..ee9075b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ repos: - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 22.10.0 hooks: - id: black \ No newline at end of file From 1a9fe62f4383930183a3c7f8258ee05fe39963b4 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:33:49 +0100 Subject: [PATCH 2/9] Use the appropriate number in contributing doc --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8d27041..6dabc47 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,7 +56,7 @@ Before creating an issue please make sure that it was not already reported. 1) Go to the *Pull requests* tab and click on the *New pull request* button. 2) *base* should always be set to `develop` and it should be compared to your branch. 3) Title should be a small sentence describing the request. -3) The comment should contain as much information as possible +4) The comment should contain as much information as possible * Actual behavior (before the new code) * Expected behavior (with the new code) * Steps to reproduce (with and without the new code to see the difference) From ec2c99d3420a7eb1dafbc54b4e2f866657c8b526 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:34:43 +0100 Subject: [PATCH 3/9] Bump test dependencies to latest version --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 24d780e..c1d523e 100644 --- a/setup.py +++ b/setup.py @@ -44,9 +44,9 @@ extras_require={ "testing": [ # Used to run async test functions - "pytest-asyncio==0.17.*", + "pytest-asyncio==0.20.*", # Used to check coverage - "pytest-cov==3.*", + "pytest-cov==4.*", ] }, python_requires=">=3.7", From a4ae7e62a4779b1a6f8f5f9f639667ff6c1f5115 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:38:25 +0100 Subject: [PATCH 4/9] Reproduce issue with non ASCII characters matching on URL query --- README.md | 2 +- tests/test_httpx_async.py | 18 ++++++++++++++++++ tests/test_httpx_sync.py | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6921a5d..cc0ed83 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Build status Coverage Code style: black -Number of tests +Number of tests Number of downloads

diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py index ef5d0cb..1e2454a 100644 --- a/tests/test_httpx_async.py +++ b/tests/test_httpx_async.py @@ -1669,3 +1669,21 @@ async def custom_response(request: httpx.Request) -> httpx.Response: async with httpx.AsyncClient() as client: response = await client.get("https://test_url") assert response.elapsed is not None + + +@pytest.mark.asyncio +async def test_non_ascii_url_response(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(url="https://test_url?query_type=数据") + + async with httpx.AsyncClient() as client: + response = await client.get("https://test_url?query_type=数据") + assert response.content == b"" + + +@pytest.mark.asyncio +async def test_url_encoded_matching_response(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(url="https://test_url?a=%E6%95%B0%E6%8D%AE") + + async with httpx.AsyncClient() as client: + response = await client.get("https://test_url?a=数据") + assert response.content == b"" diff --git a/tests/test_httpx_sync.py b/tests/test_httpx_sync.py index 1d59919..a1688db 100644 --- a/tests/test_httpx_sync.py +++ b/tests/test_httpx_sync.py @@ -1383,3 +1383,19 @@ def test_elapsed_when_add_callback(httpx_mock: HTTPXMock) -> None: with httpx.Client() as client: response = client.get("https://test_url") assert response.elapsed is not None + + +def test_non_ascii_url_response(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(url="https://test_url?query_type=数据") + + with httpx.Client() as client: + response = client.get("https://test_url?query_type=数据") + assert response.content == b"" + + +def test_url_encoded_matching_response(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(url="https://test_url?a=%E6%95%B0%E6%8D%AE") + + with httpx.Client() as client: + response = client.get("https://test_url?a=数据") + assert response.content == b"" From 4b96ab648aafc87312b5323f6fcb3eb0e637a4c5 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:40:54 +0100 Subject: [PATCH 5/9] Handle any encoding in query string while matching --- CHANGELOG.md | 2 ++ pytest_httpx/_httpx_mock.py | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd98be..336281b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- URL containing non ASCII characters in query can now be matched. ## [0.21.1] - 2022-10-20 ### Fixed diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py index 7f3c5c7..641c3d8 100644 --- a/pytest_httpx/_httpx_mock.py +++ b/pytest_httpx/_httpx_mock.py @@ -1,7 +1,6 @@ import inspect import re from typing import List, Union, Optional, Callable, Tuple, Pattern, Any, Dict, Awaitable -from urllib.parse import parse_qs import httpx @@ -41,14 +40,14 @@ def _url_match(self, request: httpx.Request) -> bool: return self.url.match(str(request.url)) is not None # Compare query parameters apart as order of parameters should not matter - request_qs = parse_qs(request.url.query) - qs = parse_qs(self.url.query) + request_params = dict(request.url.params) + params = dict(self.url.params) # Remove the query parameters from the original URL to compare everything besides query parameters request_url = request.url.copy_with(query=None) url = self.url.copy_with(query=None) - return (request_qs == qs) and (url == request_url) + return (request_params == params) and (url == request_url) def _method_match(self, request: httpx.Request) -> bool: if not self.method: From 5f728c954d0fd305fad179fa7ef461bd42025a9f Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:42:56 +0100 Subject: [PATCH 6/9] Remove python < 3.7 compatibility --- pytest_httpx/_httpx_mock.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py index 641c3d8..c0abb3a 100644 --- a/pytest_httpx/_httpx_mock.py +++ b/pytest_httpx/_httpx_mock.py @@ -6,9 +6,6 @@ from pytest_httpx import _httpx_internals -# re.Pattern was introduced in Python 3.7 -pattern_type = re._pattern_type if hasattr(re, "_pattern_type") else re.Pattern - class _RequestMatcher: def __init__( @@ -36,7 +33,7 @@ def _url_match(self, request: httpx.Request) -> bool: if not self.url: return True - if isinstance(self.url, pattern_type): + if isinstance(self.url, re.Pattern): return self.url.match(str(request.url)) is not None # Compare query parameters apart as order of parameters should not matter From 3ec2680bd2ad3c738dd4f4148291d12ead147e59 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:45:01 +0100 Subject: [PATCH 7/9] Release version 0.21.2 today --- CHANGELOG.md | 5 ++++- pytest_httpx/version.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 336281b..5e8a85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## [0.21.2] - 2022-11-03 ### Fixed - URL containing non ASCII characters in query can now be matched. @@ -229,7 +231,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - First release, should be considered as unstable for now as design might change. -[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.21.1...HEAD +[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.21.2...HEAD +[0.21.2]: https://github.com/Colin-b/pytest_httpx/compare/v0.21.1...v0.21.2 [0.21.1]: https://github.com/Colin-b/pytest_httpx/compare/v0.21.0...v0.21.1 [0.21.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.20.0...v0.21.0 [0.20.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.19.0...v0.20.0 diff --git a/pytest_httpx/version.py b/pytest_httpx/version.py index 5e3dfb3..10972d9 100644 --- a/pytest_httpx/version.py +++ b/pytest_httpx/version.py @@ -3,4 +3,4 @@ # Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0) # Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0) # Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9) -__version__ = "0.21.1" +__version__ = "0.21.2" From 9ae0b370b1763473b00b4f0c00a1103b85c46104 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 21:58:32 +0100 Subject: [PATCH 8/9] Clear requests upon reset --- CHANGELOG.md | 1 + pytest_httpx/_httpx_mock.py | 1 + tests/test_httpx_async.py | 12 ++++++++++++ tests/test_httpx_sync.py | 11 +++++++++++ 4 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e8a85f..453c2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.21.2] - 2022-11-03 ### Fixed - URL containing non ASCII characters in query can now be matched. +- Requests are now cleared when calling `httpx_mock.reset`. ## [0.21.1] - 2022-10-20 ### Fixed diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py index c0abb3a..115f8d3 100644 --- a/pytest_httpx/_httpx_mock.py +++ b/pytest_httpx/_httpx_mock.py @@ -300,6 +300,7 @@ def get_request(self, **matchers) -> Optional[httpx.Request]: return requests[0] if requests else None def reset(self, assert_all_responses_were_requested: bool) -> None: + self._requests.clear() not_called = self._reset_callbacks() if assert_all_responses_were_requested: diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py index 1e2454a..5f9f8a4 100644 --- a/tests/test_httpx_async.py +++ b/tests/test_httpx_async.py @@ -1687,3 +1687,15 @@ async def test_url_encoded_matching_response(httpx_mock: HTTPXMock) -> None: async with httpx.AsyncClient() as client: response = await client.get("https://test_url?a=数据") assert response.content == b"" + + +@pytest.mark.asyncio +async def test_reset_is_removing_requests(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response() + async with httpx.AsyncClient() as client: + await client.get("https://test_url") + + assert len(httpx_mock.get_requests()) == 1 + + httpx_mock.reset(assert_all_responses_were_requested=False) + assert len(httpx_mock.get_requests()) == 0 diff --git a/tests/test_httpx_sync.py b/tests/test_httpx_sync.py index a1688db..87661ce 100644 --- a/tests/test_httpx_sync.py +++ b/tests/test_httpx_sync.py @@ -1399,3 +1399,14 @@ def test_url_encoded_matching_response(httpx_mock: HTTPXMock) -> None: with httpx.Client() as client: response = client.get("https://test_url?a=数据") assert response.content == b"" + + +def test_reset_is_removing_requests(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response() + with httpx.Client() as client: + client.get("https://test_url") + + assert len(httpx_mock.get_requests()) == 1 + + httpx_mock.reset(assert_all_responses_were_requested=False) + assert len(httpx_mock.get_requests()) == 0 From e2e1768f4c487a662582e6e1982475a11bd84e2b Mon Sep 17 00:00:00 2001 From: Colin-b Date: Thu, 3 Nov 2022 22:00:04 +0100 Subject: [PATCH 9/9] Keep the number of test cases up to date --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc0ed83..20d81c3 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Build status Coverage Code style: black -Number of tests +Number of tests Number of downloads