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

[PR #10093/7b5d54a backport][3.12] Use quote_cookie setting from ClientSession's cookiejar in tmp_cookie_jar #10176

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -176,6 +176,11 @@ class AbstractCookieJar(Sized, IterableBase):
def __init__(self, *, loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
self._loop = loop or asyncio.get_running_loop()

@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 @@ -656,7 +656,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 @@ -117,6 +117,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 @@ -474,6 +478,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
23 changes: 20 additions & 3 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
from yarl import URL

import aiohttp
from aiohttp import client, hdrs, web
from aiohttp import CookieJar, client, hdrs, web
from aiohttp.client import ClientSession
from aiohttp.client_proto import ResponseHandler
from aiohttp.client_reqrep import ClientRequest
from aiohttp.connector import BaseConnector, Connection, TCPConnector, UnixConnector
from aiohttp.helpers import DEBUG
from aiohttp.http import RawResponseMessage
from aiohttp.pytest_plugin import AiohttpServer
from aiohttp.test_utils import make_mocked_coro
from aiohttp.tracing import Trace

Expand Down Expand Up @@ -634,8 +635,24 @@ async def handler(request):
assert resp_cookies["response"].value == "resp_value"


async def test_session_default_version(loop) -> None:
session = aiohttp.ClientSession(loop=loop)
async def test_cookies_with_not_quoted_cookie_jar(
aiohttp_server: AiohttpServer,
) -> None:
async def handler(_: web.Request) -> web.Response:
return web.Response()

app = web.Application()
app.router.add_route("GET", "/", handler)
server = await aiohttp_server(app)
jar = CookieJar(quote_cookie=False)
cookies = {"name": "val=foobar"}
async with aiohttp.ClientSession(cookie_jar=jar) as sess:
resp = await sess.request("GET", server.make_url("/"), cookies=cookies)
assert resp.request_info.headers.get("Cookie", "") == "name=val=foobar"


async def test_session_default_version(loop: asyncio.AbstractEventLoop) -> None:
session = aiohttp.ClientSession()
assert session.version == aiohttp.HttpVersion11
await session.close()

Expand Down
1 change: 1 addition & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ async def make_jar():
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