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

Add persistent cookies option to Client #3065

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
16 changes: 14 additions & 2 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def __init__(
cookies: CookieTypes | None = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
merge_response_cookies: bool = True,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
event_hooks: None | (typing.Mapping[str, list[EventHook]]) = None,
base_url: URLTypes = "",
Expand All @@ -182,6 +183,7 @@ def __init__(
self._params = QueryParams(params)
self.headers = Headers(headers)
self._cookies = Cookies(cookies)
self._merge_response_cookies = merge_response_cookies
self._timeout = Timeout(timeout)
self.follow_redirects = follow_redirects
self.max_redirects = max_redirects
Expand Down Expand Up @@ -599,6 +601,8 @@ class Client(BaseClient):
URLs.
* **timeout** - *(optional)* The timeout configuration to use when sending
requests.
* **merge_response_cookies** - *(optional) A boolean indicating if cookies from the
response should be merged into the cookie jar. Defaults to `True`.
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
Expand Down Expand Up @@ -631,6 +635,7 @@ def __init__(
mounts: None | (typing.Mapping[str, BaseTransport | None]) = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
merge_response_cookies: bool = True,
limits: Limits = DEFAULT_LIMITS,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
event_hooks: None | (typing.Mapping[str, list[EventHook]]) = None,
Expand All @@ -647,6 +652,7 @@ def __init__(
cookies=cookies,
timeout=timeout,
follow_redirects=follow_redirects,
merge_response_cookies=merge_response_cookies,
max_redirects=max_redirects,
event_hooks=event_hooks,
base_url=base_url,
Expand Down Expand Up @@ -1013,7 +1019,8 @@ def _send_single_request(self, request: Request) -> Response:
response.stream = BoundSyncStream(
response.stream, response=response, timer=timer
)
self.cookies.extract_cookies(response)
if self._merge_response_cookies:
self.cookies.extract_cookies(response)
response.default_encoding = self._default_encoding

logger.info(
Expand Down Expand Up @@ -1337,6 +1344,8 @@ class AsyncClient(BaseClient):
URLs.
* **timeout** - *(optional)* The timeout configuration to use when sending
requests.
* **merge_response_cookies** - *(optional) A boolean indicating if cookies from the
response should be merged into the cookie jar. Defaults to `True`.
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
Expand Down Expand Up @@ -1369,6 +1378,7 @@ def __init__(
mounts: None | (typing.Mapping[str, AsyncBaseTransport | None]) = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
merge_response_cookies: bool = True,
limits: Limits = DEFAULT_LIMITS,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
event_hooks: None
Expand All @@ -1386,6 +1396,7 @@ def __init__(
cookies=cookies,
timeout=timeout,
follow_redirects=follow_redirects,
merge_response_cookies=merge_response_cookies,
max_redirects=max_redirects,
event_hooks=event_hooks,
base_url=base_url,
Expand Down Expand Up @@ -1753,7 +1764,8 @@ async def _send_single_request(self, request: Request) -> Response:
response.stream = BoundAsyncStream(
response.stream, response=response, timer=timer
)
self.cookies.extract_cookies(response)
if self._merge_response_cookies:
self.cookies.extract_cookies(response)
response.default_encoding = self._default_encoding

logger.info(
Expand Down
23 changes: 23 additions & 0 deletions tests/client/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,26 @@ 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_cookie_persistence_off() -> None:
"""
Ensure that Client instances do not persist cookies between requests when
persistence is off.
"""
client = httpx.Client(
transport=httpx.MockTransport(get_and_set_cookies), merge_response_cookies=False
)

response = client.get("http://example.org/echo_cookies")
assert response.status_code == 200
assert response.json() == {"cookies": None}

response = client.get("http://example.org/set_cookie")
assert response.status_code == 200
assert response.cookies["example-name"] == "example-value"
assert "example-name" not in client.cookies.keys()

response = client.get("http://example.org/echo_cookies")
assert response.status_code == 200
assert response.json() == {"cookies": None}