Skip to content

Commit 3ae2eec

Browse files
fix(event_handler): make the max_age attribute comply with RFC specification (#4731)
* Fix bug in Max-Age attribute * Add more fields to test
1 parent 599f6fe commit 3ae2eec

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

Diff for: aws_lambda_powertools/shared/cookies.py

+2-2
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")

Diff for: tests/unit/test_cookie_class.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 cookie.http_only is True
90+
assert str(cookie) == "powertools=test; Path=/; HttpOnly; Secure"
91+
92+
93+
def test_cookie_with_same_site():
94+
# GIVEN a cookie with same_site
95+
cookie = Cookie(name="powertools", value="test", path="/", same_site=SameSite.STRICT_MODE)
96+
97+
# WHEN getting the cookie's attributes
98+
# THEN the path attribute should be set to the provided value
99+
assert cookie.name == "powertools"
100+
assert cookie.value == "test"
101+
assert cookie.path == "/"
102+
assert cookie.same_site == SameSite.STRICT_MODE
103+
assert str(cookie) == "powertools=test; Path=/; Secure; SameSite=Strict"
104+
105+
106+
def test_cookie_with_custom_attribute():
107+
# GIVEN a cookie with custom_attributes
108+
cookie = Cookie(name="powertools", value="test", path="/", custom_attributes=["extra1=value1", "extra2=value2"])
109+
110+
# WHEN getting the cookie's attributes
111+
# THEN the path attribute should be set to the provided value
112+
assert cookie.name == "powertools"
113+
assert cookie.value == "test"
114+
assert cookie.path == "/"
115+
assert cookie.custom_attributes == ["extra1=value1", "extra2=value2"]
116+
assert str(cookie) == "powertools=test; Path=/; Secure; extra1=value1; extra2=value2"

0 commit comments

Comments
 (0)