Skip to content
Myr

cookies: Take a less cowardly approach

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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...

authored by Michael Becker · committed by Michael Becker · parent 6ac7045 · Browse files · 2 files+73 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+73 added−38 removed

  • other:+73 added−38 removed

2 files+73 added−38 removed

modified:requests/cookies.py+17 added-20 removed
376 unmodified lines
377378379def morsel_to_cookie(morsel):380 """Convert a Morsel object into a Cookie containing the one k/v pair."""381382 expires = None382 if morsel["max-age"]:383 expires = time.time() + morsel["max-age"]383 if morsel['max-age']:384 expires = time.time() + morsel['max-age']385 elif morsel['expires']:385 expires = morsel['expires']386 try:387 time_template = "%a, %d-%b-%Y %H:%M:%S GMT"388 expires = time.mktime(time.strptime(expires, time_template))389 except TypeError:390 pass391 c = create_cookie(392 name=morsel.key,393 value=morsel.value,394 version=morsel['version'] or 0,395 port=None,396 domain=morsel['domain'],397 path=morsel['path'],398 secure=bool(morsel['secure']),399 expires=expires,400 discard=False,386 time_template = '%a, %d-%b-%Y %H:%M:%S GMT'387 expires = time.mktime(time.strptime(morsel['expires'], time_template))388 return create_cookie(389 comment=morsel['comment'],390 comment_url=bool(morsel['comment']),391 discard=False,392 domain=morsel['domain'],393 expires=expires,394 name=morsel.key,395 path=morsel['path'],396 port=None,397 rest={'HttpOnly': morsel['httponly']},404 rfc2109=False,)405 return c398 rfc2109=False,399 secure=bool(morsel['secure']),400 value=morsel.value,401 version=morsel['version'] or 0,402 )403404405def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):406 """Returns a CookieJar from a key/value dictionary.
38 unmodified lines
modified:test_requests.py+56 added-18 removed
758 unmodified lines
759 req = requests.Request('GET', test_url)760 preq = req.prepare()761 assert test_url == preq.url762763 def test_morsel_to_cookie_expires_is_converted(self):764 morsel = Morsel()763766 # Test case where we convert from string time767 morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'768 cookie = morsel_to_cookie(morsel)769 self.assertEquals(cookie.expires, 18001)770771 # Test case where no conversion is required772 morsel['expires'] = 100773 cookie = morsel_to_cookie(morsel)774 self.assertEquals(cookie.expires, 100)775776 # Test case where an invalid string is input777 morsel['expires'] = 'woops'778 with self.assertRaises(ValueError):779 cookie = morsel_to_cookie(morsel)780781764class TestContentEncodingDetection(unittest.TestCase):765766 def test_none(self):767 encodings = requests.utils.get_encodings_from_content('')
236 unmodified lines
1004 assert address_in_network('192.168.1.1', '192.168.1.0/24')1005 assert not address_in_network('172.16.0.1', '192.168.1.0/24')1006100710081009class TestMorselToCookieExpires(unittest.TestCase):10101011 """Tests for morsel_to_cookie when morsel contains expires."""10121013 def test_expires_valid_str(self):1014 """Test case where we convert expires from string time."""10151016 morsel = Morsel()1017 morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'1018 cookie = morsel_to_cookie(morsel)1019 self.assertEquals(cookie.expires, 18001)10201021 def test_expires_invalid_int(self):1022 """Test case where an invalid type is passed for expires."""10231024 morsel = Morsel()1025 morsel['expires'] = 1001026 self.assertRaises(TypeError, morsel_to_cookie, (morsel))10271028 def test_expires_invalid_str(self):1029 """Test case where an invalid string is input."""10301031 morsel = Morsel()1032 morsel['expires'] = 'woops'1033 self.assertRaises(ValueError, morsel_to_cookie, (morsel))10341035 def test_expires_none(self):1036 """Test case where expires is None."""10371038 morsel = Morsel()1039 morsel['expires'] = None1040 cookie = morsel_to_cookie(morsel)1041 self.assertEquals(cookie.expires, None)104210431044class TestMorselToCookieMaxAge(unittest.TestCase):10451046 """Tests for morsel_to_cookie when morsel contains max-age."""10471048 def test_max_age_valid_int(self):1049 """Test case where a valid max age in seconds is passed."""10501051 morsel = Morsel()1052 morsel['max-age'] = 601053 cookie = morsel_to_cookie(morsel)1054 self.assertIsInstance(cookie.expires, int)10551056 def test_max_age_invalid_str(self):1057 """Test case where a invalid max age is passed."""10581059 morsel = Morsel()1060 morsel['max-age'] = 'woops'1061 self.assertRaises(TypeError, morsel_to_cookie, (morsel))106210631064if __name__ == '__main__':1065 unittest.main()