Skip to content

Commit

Permalink
Use quote_cookie setting from ClientSession's cookiejar in `tmp_coo…
Browse files Browse the repository at this point in the history
…kie_jar` (#10093)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sam Bull <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 7c12b1a commit 7b5d54a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES/10093.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :py:meth:`~aiohttp.ClientSession.request` to reuse the ``quote_cookie`` setting from ``ClientSession._cookie_jar`` when processing cookies parameter.
-- by :user:`Cycloctane`.
5 changes: 5 additions & 0 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ async def close(self) -> None:
class AbstractCookieJar(Sized, IterableBase):
"""Abstract Cookie Jar."""

@property
@abstractmethod
def quote_cookie(self) -> bool:
"""Return True if cookies should be quoted."""

@abstractmethod
def clear(self, predicate: Optional[ClearCookiePredicate] = None) -> None:
"""Clear all cookies if no predicate is passed."""
Expand Down
4 changes: 3 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ async def _request(
all_cookies = self._cookie_jar.filter_cookies(url)

if cookies is not None:
tmp_cookie_jar = CookieJar()
tmp_cookie_jar = CookieJar(
quote_cookie=self._cookie_jar.quote_cookie
)
tmp_cookie_jar.update_cookies(cookies)
req_cookies = tmp_cookie_jar.filter_cookies(url)
if req_cookies:
Expand Down
8 changes: 8 additions & 0 deletions aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def __init__(
self._expire_heap: List[Tuple[float, Tuple[str, str, str]]] = []
self._expirations: Dict[Tuple[str, str, str], float] = {}

@property
def quote_cookie(self) -> bool:
return self._quote_cookie

def save(self, file_path: PathLike) -> None:
file_path = pathlib.Path(file_path)
with file_path.open(mode="wb") as f:
Expand Down Expand Up @@ -471,6 +475,10 @@ def __iter__(self) -> "Iterator[Morsel[str]]":
def __len__(self) -> int:
return 0

@property
def quote_cookie(self) -> bool:
return True

def clear(self, predicate: Optional[ClearCookiePredicate] = None) -> None:
pass

Expand Down
1 change: 0 additions & 1 deletion tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ async def handler(request: web.Request) -> web.Response:
assert resp_cookies["response"].value == "resp_value"


@pytest.mark.xfail(reason="Reproducer for #9336")
async def test_cookies_with_not_quoted_cookie_jar(
aiohttp_server: AiohttpServer,
) -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ async def make_jar() -> CookieJar:
async def test_dummy_cookie_jar() -> None:
cookie = SimpleCookie("foo=bar; Domain=example.com;")
dummy_jar = DummyCookieJar()
assert dummy_jar.quote_cookie is True
assert len(dummy_jar) == 0
dummy_jar.update_cookies(cookie)
assert len(dummy_jar) == 0
Expand Down

0 comments on commit 7b5d54a

Please sign in to comment.