Skip to content

Commit fbfdd73

Browse files
committed
fix: address review comments for ExpireCookie
- Add CookieSameSiteDisabled handling in ExpireCookie to not set SameSite attribute when explicitly disabled - Fix documentation signature to show fiber.Res receiver instead of fiber.Ctx - Add test case for CookieSameSiteDisabled to verify SameSite is not present in Set-Cookie header
1 parent efcc881 commit fbfdd73

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

ctx_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,9 +4550,10 @@ func Test_Ctx_ExpireCookie(t *testing.T) {
45504550
t.Parallel()
45514551

45524552
testCases := []struct {
4553-
expectedStrs []string
4554-
name string
4555-
cookie Cookie
4553+
expectedStrs []string
4554+
notExpectedStrs []string
4555+
name string
4556+
cookie Cookie
45564557
}{
45574558
{
45584559
name: "with path",
@@ -4621,6 +4622,15 @@ func Test_Ctx_ExpireCookie(t *testing.T) {
46214622
},
46224623
expectedStrs: []string{"partitioned_cookie=;", "Partitioned", "secure", "expires="},
46234624
},
4625+
{
4626+
name: "with SameSite Disabled (should not set SameSite)",
4627+
cookie: Cookie{
4628+
Name: "disabled_samesite",
4629+
SameSite: CookieSameSiteDisabled,
4630+
},
4631+
expectedStrs: []string{"disabled_samesite=;", "expires="},
4632+
notExpectedStrs: []string{"SameSite"},
4633+
},
46244634
}
46254635

46264636
for _, tc := range testCases {
@@ -4636,6 +4646,9 @@ func Test_Ctx_ExpireCookie(t *testing.T) {
46364646
for _, expected := range tc.expectedStrs {
46374647
require.Contains(t, setCookie, expected)
46384648
}
4649+
for _, notExpected := range tc.notExpectedStrs {
4650+
require.NotContains(t, setCookie, notExpected)
4651+
}
46394652
})
46404653
}
46414654
}

docs/api/ctx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ Alternatively, use `ExpireCookie` for a cleaner approach (see below).
17771777
Expires a cookie by its cookie definition. This is useful when you need to expire a cookie that was set with a specific `Path` or `Domain`. The browser will only clear the cookie if the `Path` and `Domain` attributes match the original cookie.
17781778

17791779
```go title="Signature"
1780-
func (c fiber.Ctx) ExpireCookie(cookie *Cookie)
1780+
func (r fiber.Res) ExpireCookie(cookie *fiber.Cookie)
17811781
```
17821782

17831783
```go title="Example"

res.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ func (r *DefaultRes) ExpireCookie(cookie *Cookie) {
250250
isSecure = true // SameSite=None requires Secure
251251
case utils.EqualFold(cookie.SameSite, CookieSameSiteLaxMode):
252252
fcookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
253+
case utils.EqualFold(cookie.SameSite, CookieSameSiteDisabled):
254+
// SameSite explicitly disabled: do not set SameSite attribute
253255
default:
254256
// No SameSite attribute set
255257
}

0 commit comments

Comments
 (0)