| 1 |
import time |
| 2 |
from datetime import date, datetime, timedelta, timezone |
| 3 |
from email.utils import format_datetime as format_datetime_rfc5322 |
| 4 |
from http import cookies |
| 5 |
|
| 6 |
from django.http import HttpResponse |
| 7 |
from django.test import SimpleTestCase |
| 8 |
from django.test.utils import freeze_time |
| 9 |
from django.utils.http import http_date |
| 10 |
|
| 11 |
|
| 12 |
class SetCookieTests(SimpleTestCase): |
| 13 |
def test_near_expiration(self): |
| 14 |
"""Cookie will expire when a near expiration time is provided.""" |
| 15 |
response = HttpResponse() |
| 16 |
# There's a timing weakness in this test; The expected result for |
| 17 |
# max-age requires that there be a very slight difference between the |
| 18 |
# evaluated expiration time and the time evaluated in set_cookie(). If |
| 19 |
# this difference doesn't exist, the cookie time will be 1 second |
| 20 |
# larger. The sleep guarantees that there will be a time difference. |
| 21 |
expires = datetime.now(tz=timezone.utc).replace(tzinfo=None) + timedelta( |
| 22 |
seconds=10 |
| 23 |
) |
| 24 |
time.sleep(0.001) |
| 25 |
response.set_cookie("datetime", expires=expires) |
| 26 |
datetime_cookie = response.cookies["datetime"] |
| 27 |
self.assertEqual(datetime_cookie["max-age"], 10) |
| 28 |
|
| 29 |
def test_aware_expiration(self): |
| 30 |
"""set_cookie() accepts an aware datetime as expiration time.""" |
| 31 |
response = HttpResponse() |
| 32 |
expires = datetime.now(tz=timezone.utc) + timedelta(seconds=10) |
| 33 |
time.sleep(0.001) |
| 34 |
response.set_cookie("datetime", expires=expires) |
| 35 |
datetime_cookie = response.cookies["datetime"] |
| 36 |
self.assertEqual(datetime_cookie["max-age"], 10) |
| 37 |
|
| 38 |
def test_create_cookie_after_deleting_cookie(self): |
| 39 |
"""Setting a cookie after deletion clears the expiry date.""" |
| 40 |
response = HttpResponse() |
| 41 |
response.set_cookie("c", "old-value") |
| 42 |
self.assertEqual(response.cookies["c"]["expires"], "") |
| 43 |
response.delete_cookie("c") |
| 44 |
self.assertEqual( |
| 45 |
response.cookies["c"]["expires"], "Thu, 01 Jan 1970 00:00:00 GMT" |
| 46 |
) |
| 47 |
response.set_cookie("c", "new-value") |
| 48 |
self.assertEqual(response.cookies["c"]["expires"], "") |
| 49 |
|
| 50 |
def test_far_expiration(self): |
| 51 |
"""Cookie will expire when a distant expiration time is provided.""" |
| 52 |
response = HttpResponse() |
| 53 |
future_datetime = datetime( |
| 54 |
date.today().year + 2, 1, 1, 4, 5, 6, tzinfo=timezone.utc |
| 55 |
) |
| 56 |
response.set_cookie("datetime", expires=future_datetime) |
| 57 |
datetime_cookie = response.cookies["datetime"] |
| 58 |
self.assertIn( |
| 59 |
datetime_cookie["expires"], |
| 60 |
# assertIn accounts for slight time dependency (#23450) |
| 61 |
( |
| 62 |
format_datetime_rfc5322(future_datetime, usegmt=True), |
| 63 |
format_datetime_rfc5322(future_datetime.replace(second=7), usegmt=True), |
| 64 |
), |
| 65 |
) |
| 66 |
|
| 67 |
def test_max_age_expiration(self): |
| 68 |
"""Cookie will expire if max_age is provided.""" |
| 69 |
response = HttpResponse() |
| 70 |
set_cookie_time = time.time() |
| 71 |
with freeze_time(set_cookie_time): |
| 72 |
response.set_cookie("max_age", max_age=10) |
| 73 |
max_age_cookie = response.cookies["max_age"] |
| 74 |
self.assertEqual(max_age_cookie["max-age"], 10) |
| 75 |
self.assertEqual(max_age_cookie["expires"], http_date(set_cookie_time + 10)) |
| 76 |
|
| 77 |
def test_max_age_int(self): |
| 78 |
response = HttpResponse() |
| 79 |
response.set_cookie("max_age", max_age=10.6) |
| 80 |
self.assertEqual(response.cookies["max_age"]["max-age"], 10) |
| 81 |
|
| 82 |
def test_max_age_timedelta(self): |
| 83 |
response = HttpResponse() |
| 84 |
response.set_cookie("max_age", max_age=timedelta(hours=1)) |
| 85 |
self.assertEqual(response.cookies["max_age"]["max-age"], 3600) |
| 86 |
|
| 87 |
def test_max_age_with_expires(self): |
| 88 |
response = HttpResponse() |
| 89 |
msg = "'expires' and 'max_age' can't be used together." |
| 90 |
with self.assertRaisesMessage(ValueError, msg): |
| 91 |
response.set_cookie( |
| 92 |
"max_age", expires=datetime(2000, 1, 1), max_age=timedelta(hours=1) |
| 93 |
) |
| 94 |
|
| 95 |
def test_httponly_cookie(self): |
| 96 |
response = HttpResponse() |
| 97 |
response.set_cookie("example", httponly=True) |
| 98 |
example_cookie = response.cookies["example"] |
| 99 |
self.assertIn( |
| 100 |
"; %s" % cookies.Morsel._reserved["httponly"], str(example_cookie) |
| 101 |
) |
| 102 |
self.assertIs(example_cookie["httponly"], True) |
| 103 |
|
| 104 |
def test_unicode_cookie(self): |
| 105 |
"""HttpResponse.set_cookie() works with Unicode data.""" |
| 106 |
response = HttpResponse() |
| 107 |
cookie_value = "清風" |
| 108 |
response.set_cookie("test", cookie_value) |
| 109 |
self.assertEqual(response.cookies["test"].value, cookie_value) |
| 110 |
|
| 111 |
def test_samesite(self): |
| 112 |
response = HttpResponse() |
| 113 |
response.set_cookie("example", samesite="None") |
| 114 |
self.assertEqual(response.cookies["example"]["samesite"], "None") |
| 115 |
response.set_cookie("example", samesite="Lax") |
| 116 |
self.assertEqual(response.cookies["example"]["samesite"], "Lax") |
| 117 |
response.set_cookie("example", samesite="strict") |
| 118 |
self.assertEqual(response.cookies["example"]["samesite"], "strict") |
| 119 |
|
| 120 |
def test_invalid_samesite(self): |
| 121 |
msg = 'samesite must be "lax", "none", or "strict".' |
| 122 |
with self.assertRaisesMessage(ValueError, msg): |
| 123 |
HttpResponse().set_cookie("example", samesite="invalid") |
| 124 |
|
| 125 |
|
| 126 |
class DeleteCookieTests(SimpleTestCase): |
| 127 |
def test_default(self): |
| 128 |
response = HttpResponse() |
| 129 |
response.delete_cookie("c") |
| 130 |
cookie = response.cookies["c"] |
| 131 |
self.assertEqual(cookie["expires"], "Thu, 01 Jan 1970 00:00:00 GMT") |
| 132 |
self.assertEqual(cookie["max-age"], 0) |
| 133 |
self.assertEqual(cookie["path"], "/") |
| 134 |
self.assertEqual(cookie["secure"], "") |
| 135 |
self.assertEqual(cookie["domain"], "") |
| 136 |
self.assertEqual(cookie["samesite"], "") |
| 137 |
|
| 138 |
def test_delete_cookie_secure_prefix(self): |
| 139 |
""" |
| 140 |
delete_cookie() sets the secure flag if the cookie name starts with |
| 141 |
__Host- or __Secure- (without that, browsers ignore cookies with those |
| 142 |
prefixes). |
| 143 |
""" |
| 144 |
response = HttpResponse() |
| 145 |
for prefix in ("Secure", "Host"): |
| 146 |
with self.subTest(prefix=prefix): |
| 147 |
cookie_name = "__%s-c" % prefix |
| 148 |
response.delete_cookie(cookie_name) |
| 149 |
self.assertIs(response.cookies[cookie_name]["secure"], True) |
| 150 |
|
| 151 |
def test_delete_cookie_secure_samesite_none(self): |
| 152 |
# delete_cookie() sets the secure flag if samesite='none'. |
| 153 |
response = HttpResponse() |
| 154 |
response.delete_cookie("c", samesite="none") |
| 155 |
self.assertIs(response.cookies["c"]["secure"], True) |
| 156 |
|
| 157 |
def test_delete_cookie_samesite(self): |
| 158 |
response = HttpResponse() |
| 159 |
response.delete_cookie("c", samesite="lax") |
| 160 |
self.assertEqual(response.cookies["c"]["samesite"], "lax") |