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
12 changes: 6 additions & 6 deletions src/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
62 changes: 62 additions & 0 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -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