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
Provenance
- Commit
ef5875c- Tree
d0f0850- Author
Michael Becker <mike@beckerfuffle.com>- Committer
Michael Becker <mike@beckerfuffle.com>- Parent
6ac7045
2 files changed+73 added−38 removed
modified:requests/cookies.py+17 added-20 removed
376 unmodified lines377 378 379 def morsel_to_cookie(morsel):380 """Convert a Morsel object into a Cookie containing the one k/v pair."""381+382 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+ )403 404 405 def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):406 """Returns a CookieJar from a key/value dictionary.38 unmodified linesmodified:test_requests.py+56 added-18 removed
758 unmodified lines759 req = requests.Request('GET', test_url)760 preq = req.prepare()761 assert test_url == preq.url762 763- def test_morsel_to_cookie_expires_is_converted(self):764- morsel = Morsel()763 766- # 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)770-771- # Test case where no conversion is required772- morsel['expires'] = 100773- cookie = morsel_to_cookie(morsel)774- self.assertEquals(cookie.expires, 100)775-776- # Test case where an invalid string is input777- morsel['expires'] = 'woops'778- with self.assertRaises(ValueError):779- cookie = morsel_to_cookie(morsel)780-781-764 class TestContentEncodingDetection(unittest.TestCase):765 766 def test_none(self):767 encodings = requests.utils.get_encodings_from_content('')236 unmodified lines1004 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')1006 1007 1008+1009+class TestMorselToCookieExpires(unittest.TestCase):1010+1011+ """Tests for morsel_to_cookie when morsel contains expires."""1012+1013+ def test_expires_valid_str(self):1014+ """Test case where we convert expires from string time."""1015+1016+ 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)1020+1021+ def test_expires_invalid_int(self):1022+ """Test case where an invalid type is passed for expires."""1023+1024+ morsel = Morsel()1025+ morsel['expires'] = 1001026+ self.assertRaises(TypeError, morsel_to_cookie, (morsel))1027+1028+ def test_expires_invalid_str(self):1029+ """Test case where an invalid string is input."""1030+1031+ morsel = Morsel()1032+ morsel['expires'] = 'woops'1033+ self.assertRaises(ValueError, morsel_to_cookie, (morsel))1034+1035+ def test_expires_none(self):1036+ """Test case where expires is None."""1037+1038+ morsel = Morsel()1039+ morsel['expires'] = None1040+ cookie = morsel_to_cookie(morsel)1041+ self.assertEquals(cookie.expires, None)1042+1043+1044+class TestMorselToCookieMaxAge(unittest.TestCase):1045+1046+ """Tests for morsel_to_cookie when morsel contains max-age."""1047+1048+ def test_max_age_valid_int(self):1049+ """Test case where a valid max age in seconds is passed."""1050+1051+ morsel = Morsel()1052+ morsel['max-age'] = 601053+ cookie = morsel_to_cookie(morsel)1054+ self.assertIsInstance(cookie.expires, int)1055+1056+ def test_max_age_invalid_str(self):1057+ """Test case where a invalid max age is passed."""1058+1059+ morsel = Morsel()1060+ morsel['max-age'] = 'woops'1061+ self.assertRaises(TypeError, morsel_to_cookie, (morsel))1062+1063+1064 if __name__ == '__main__':1065 unittest.main()