def set_cookie(key, value, path = "/", expires_days = 60): """ Schreibt das Cookie in den **Response**. """ cookie = cherrypy.response.cookie # Cookie setzen cookie[key] = value cookie[key]["path"] = "/" expires = time.time() + (expires_days * 24 * 60 * 60) cookie[key]["expires"] = cookielib.time2netscape(expires)
def test_expires(self): from cookielib import time2netscape # if expires is in future, keep cookie... c = CookieJar() future = time2netscape(time.time()+3600) interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' % future) self.assertEquals(len(c), 1) now = time2netscape(time.time()-1) # ... and if in past or present, discard it interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' % now) h = interact_netscape(c, "http://www.acme.com/") self.assertEquals(len(c), 1) self.assert_('spam="bar"' in h and "foo" not in h) # max-age takes precedence over expires, and zero max-age is request to # delete both new cookie and any old matching cookie interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' % future) interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' % future) self.assertEquals(len(c), 3) interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; ' 'expires=%s; max-age=0' % future) interact_netscape(c, "http://www.acme.com/", 'bar="bar"; ' 'max-age=0; expires=%s' % future) h = interact_netscape(c, "http://www.acme.com/") self.assertEquals(len(c), 1) # test expiry at end of session for cookies with no expires attribute interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"') self.assertEquals(len(c), 2) c.clear_session_cookies() self.assertEquals(len(c), 1) self.assert_('spam="bar"' in h)