Skip to content
Open
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
8 changes: 6 additions & 2 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,15 @@ def _merge_cookies(self, cookies: CookieTypes | None = None) -> CookieTypes | No
Merge a cookies argument together with any cookies on the client,
to create the cookies used for the outgoing request.
"""
if cookies or self.cookies:
if not cookies:
# Nothing to merge in: use the client cookies as they are,
# rather than copying the whole jar on every request.
return self.cookies
if not self.cookies:
return cookies
merged_cookies = Cookies(self.cookies)
merged_cookies.update(cookies)
return merged_cookies
return cookies

def _merge_headers(self, headers: HeaderTypes | None = None) -> HeaderTypes | None:
"""
Expand Down
6 changes: 5 additions & 1 deletion httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,11 @@ def __init__(
self.extensions = {} if extensions is None else dict(extensions)

if cookies:
Cookies(cookies).set_cookie_header(self)
# Use a `Cookies` instance as it is, rather than copying
# the whole jar just to read the header values from it.
if not isinstance(cookies, Cookies):
cookies = Cookies(cookies)
cookies.set_cookie_header(self)

if stream is None:
content_type: str | None = self.headers.get("content-type")
Expand Down
37 changes: 37 additions & 0 deletions tests/client/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,40 @@ def test_cookie_persistence() -> None:
response = client.get("http://example.org/echo_cookies")
assert response.status_code == 200
assert response.json() == {"cookies": "example-name=example-value"}


def test_merge_cookies_without_request_cookies_reuses_client_cookies() -> None:
"""
Regression test for https://github.com/encode/httpx/issues/2875

Merging cookies used to copy the client's entire cookie jar on
every request, even when there were no per-request cookies to
merge in, making request setup scale with the number of cookies.
"""
client = httpx.Client(cookies={"a": "1", "b": "2"})

assert client._merge_cookies() is client.cookies
assert client._merge_cookies(None) is client.cookies

request = client.build_request("GET", "http://example.org/")
assert request.headers["cookie"] == "a=1; b=2"


def test_merge_cookies_without_client_cookies_reuses_request_cookies() -> None:
client = httpx.Client()
cookies = httpx.Cookies({"a": "1"})

assert client._merge_cookies(cookies) is cookies


def test_merge_cookies_still_merges_client_and_request_cookies() -> None:
client = httpx.Client(cookies={"a": "1", "b": "2"})

request = client.build_request(
"GET", "http://example.org/", cookies={"b": "override", "c": "3"}
)

assert request.headers["cookie"] == "a=1; b=override; c=3"
# The client's own cookies are unchanged by the merge.
assert client.cookies["b"] == "2"
assert "c" not in client.cookies
Loading