def session_start_response(status, headers, exc_info=None): session_data = environ[self.environ_key] path = environ['SCRIPT_NAME'] or '/' domain = environ['HTTP_HOST'].split(':', 1)[0] #if there is no registered user #then delete the cookie on the client. #by setting the contents to be the empty string #and the expires time to be now. if session_data: token = self.generate(session_data) expires = datetime.now() + timedelta(minutes=self.lifetime) else: token = self.generate({}) expires = datetime(2000, 1, 1) cookie = Cookie(name=self.cookie_name, value=token, path=path, domain=domain, expires=expires) cookie_value = str(cookie) self.check_cookie_size(cookie_value) headers.append(('Set-Cookie', cookie_value)) return start_response(status, headers, exc_info)
def test_with_all_attributes(): cookie = Cookie('key', 'value', expires=FUTURE, path='/bar', domain='baz.org', max_age=800, secure=True, httponly=True, samesite='Strict') assert str(cookie) == ('key=value; Expires=Tue, 21 Sep 2027 11:22:00 GMT; ' 'Max-Age=800; Domain=baz.org; Path=/bar; Secure; ' 'HttpOnly; SameSite=Strict')
def cookie(self, sid, path="/", domain="localhost"): """We enforce the expiration. """ # Refresh the signature on the sid. ssid = self.refresh_id(sid) # Generate the expiration date using the delta expires = datetime.now() + timedelta(seconds=self.delta) # Create the cookie containing the ssid. cookie = Cookie( name=self.cookie_name, value=ssid, path=path, domain=domain, expires=expires) value = str(cookie) # Check value if len(value) > 4093: # 4096 - 3 bytes of overhead raise ValueError('The Cookie is over 4093 bytes.') return value
def test_create_new_cookie(): cookie = Cookie('key', 'value') assert cookie.name == 'key' assert cookie.value == 'value' assert str(cookie) == 'key=value; Path=/'
def test_value_encoding(value, expected): cookie = Cookie('key', value) assert str(cookie) == expected # Make sure we can read a cookie we have just encoded. assert parse(expected)['key'] == value
def test_can_set_httponly(): cookie = Cookie('key', 'value', httponly=True) assert str(cookie) == 'key=value; Path=/; HttpOnly'
def test_can_set_secure(): cookie = Cookie('key', 'value', secure=True) assert str(cookie) == 'key=value; Path=/; Secure'
def test_can_set_max_age(): cookie = Cookie('key', 'value', max_age=600) assert str(cookie) == 'key=value; Max-Age=600; Path=/'
def test_can_set_domain(): cookie = Cookie('key', 'value', domain='www.example.org') assert str(cookie) == 'key=value; Domain=www.example.org; Path=/'
def test_can_change_path(): cookie = Cookie('key', 'value', path='/foo') assert str(cookie) == 'key=value; Path=/foo'
def test_cookie_expires_format(): cookie = Cookie('key', 'value', expires=FUTURE) assert str(cookie) == ('key=value; Expires=Tue, 21 Sep 2027 11:22:00 GMT; ' 'Path=/')
def set(self, name, *args, **kwargs): self[name] = Cookie(name, *args, **kwargs)
def test_can_set_samesite(): cookie = Cookie('key', 'value', samesite="Strict") assert str(cookie) == 'key=value; Path=/; SameSite=Strict'