From: Michael Becker Date: Tue, 3 Dec 2013 16:00:25 +0000 (-0500) Subject: cookies: Take a less cowardly approach X-Git-Tag: v2.2.0~9^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ef5875cc19d8ca34a37662f53e196944595874d8;p=services%2Fpython-requests.git 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... --- diff --git a/requests/cookies.py b/requests/cookies.py index 29b60f8..af33b2e 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -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): diff --git a/test_requests.py b/test_requests.py index 4134153..d999a48 100755 --- a/test_requests.py +++ b/test_requests.py @@ -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()