Skip to content

Commit

Permalink
Merge pull request #258 from RafaelWO/feature/url-headers-from-session
Browse files Browse the repository at this point in the history
Feature: Use base_url and headers from ClientSession
  • Loading branch information
pnuckowski authored Dec 11, 2024
2 parents 03ecf38 + 1478a0b commit d9978f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 11 additions & 1 deletion aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,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'])

Expand Down Expand Up @@ -497,7 +500,14 @@ async def _request_mock(self, orig_self: ClientSession,
if orig_self.closed:
raise RuntimeError('Session is closed')

url_origin = url
# 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._prepare_headers(kwargs.get("headers"))

url = normalize_url(merge_params(url, kwargs.get('params')))
url_str = str(url)
for prefix in self._passthrough:
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 26 additions & 1 deletion tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d9978f5

Please sign in to comment.