Skip to content

Commit eca4b50

Browse files
Fix bug in Max-Age attribute
1 parent 10d551c commit eca4b50

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

aws_lambda_powertools/shared/cookies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ def __str__(self) -> str:
9999

100100
if self.max_age:
101101
if self.max_age > 0:
102-
payload.write(f"; MaxAge={self.max_age}")
102+
payload.write(f"; Max-Age={self.max_age}")
103103
else:
104104
# negative or zero max-age should be set to 0
105-
payload.write("; MaxAge=0")
105+
payload.write("; Max-Age=0")
106106

107107
if self.http_only:
108108
payload.write("; HttpOnly")

tests/unit/test_cookie_class.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from datetime import datetime
2+
3+
from aws_lambda_powertools.shared.cookies import Cookie, SameSite
4+
5+
6+
def test_cookie_without_secure():
7+
# GIVEN a cookie without secure
8+
cookie = Cookie(name="powertools", value="test", path="/", secure=False)
9+
10+
# WHEN getting the cookie's attributes
11+
# THEN the path attribute should be set to the provided value
12+
assert cookie.secure is False
13+
assert str(cookie) == "powertools=test; Path=/"
14+
15+
16+
def test_cookie_with_path():
17+
# GIVEN a cookie with a path
18+
cookie = Cookie(name="powertools", value="test", path="/")
19+
20+
# WHEN getting the cookie's attributes
21+
# THEN the path attribute should be set to the provided value
22+
assert cookie.name == "powertools"
23+
assert cookie.value == "test"
24+
assert cookie.path == "/"
25+
assert str(cookie) == "powertools=test; Path=/; Secure"
26+
27+
28+
def test_cookie_with_domain():
29+
# GIVEN a cookie with a domain
30+
cookie = Cookie(name="powertools", value="test", path="/", domain="example.com")
31+
32+
# WHEN getting the cookie's attributes
33+
# THEN the path attribute should be set to the provided value
34+
assert cookie.name == "powertools"
35+
assert cookie.value == "test"
36+
assert cookie.path == "/"
37+
assert cookie.domain == "example.com"
38+
assert str(cookie) == "powertools=test; Path=/; Domain=example.com; Secure"
39+
40+
41+
def test_cookie_with_expires():
42+
# GIVEN a cookie with a expires
43+
time_to_expire = datetime(year=2022, month=12, day=31)
44+
cookie = Cookie(name="powertools", value="test", path="/", expires=time_to_expire)
45+
46+
# WHEN getting the cookie's attributes
47+
# THEN the path attribute should be set to the provided value
48+
assert cookie.name == "powertools"
49+
assert cookie.value == "test"
50+
assert cookie.path == "/"
51+
assert cookie.expires == time_to_expire
52+
assert str(cookie) == "powertools=test; Path=/; Expires=Sat, 31 Dec 2022 00:00:00 GMT; Secure"
53+
54+
55+
def test_cookie_with_max_age_positive():
56+
# GIVEN a cookie with a positive max age
57+
cookie = Cookie(name="powertools", value="test", path="/", max_age=100)
58+
59+
# WHEN getting the cookie's attributes
60+
# THEN the path attribute should be set to the provided value
61+
assert cookie.name == "powertools"
62+
assert cookie.value == "test"
63+
assert cookie.path == "/"
64+
assert cookie.max_age == 100
65+
assert str(cookie) == "powertools=test; Path=/; Max-Age=100; Secure"
66+
67+
68+
def test_cookie_with_max_age_negative():
69+
# GIVEN a cookie with a negative max age
70+
cookie = Cookie(name="powertools", value="test", path="/", max_age=-100)
71+
72+
# WHEN getting the cookie's attributes
73+
# THEN the path attribute should be set to the provided value and Max-Age must be 0
74+
assert cookie.name == "powertools"
75+
assert cookie.value == "test"
76+
assert cookie.path == "/"
77+
assert str(cookie) == "powertools=test; Path=/; Max-Age=0; Secure"
78+
79+
80+
def test_cookie_with_http_only():
81+
# GIVEN a cookie with http_only
82+
cookie = Cookie(name="powertools", value="test", path="/", http_only=True)
83+
84+
# WHEN getting the cookie's attributes
85+
# THEN the path attribute should be set to the provided value
86+
assert cookie.name == "powertools"
87+
assert cookie.value == "test"
88+
assert cookie.path == "/"
89+
assert str(cookie) == "powertools=test; Path=/; HttpOnly; Secure"
90+
91+
92+
def test_cookie_with_same_site():
93+
# GIVEN a cookie with same_site
94+
cookie = Cookie(name="powertools", value="test", path="/", same_site=SameSite.STRICT_MODE)
95+
96+
# WHEN getting the cookie's attributes
97+
# THEN the path attribute should be set to the provided value
98+
assert cookie.name == "powertools"
99+
assert cookie.value == "test"
100+
assert cookie.path == "/"
101+
assert str(cookie) == "powertools=test; Path=/; Secure; SameSite=Strict"
102+
103+
104+
def test_cookie_with_custom_attribute():
105+
# GIVEN a cookie with custom_attributes
106+
cookie = Cookie(name="powertools", value="test", path="/", custom_attributes=["extra1=value1", "extra2=value2"])
107+
108+
# WHEN getting the cookie's attributes
109+
# THEN the path attribute should be set to the provided value
110+
assert cookie.name == "powertools"
111+
assert cookie.value == "test"
112+
assert cookie.path == "/"
113+
assert str(cookie) == "powertools=test; Path=/; Secure; extra1=value1; extra2=value2"

0 commit comments

Comments
 (0)