Skip to content

Commit 7c70e48

Browse files
committed
Fix cookie value bug for empty strings
1 parent 7029833 commit 7c70e48

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

src/requests/cookies.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,22 @@ def _find_no_duplicates(self, name, domain=None, path=None):
395395
that match name and optionally domain and path
396396
:return: cookie.value
397397
"""
398-
toReturn = None
398+
foundCookie = None
399399
for cookie in iter(self):
400400
if cookie.name == name:
401401
if domain is None or cookie.domain == domain:
402402
if path is None or cookie.path == path:
403-
if toReturn is not None:
403+
if foundCookie is not None:
404404
# if there are multiple cookies that meet passed in criteria
405405
raise CookieConflictError(
406406
f"There are multiple cookies with name, {name!r}"
407407
)
408408
# we will eventually return this as long as no cookie conflict
409-
toReturn = cookie.value
409+
foundCookie = cookie
410410

411-
if toReturn:
412-
return toReturn
413-
raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")
411+
if foundCookie is None:
412+
raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")
413+
return foundCookie.value
414414

415415
def __getstate__(self):
416416
"""Unlike a normal CookieJar, this class is pickleable."""

tests/test_cookies.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from my_requests.cookies import RequestsCookieJar
2+
3+
4+
def test_cookie_empty_value_bug():
5+
jar = RequestsCookieJar()
6+
jar.set('token', 0, domain='example.com', path='/')
7+
8+
assert list(jar.items()) == [('token', 0)]
9+
assert jar.get('token', domain='example.com', path='/') == 0
10+
11+
12+
def test_cookie_zero_value_among_others():
13+
jar = RequestsCookieJar()
14+
jar.set('token', 0, domain='example.com', path='/')
15+
jar.set('session', 'abc', domain='example.com', path='/')
16+
17+
assert jar.get('token', domain='example.com', path='/') == 0
18+
19+
20+
def test_cookie_string_zero_value():
21+
jar = RequestsCookieJar()
22+
jar.set('code', '0', domain='example.com', path='/')
23+
24+
assert jar.get('code', domain='example.com', path='/') == '0'
25+
26+
27+
def test_cookie_empty_string_value():
28+
jar = RequestsCookieJar()
29+
jar.set('empty', '', domain='example.com', path='/')
30+
31+
assert jar.get('empty', domain='example.com', path='/') == ''
32+
33+
34+
def test_cookie_none_value():
35+
jar = RequestsCookieJar()
36+
jar.set('missing', None, domain='example.com', path='/')
37+
38+
assert jar.get('missing', domain='example.com', path='/') is None
39+
40+
41+
def test_cookie_zero_value_with_domain_and_path_matching():
42+
jar = RequestsCookieJar()
43+
jar.set('token', 0, domain='example.com', path='/api')
44+
45+
assert jar.get('token', domain='example.com', path='/api') == 0
46+
47+
48+
def test_cookie_overwrite_zero_value():
49+
jar = RequestsCookieJar()
50+
jar.set('token', 'old', domain='example.com', path='/')
51+
jar.set('token', 0, domain='example.com', path='/')
52+
53+
assert jar.get('token', domain='example.com', path='/') == 0
54+
55+
56+
def test_cookie_zero_value_persists_after_update():
57+
jar = RequestsCookieJar()
58+
jar.set('token', 0, domain='example.com', path='/')
59+
60+
jar.update(jar)
61+
62+
assert jar.get('token', domain='example.com', path='/') == 0

0 commit comments

Comments
 (0)