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):
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):
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()