From 7c70e48e243efdfe487609f1e1b9ab40c268c12c Mon Sep 17 00:00:00 2001 From: Emma Gangi Date: Mon, 1 Dec 2025 14:57:08 -0500 Subject: [PATCH] Fix cookie value bug for empty strings --- src/requests/cookies.py | 12 ++++---- tests/test_cookies.py | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 tests/test_cookies.py diff --git a/src/requests/cookies.py b/src/requests/cookies.py index f69d0cda9e..22085b83d5 100644 --- a/src/requests/cookies.py +++ b/src/requests/cookies.py @@ -395,22 +395,22 @@ def _find_no_duplicates(self, name, domain=None, path=None): that match name and optionally domain and path :return: cookie.value """ - toReturn = None + foundCookie = None for cookie in iter(self): if cookie.name == name: if domain is None or cookie.domain == domain: if path is None or cookie.path == path: - if toReturn is not None: + if foundCookie is not None: # if there are multiple cookies that meet passed in criteria raise CookieConflictError( f"There are multiple cookies with name, {name!r}" ) # we will eventually return this as long as no cookie conflict - toReturn = cookie.value + foundCookie = cookie - if toReturn: - return toReturn - raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}") + if foundCookie is None: + raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}") + return foundCookie.value def __getstate__(self): """Unlike a normal CookieJar, this class is pickleable.""" diff --git a/tests/test_cookies.py b/tests/test_cookies.py new file mode 100644 index 0000000000..83978e0c07 --- /dev/null +++ b/tests/test_cookies.py @@ -0,0 +1,62 @@ +from my_requests.cookies import RequestsCookieJar + + +def test_cookie_empty_value_bug(): + jar = RequestsCookieJar() + jar.set('token', 0, domain='example.com', path='/') + + assert list(jar.items()) == [('token', 0)] + assert jar.get('token', domain='example.com', path='/') == 0 + + +def test_cookie_zero_value_among_others(): + jar = RequestsCookieJar() + jar.set('token', 0, domain='example.com', path='/') + jar.set('session', 'abc', domain='example.com', path='/') + + assert jar.get('token', domain='example.com', path='/') == 0 + + +def test_cookie_string_zero_value(): + jar = RequestsCookieJar() + jar.set('code', '0', domain='example.com', path='/') + + assert jar.get('code', domain='example.com', path='/') == '0' + + +def test_cookie_empty_string_value(): + jar = RequestsCookieJar() + jar.set('empty', '', domain='example.com', path='/') + + assert jar.get('empty', domain='example.com', path='/') == '' + + +def test_cookie_none_value(): + jar = RequestsCookieJar() + jar.set('missing', None, domain='example.com', path='/') + + assert jar.get('missing', domain='example.com', path='/') is None + + +def test_cookie_zero_value_with_domain_and_path_matching(): + jar = RequestsCookieJar() + jar.set('token', 0, domain='example.com', path='/api') + + assert jar.get('token', domain='example.com', path='/api') == 0 + + +def test_cookie_overwrite_zero_value(): + jar = RequestsCookieJar() + jar.set('token', 'old', domain='example.com', path='/') + jar.set('token', 0, domain='example.com', path='/') + + assert jar.get('token', domain='example.com', path='/') == 0 + + +def test_cookie_zero_value_persists_after_update(): + jar = RequestsCookieJar() + jar.set('token', 0, domain='example.com', path='/') + + jar.update(jar) + + assert jar.get('token', domain='example.com', path='/') == 0