From 7265d06e18cb257a266071b627d8a8d2aa84e7d7 Mon Sep 17 00:00:00 2001 From: Rostislav Aleev Date: Fri, 2 Jun 2023 16:05:15 +0300 Subject: [PATCH 1/5] use ClientSession base_url and headers --- aioresponses/core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/aioresponses/core.py b/aioresponses/core.py index a22fad6..0de6bcd 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -497,6 +497,13 @@ async def _request_mock(self, orig_self: ClientSession, raise RuntimeError('Session is closed') url_origin = url + # construct URL with ClientSession._base_url + if getattr(orig_self, "_base_url", None): + url_origin = f"{orig_self._base_url}{url}" + url = f"{orig_self._base_url}{url}" + # retrieve ClientSession headers + if orig_self.headers: + kwargs["headers"] = orig_self.headers url = normalize_url(merge_params(url, kwargs.get('params'))) url_str = str(url) for prefix in self._passthrough: From eadbd0fb05755a17b2a408a1bddd25997f98ae1d Mon Sep 17 00:00:00 2001 From: RafaelWO Date: Wed, 1 May 2024 17:37:01 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20usage=20of?= =?UTF-8?q?=20session=20base=5Furl=20and=20headers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aioresponses/core.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/aioresponses/core.py b/aioresponses/core.py index 0de6bcd..9012ad6 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -496,14 +496,14 @@ async def _request_mock(self, orig_self: ClientSession, if orig_self.closed: raise RuntimeError('Session is closed') - url_origin = url - # construct URL with ClientSession._base_url - if getattr(orig_self, "_base_url", None): - url_origin = f"{orig_self._base_url}{url}" - url = f"{orig_self._base_url}{url}" - # retrieve ClientSession headers + # Join url with ClientSession._base_url + url = orig_self._build_url(url) + url_origin = str(url) + + # Combine ClientSession headers with passed headers if orig_self.headers: - kwargs["headers"] = orig_self.headers + kwargs["headers"] = orig_self._prepare_headers(kwargs.get("headers")) + url = normalize_url(merge_params(url, kwargs.get('params'))) url_str = str(url) for prefix in self._passthrough: From 5f5512394e664f39455086d47353356c08b3aa86 Mon Sep 17 00:00:00 2001 From: RafaelWO Date: Wed, 1 May 2024 17:37:24 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20base=5Furl?= =?UTF-8?q?=20and=20headers=20from=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_aioresponses.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_aioresponses.py b/tests/test_aioresponses.py index 22b1bc4..38f6d99 100644 --- a/tests/test_aioresponses.py +++ b/tests/test_aioresponses.py @@ -10,7 +10,7 @@ from aiohttp import http from aiohttp.client import ClientSession from aiohttp.client_reqrep import ClientResponse -from ddt import ddt, data +from ddt import ddt, data, unpack from packaging.version import Version try: @@ -80,6 +80,31 @@ async def test_returned_instance_and_status_code(self, m): self.assertIsInstance(response, ClientResponse) self.assertEqual(response.status, 204) + @unpack + @data( + ("http://example.com", "/api?foo=bar#fragment"), + ("http://example.com/", "/api?foo=bar#fragment") + ) + @aioresponses() + async def test_base_url(self, m, base_url, relative_url): + m.get(self.url, status=200) + self.session = ClientSession(base_url=base_url) + response = await self.session.get(relative_url) + self.assertEqual(response.status, 200) + + @aioresponses() + async def test_session_headers(self, m): + m.get(self.url) + self.session = ClientSession(headers={"Authorization": "Bearer foobar"}) + response = await self.session.get(self.url) + + self.assertEqual(response.status, 200) + + # Check that the headers from the ClientSession are within the request + key = ('GET', URL(self.url)) + request = m.requests[key][0] + self.assertEqual(request.kwargs["headers"]["Authorization"], 'Bearer foobar') + @aioresponses() async def test_returned_response_headers(self, m): m.get(self.url, From 95c9f5efbd6f4604713c0030eeef502107069446 Mon Sep 17 00:00:00 2001 From: RafaelWO Date: Wed, 1 May 2024 17:38:00 +0200 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20Add=20=5F=5Frepr=5F=5F=20to=20R?= =?UTF-8?q?equestMatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aioresponses/core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aioresponses/core.py b/aioresponses/core.py index 9012ad6..425dff0 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -212,6 +212,9 @@ async def build_response( reason=result.reason) return resp + def __repr__(self) -> str: + return f"RequestMatch('{self.url_or_pattern}')" + RequestCall = namedtuple('RequestCall', ['args', 'kwargs']) From 1478a0bfd8cefe3c8a81354b31685a929277376e Mon Sep 17 00:00:00 2001 From: RafaelWO Date: Wed, 1 May 2024 17:39:40 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Upgrade=20yarl=20to=20?= =?UTF-8?q?be=20compatible=20with=20Python=203.12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements-dev.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index f19c759..bb31c52 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,4 +10,5 @@ pytest-html==2.1.1 ddt==1.4.1 typing asynctest==0.13.0 -yarl==1.8.2 +yarl==1.9.4 +setuptools==69.5.1