From 9db193049ad194c7cd0f6fa08e1c695cd8badc84 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Mon, 25 Nov 2024 10:00:29 +1100 Subject: [PATCH 1/4] Prevent aiohttp request headers from overriding session headers in matcher --- src/pook/interceptors/aiohttp.py | 11 ++++------ tests/unit/interceptors/aiohttp_test.py | 28 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/pook/interceptors/aiohttp.py b/src/pook/interceptors/aiohttp.py index 3737ee3..f1f1357 100644 --- a/src/pook/interceptors/aiohttp.py +++ b/src/pook/interceptors/aiohttp.py @@ -58,18 +58,15 @@ def set_headers(self, req, headers) -> None: # ``pook.request`` only allows a dict, so we need to map the iterable to the matchable interface if headers: if isinstance(headers, Mapping): - req.headers = headers + req.headers.update(**headers) else: - req_headers: dict[str, str] = {} # If it isn't a mapping, then its an Iterable[Tuple[Union[str, istr], str]] for req_header, req_header_value in headers: normalised_header = req_header.lower() - if normalised_header in req_headers: - req_headers[normalised_header] += f", {req_header_value}" + if normalised_header in req.headers: + req.headers[normalised_header] += f", {req_header_value}" else: - req_headers[normalised_header] = req_header_value - - req.headers = req_headers + req.headers[normalised_header] = req_header_value async def _on_request( self, diff --git a/tests/unit/interceptors/aiohttp_test.py b/tests/unit/interceptors/aiohttp_test.py index 78195b1..fa52a12 100644 --- a/tests/unit/interceptors/aiohttp_test.py +++ b/tests/unit/interceptors/aiohttp_test.py @@ -86,3 +86,31 @@ async def test_client_headers(httpbin): res = await session.get(httpbin + "/status/404") assert res.status == 200 assert await res.read() == b"hello from pook" + + +@pytest.mark.asyncio +async def test_client_headers_merged(httpbin): + """Headers set on the client should be matched even if request-specific headers are sent.""" + pook.get(httpbin + "/status/404").header("x-pook", "hello").reply(200).body( + "hello from pook" + ) + async with aiohttp.ClientSession(headers={"x-pook": "hello"}) as session: + res = await session.get( + httpbin + "/status/404", headers={"x-pook-secondary": "xyz"} + ) + assert res.status == 200 + assert await res.read() == b"hello from pook" + + +@pytest.mark.asyncio +async def test_client_headers_both_session_and_request(httpbin): + """Headers should be matchable from both the session and request in the same matcher""" + pook.get(httpbin + "/status/404").header("x-pook-session", "hello").header( + "x-pook-request", "hey" + ).reply(200).body("hello from pook") + async with aiohttp.ClientSession(headers={"x-pook-session": "hello"}) as session: + res = await session.get( + httpbin + "/status/404", headers={"x-pook-request": "hey"} + ) + assert res.status == 200 + assert await res.read() == b"hello from pook" From 7cf93ee8db6dffe9487429878d35e838726f1749 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Mon, 25 Nov 2024 10:00:40 +1100 Subject: [PATCH 2/4] Bump to 2.1.3 --- History.rst | 5 +++++ pyproject.toml | 1 + src/pook/__init__.py | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/History.rst b/History.rst index edd6634..1d1efec 100644 --- a/History.rst +++ b/History.rst @@ -1,6 +1,11 @@ History ======= +v2.1.3 / 2024-11-25 +------------------------- + + * Ensure aiohttp headers can be matched from both the session and request in the same matcher + v2.1.2 / 2024-11-21 ------------------------- diff --git a/pyproject.toml b/pyproject.toml index 9fc07ef..ecf7f54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ readme = "README.rst" license = "MIT" authors = [ { name = "Tomas Aparicio", email = "tomas@aparicio.me" }, + { name = "Sara Marcondes", email = "git@sarayourfriend.pictures" }, ] classifiers = [ "Development Status :: 5 - Production/Stable", diff --git a/src/pook/__init__.py b/src/pook/__init__.py index 2c00095..b80eefe 100644 --- a/src/pook/__init__.py +++ b/src/pook/__init__.py @@ -44,4 +44,4 @@ __license__ = "MIT" # Current version -__version__ = "2.1.2" +__version__ = "2.1.3" From 93ba9519500c09c8dd0fd3e1d9e65c57b181b629 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Mon, 16 Dec 2024 12:58:44 +1100 Subject: [PATCH 3/4] Add test for unmatched headers --- tests/unit/interceptors/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/unit/interceptors/base.py b/tests/unit/interceptors/base.py index 390efae..4193509 100644 --- a/tests/unit/interceptors/base.py +++ b/tests/unit/interceptors/base.py @@ -208,7 +208,7 @@ def test_header_sent(self, url_404): assert body == b"hello from pook" @pytest.mark.pook - def test_mocked_resposne_headers(self, url_404): + def test_mocked_response_headers(self, url_404): """Mocked response headers are appropriately returned.""" pook.get(url_404).reply(200).header("x-hello", "from pook") @@ -238,3 +238,11 @@ def test_mutli_value_response_headers(self, url_404): assert status == 200 assert headers["x-hello"] == "from pook, another time" + + @pytest.mark.pook(allow_pending_mocks=True) + def test_unmatched_headers_none_sent(self, url_404): + """Header matching will run, but not match, on requests that send no headers.""" + pook.get(url_404).header("x-hello", "from pook").reply(200) + + with pytest.raises(PookNoMatches): + self.make_request("GET", url_404) From ca159fed44351f3c97c231ee6b9173f3069445a5 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Mon, 16 Dec 2024 12:59:09 +1100 Subject: [PATCH 4/4] Update date in changelog --- History.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/History.rst b/History.rst index 1d1efec..275b62c 100644 --- a/History.rst +++ b/History.rst @@ -1,7 +1,7 @@ History ======= -v2.1.3 / 2024-11-25 +v2.1.3 / 2024-12-16 ------------------------- * Ensure aiohttp headers can be matched from both the session and request in the same matcher