Skip to content

Commit

Permalink
Merge pull request #265 from pnuckowski/fix-clientsession-base_url
Browse files Browse the repository at this point in the history
fix: added condition on py3.8 while supporting ClientSession.base_url
  • Loading branch information
pnuckowski authored Dec 12, 2024
2 parents 36c0329 + e14c9d5 commit 2e8e5ed
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
22 changes: 12 additions & 10 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
)
from aiohttp.helpers import TimerNoop
from multidict import CIMultiDict, CIMultiDictProxy
from packaging.version import Version

from .compat import (
URL,
Pattern,
stream_reader_factory,
merge_params,
normalize_url,
RequestInfo,
RequestInfo, AIOHTTP_VERSION,
)


_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])


Expand Down Expand Up @@ -223,7 +223,7 @@ def __repr__(self) -> str:
class aioresponses(object):
"""Mock aiohttp requests made by ClientSession."""
_matches = None # type: Dict[str, RequestMatch]
_responses = None # type: List[ClientResponse]
_responses: List[ClientResponse] = None
requests = None # type: Dict

def __init__(self, **kwargs: Any):
Expand Down Expand Up @@ -500,13 +500,15 @@ async def _request_mock(self, orig_self: ClientSession,
if orig_self.closed:
raise RuntimeError('Session is closed')

# 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"))
if AIOHTTP_VERSION >= Version('3.8.0'):
# 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"))
else:
url_origin = url

url = normalize_url(merge_params(url, kwargs.get('params')))
url_str = str(url)
Expand Down
5 changes: 2 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pip
wheel
flake8==3.8.3
flake8==5.0.4
tox==3.19.0
coverage==5.2.1
Sphinx==1.5.6
Expand All @@ -10,5 +10,4 @@ pytest-html==2.1.1
ddt==1.4.1
typing
asynctest==0.13.0
yarl==1.9.4
setuptools==69.5.1
yarl==1.9.4
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ universal = 1

[flake8]
exclude = docs
max-line-length = 120

[tool:pytest]
testpaths = tests
2 changes: 2 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ async def test_returned_instance_and_status_code(self, m):
("http://example.com/", "/api?foo=bar#fragment")
)
@aioresponses()
@skipIf(condition=AIOHTTP_VERSION < Version('3.8.0'), reason='aiohttp must be >= 3.8.0')
async def test_base_url(self, base_url, relative_url, m):
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()
@skipIf(condition=AIOHTTP_VERSION < Version('3.8.0'), reason='aiohttp must be >= 3.8.0')
async def test_session_headers(self, m):
m.get(self.url)
self.session = ClientSession(headers={"Authorization": "Bearer foobar"})
Expand Down

0 comments on commit 2e8e5ed

Please sign in to comment.