mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
cookies: Take a less cowardly approach
Per discussion with @sigmavirus24, we'll take the less cowardly approach here. "the RFC defines the Expires header as a string with the format we're assigning to ``time_template`` so checking if it is a string is unnecessary." Crossing my fingers that this doesn't break anyones existing code...
This commit is contained in:
+17
-20
@@ -378,31 +378,28 @@ def create_cookie(name, value, **kwargs):
|
||||
|
||||
def morsel_to_cookie(morsel):
|
||||
"""Convert a Morsel object into a Cookie containing the one k/v pair."""
|
||||
|
||||
expires = None
|
||||
if morsel["max-age"]:
|
||||
expires = time.time() + morsel["max-age"]
|
||||
if morsel['max-age']:
|
||||
expires = time.time() + morsel['max-age']
|
||||
elif morsel['expires']:
|
||||
expires = morsel['expires']
|
||||
try:
|
||||
time_template = "%a, %d-%b-%Y %H:%M:%S GMT"
|
||||
expires = time.mktime(time.strptime(expires, time_template))
|
||||
except TypeError:
|
||||
pass
|
||||
c = create_cookie(
|
||||
name=morsel.key,
|
||||
value=morsel.value,
|
||||
version=morsel['version'] or 0,
|
||||
port=None,
|
||||
domain=morsel['domain'],
|
||||
path=morsel['path'],
|
||||
secure=bool(morsel['secure']),
|
||||
expires=expires,
|
||||
discard=False,
|
||||
time_template = '%a, %d-%b-%Y %H:%M:%S GMT'
|
||||
expires = time.mktime(time.strptime(morsel['expires'], time_template))
|
||||
return create_cookie(
|
||||
comment=morsel['comment'],
|
||||
comment_url=bool(morsel['comment']),
|
||||
discard=False,
|
||||
domain=morsel['domain'],
|
||||
expires=expires,
|
||||
name=morsel.key,
|
||||
path=morsel['path'],
|
||||
port=None,
|
||||
rest={'HttpOnly': morsel['httponly']},
|
||||
rfc2109=False,)
|
||||
return c
|
||||
rfc2109=False,
|
||||
secure=bool(morsel['secure']),
|
||||
value=morsel.value,
|
||||
version=morsel['version'] or 0,
|
||||
)
|
||||
|
||||
|
||||
def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):
|
||||
|
||||
+56
-18
@@ -760,24 +760,6 @@ class RequestsTestCase(unittest.TestCase):
|
||||
preq = req.prepare()
|
||||
assert test_url == preq.url
|
||||
|
||||
def test_morsel_to_cookie_expires_is_converted(self):
|
||||
morsel = Morsel()
|
||||
|
||||
# Test case where we convert from string time
|
||||
morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
self.assertEquals(cookie.expires, 18001)
|
||||
|
||||
# Test case where no conversion is required
|
||||
morsel['expires'] = 100
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
self.assertEquals(cookie.expires, 100)
|
||||
|
||||
# Test case where an invalid string is input
|
||||
morsel['expires'] = 'woops'
|
||||
with self.assertRaises(ValueError):
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
|
||||
|
||||
class TestContentEncodingDetection(unittest.TestCase):
|
||||
|
||||
@@ -1023,5 +1005,61 @@ class UtilsTestCase(unittest.TestCase):
|
||||
assert not address_in_network('172.16.0.1', '192.168.1.0/24')
|
||||
|
||||
|
||||
|
||||
class TestMorselToCookieExpires(unittest.TestCase):
|
||||
|
||||
"""Tests for morsel_to_cookie when morsel contains expires."""
|
||||
|
||||
def test_expires_valid_str(self):
|
||||
"""Test case where we convert expires from string time."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
self.assertEquals(cookie.expires, 18001)
|
||||
|
||||
def test_expires_invalid_int(self):
|
||||
"""Test case where an invalid type is passed for expires."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['expires'] = 100
|
||||
self.assertRaises(TypeError, morsel_to_cookie, (morsel))
|
||||
|
||||
def test_expires_invalid_str(self):
|
||||
"""Test case where an invalid string is input."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['expires'] = 'woops'
|
||||
self.assertRaises(ValueError, morsel_to_cookie, (morsel))
|
||||
|
||||
def test_expires_none(self):
|
||||
"""Test case where expires is None."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['expires'] = None
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
self.assertEquals(cookie.expires, None)
|
||||
|
||||
|
||||
class TestMorselToCookieMaxAge(unittest.TestCase):
|
||||
|
||||
"""Tests for morsel_to_cookie when morsel contains max-age."""
|
||||
|
||||
def test_max_age_valid_int(self):
|
||||
"""Test case where a valid max age in seconds is passed."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['max-age'] = 60
|
||||
cookie = morsel_to_cookie(morsel)
|
||||
self.assertIsInstance(cookie.expires, int)
|
||||
|
||||
def test_max_age_invalid_str(self):
|
||||
"""Test case where a invalid max age is passed."""
|
||||
|
||||
morsel = Morsel()
|
||||
morsel['max-age'] = 'woops'
|
||||
self.assertRaises(TypeError, morsel_to_cookie, (morsel))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user