Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aws_lambda_powertools/shared/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def __str__(self) -> str:
if self.secure:
payload.write("; Secure")

if self.same_site:
# DEFAULT_MODE leaves the attribute out, so the browser applies its own default
if self.same_site and self.same_site.value:
payload.write(f"; SameSite={self.same_site.value}")

if self.custom_attributes:
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_cookie_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ def test_cookie_with_same_site():
assert str(cookie) == "powertools=test; Path=/; Secure; SameSite=Strict"


def test_cookie_with_same_site_default_mode():
# GIVEN a cookie with the default same_site mode
cookie = Cookie(name="powertools", value="test", path="/", same_site=SameSite.DEFAULT_MODE)

# WHEN getting the cookie's attributes
# THEN the SameSite attribute should be left out instead of sent without a value
assert cookie.same_site == SameSite.DEFAULT_MODE
assert str(cookie) == "powertools=test; Path=/; Secure"


def test_cookie_with_custom_attribute():
# GIVEN a cookie with custom_attributes
cookie = Cookie(name="powertools", value="test", path="/", custom_attributes=["extra1=value1", "extra2=value2"])
Expand Down