def test_expired_cookie(self, mock_request): response = Response() response.status_code = 200 response._content = 'Mocked response content' response.headers = { 'Set-Cookie': 'a=apple; expires=%s;, b=banana; max-age=6' % _getdate(future=3) } response.url = 'http://www.fruits.com' mock_request.return_value = response get('http://www.fruits.com/path') dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=1) get('http://www.fruits.com/path') mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True, cookies={'a': 'apple', 'b': 'banana'}) dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=4) get('http://www.fruits.com/path') mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True, cookies={'b': 'banana'}) dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=11) get('http://www.fruits.com/path') mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True)
def tmp_func(self, attrs=None): # Build up our result # result = [] RA = result.append # First, the key=value pair RA("%s=%s" % (self.key, self.coded_value)) # Now add any defined attributes if attrs is None: attrs = self._reserved items = self.items() items.sort() for K, V in items: if V == "": continue if K not in attrs: continue if K == "expires" and type(V) == type(1): RA("%s=%s" % (self._reserved[K], _getdate(V))) elif K == "max-age" and type(V) == type(1): RA("%s=%d" % (self._reserved[K], V)) elif K == "secure": RA(str(self._reserved[K])) elif K == "httponly": RA(str(self._reserved[K])) else: RA("%s=%s" % (self._reserved[K], V)) # Return the result return _semispacejoin(result)
def tmp_func(self, attrs=None): # Build up our result # result = [] RA = result.append # First, the key=value pair RA("%s=%s" % (self.key, self.coded_value)) # Now add any defined attributes if attrs is None: attrs = self._reserved items = self.items() items.sort() for K,V in items: if V == "": continue if K not in attrs: continue if K == "expires" and type(V) == type(1): RA("%s=%s" % (self._reserved[K], _getdate(V))) elif K == "max-age" and type(V) == type(1): RA("%s=%d" % (self._reserved[K], V)) elif K == "secure": RA(str(self._reserved[K])) elif K == "httponly": RA(str(self._reserved[K])) else: RA("%s=%s" % (self._reserved[K], V)) # Return the result return _semispacejoin(result)
def test_origin_cookies_with_max_age(self): """ Test setting and getting origin cookies with 'Max-Age' and/or 'Expires' attribute """ # Cookie cache keys for convenience lookup_key = self.cookie_manager.get_origin_cookie_lookup_key("sweet.test.com") chipsahoy_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "chipsahoy") coke_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "coke") squeeze_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "squeeze") kitkat_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "kitkat") # Prepare test responses response0 = Response() response0.headers = { "Set-Cookie": "chipsahoy=cookie; Max-Age=3;, " + "coke=soda; Max-Age=6;, " + "squeeze=juice; Expires=%s;, " % _getdate(future=3) + "kitkat=chocolate;" } response0.url = "http://sweet.test.com/path" ##### Process response cookie #### self.cookie_manager.process_response(None, response0) # Note that 'request' is not used (thus None param) ##### Process request ##### request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual( request.cookies, {"chipsahoy": "cookie", "coke": "soda", "squeeze": "juice", "kitkat": "chocolate"} ) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([chipsahoy_key, coke_key, squeeze_key, kitkat_key])) # 3 seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3) request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {"coke": "soda", "kitkat": "chocolate"}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([coke_key, kitkat_key])) # 3 more seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=6) request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {"kitkat": "chocolate"}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([kitkat_key]))
def test_domain_cookies_with_max_age(self): """ Test setting and getting domain cookies with 'Max-Age' and/or 'Expires' attribute """ # Cache keys for convenience lookup_key = self.cookie_manager.get_domain_cookie_lookup_key("sweet.test.com") chipsahoy_key = self.cookie_manager.get_domain_cookie_key("sweet.test.com", "", "chipsahoy") cadbury_key = self.cookie_manager.get_domain_cookie_key("sweet.test.com", "", "cadbury") kfc_key = self.cookie_manager.get_domain_cookie_key("sweet.test.com", "", "kfc") happymeal_key = self.cookie_manager.get_domain_cookie_key("sweet.test.com", "", "happymeal") # Prepare test response response = Response() response.headers = { "Set-Cookie": "chipsahoy=cookie; Domain=sweet.test.com; Max-Age=3;, " + "cadbury=chocolate; Domain=sweet.test.com; Max-Age=6;, " + "kfc=chicken; Domain=sweet.test.com; Expires=%s;, " % _getdate(future=3) + "happymeal=meal; Domain=sweet.test.com;, " } response.url = "http://sweet.test.com/path" ##### Process response cookies ##### self.cookie_manager.process_response(None, response) # Note that 'request' is not used (thus None param) ##### Process request ##### request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual( request.cookies, {"chipsahoy": "cookie", "cadbury": "chocolate", "kfc": "chicken", "happymeal": "meal"} ) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([chipsahoy_key, cadbury_key, kfc_key, happymeal_key])) # 3 seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3) request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {"cadbury": "chocolate", "happymeal": "meal"}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([cadbury_key, happymeal_key])) # 3 more seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=6) request = Request("http://sweet.test.com/help") self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {"happymeal": "meal"}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([happymeal_key]))
def test_origin_cookies_with_max_age(self): """ Test setting and getting origin cookies with 'Max-Age' and/or 'Expires' attribute """ # Cookie cache keys for convenience lookup_key = self.cookie_manager.get_origin_cookie_lookup_key('sweet.test.com') chipsahoy_key = self.cookie_manager.get_origin_cookie_key('sweet.test.com', '', 'chipsahoy') coke_key = self.cookie_manager.get_origin_cookie_key('sweet.test.com', '', 'coke') squeeze_key = self.cookie_manager.get_origin_cookie_key('sweet.test.com', '', 'squeeze') kitkat_key = self.cookie_manager.get_origin_cookie_key('sweet.test.com', '', 'kitkat') # Prepare test responses response0 = Response() response0.headers = { 'Set-Cookie': 'chipsahoy=cookie; Max-Age=3;, ' + 'coke=soda; Max-Age=6;, ' + 'squeeze=juice; Expires=%s;, ' % _getdate(future=3) + 'kitkat=chocolate;' } response0.url = 'http://sweet.test.com/path' ##### Process response cookie #### self.cookie_manager.process_response(None, response0) # Note that 'request' is not used (thus None param) ##### Process request ##### request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'chipsahoy': 'cookie', 'coke': 'soda', 'squeeze': 'juice', 'kitkat': 'chocolate'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([chipsahoy_key, coke_key, squeeze_key, kitkat_key])) # 3 seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3) request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'coke': 'soda', 'kitkat': 'chocolate'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([coke_key, kitkat_key])) # 3 more seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=6) request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'kitkat': 'chocolate'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([kitkat_key]))
def test_domain_cookies_with_max_age(self): """ Test setting and getting domain cookies with 'Max-Age' and/or 'Expires' attribute """ # Cache keys for convenience lookup_key = self.cookie_manager.get_domain_cookie_lookup_key('sweet.test.com') chipsahoy_key = self.cookie_manager.get_domain_cookie_key('sweet.test.com', '', 'chipsahoy') cadbury_key = self.cookie_manager.get_domain_cookie_key('sweet.test.com', '', 'cadbury') kfc_key = self.cookie_manager.get_domain_cookie_key('sweet.test.com', '', 'kfc') happymeal_key = self.cookie_manager.get_domain_cookie_key('sweet.test.com', '', 'happymeal') # Prepare test response response = Response() response.headers = { 'Set-Cookie': 'chipsahoy=cookie; Domain=sweet.test.com; Max-Age=3;, ' + 'cadbury=chocolate; Domain=sweet.test.com; Max-Age=6;, ' + 'kfc=chicken; Domain=sweet.test.com; Expires=%s;, ' % _getdate(future=3) + 'happymeal=meal; Domain=sweet.test.com;, ' } response.url = 'http://sweet.test.com/path' ##### Process response cookies ##### self.cookie_manager.process_response(None, response) # Note that 'request' is not used (thus None param) ##### Process request ##### request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'chipsahoy': 'cookie', 'cadbury': 'chocolate', 'kfc': 'chicken', 'happymeal': 'meal'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([chipsahoy_key, cadbury_key, kfc_key, happymeal_key])) # 3 seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3) request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'cadbury': 'chocolate', 'happymeal': 'meal'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([cadbury_key, happymeal_key])) # 3 more seconds pass by dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=6) request = Request('http://sweet.test.com/help') self.cookie_manager.process_request(request) self.assertEqual(request.cookies, {'happymeal': 'meal'}) cookie_keys_set = self.cookie_cache.get(lookup_key) self.assertEqual(cookie_keys_set, set([happymeal_key]))