def test_max_age_expiration(self): "Cookie will expire if max_age is provided" response = HttpResponse() response.set_cookie("max_age", max_age=10) max_age_cookie = response.cookies["max_age"] self.assertEqual(max_age_cookie["max-age"], 10) self.assertEqual(max_age_cookie["expires"], cookie_date(time.time() + 10))
def test_httponly_cookie(self): response = HttpResponse() response.set_cookie("example", httponly=True) example_cookie = response.cookies["example"] # A compat cookie may be in use -- check that it has worked # both as an output string, and using the cookie attributes self.assertTrue("; httponly" in str(example_cookie)) self.assertTrue(example_cookie["httponly"])
def test_aware_expiration(self): "Cookie accepts an aware datetime as expiration time" response = HttpResponse() expires = (datetime.utcnow() + timedelta(seconds=10)).replace(tzinfo=utc) time.sleep(0.001) response.set_cookie("datetime", expires=expires) datetime_cookie = response.cookies["datetime"] self.assertEqual(datetime_cookie["max-age"], 10)
def test_near_expiration(self): "Cookie will expire when an near expiration time is provided" response = HttpResponse() # There is a timing weakness in this test; The # expected result for max-age requires that there be # a very slight difference between the evaluated expiration # time, and the time evaluated in set_cookie(). If this # difference doesn't exist, the cookie time will be # 1 second larger. To avoid the problem, put in a quick sleep, # which guarantees that there will be a time difference. expires = datetime.utcnow() + timedelta(seconds=10) time.sleep(0.001) response.set_cookie("datetime", expires=expires) datetime_cookie = response.cookies["datetime"] self.assertEqual(datetime_cookie["max-age"], 10)
def test_far_expiration(self): "Cookie will expire when an distant expiration time is provided" response = HttpResponse() response.set_cookie("datetime", expires=datetime(2028, 1, 1, 4, 5, 6)) datetime_cookie = response.cookies["datetime"] self.assertEqual(datetime_cookie["expires"], "Sat, 01-Jan-2028 04:05:06 GMT")