Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use quote_cookie setting from ClientSession's cookiejar in tmp_cookie_jar #10093

Merged
merged 10 commits into from
Dec 17, 2024
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 @@ -171,6 +171,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
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
)
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
bdraco marked this conversation as resolved.
Show resolved Hide resolved

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
Loading