Skip to content

Commit 0d64661

Browse files
authored
Add more unit-tests
1 parent a9d2ee9 commit 0d64661

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

ctx.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
400400
if cookie.Path == "" {
401401
cookie.Path = "/"
402402
}
403+
403404
if cookie.SessionOnly {
404405
cookie.MaxAge = 0
405406
cookie.Expires = time.Time{}
@@ -431,7 +432,9 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
431432
SameSite: sameSite,
432433
Partitioned: cookie.Partitioned,
433434
}
435+
434436
if err := hc.Valid(); err != nil {
437+
// invalid cookies are ignored, same approach as net/http
435438
return
436439
}
437440

@@ -441,12 +444,15 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
441444
fcookie.SetValue(hc.Value)
442445
fcookie.SetPath(hc.Path)
443446
fcookie.SetDomain(hc.Domain)
447+
444448
if !cookie.SessionOnly {
445449
fcookie.SetMaxAge(hc.MaxAge)
446450
fcookie.SetExpire(hc.Expires)
447451
}
452+
448453
fcookie.SetSecure(hc.Secure)
449454
fcookie.SetHTTPOnly(hc.HttpOnly)
455+
450456
switch sameSite {
451457
case http.SameSiteLaxMode:
452458
fcookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
@@ -459,7 +465,10 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
459465
default:
460466
fcookie.SetSameSite(fasthttp.CookieSameSiteDisabled)
461467
}
468+
462469
fcookie.SetPartitioned(hc.Partitioned)
470+
471+
// Set resp header
463472
c.fasthttp.Response.Header.SetCookie(fcookie)
464473
fasthttp.ReleaseCookie(fcookie)
465474
}

ctx_test.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,6 @@ func Test_Ctx_Cookie_PartitionedSecure(t *testing.T) {
941941
func Test_Ctx_Cookie_Invalid(t *testing.T) {
942942
t.Parallel()
943943
app := New()
944-
c := app.AcquireCtx(&fasthttp.RequestCtx{})
945944

946945
cases := []*Cookie{
947946
{Name: "", Value: "a"}, // empty name
@@ -956,10 +955,71 @@ func Test_Ctx_Cookie_Invalid(t *testing.T) {
956955
}
957956

958957
for _, invalid := range cases {
958+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
959959
c.Res().Cookie(invalid)
960960
require.Empty(t, c.Res().Get(HeaderSetCookie))
961961
c.Response().Header.Reset()
962+
app.ReleaseCtx(c)
963+
}
964+
}
965+
966+
// go test -run Test_Ctx_Cookie_DefaultPath
967+
func Test_Ctx_Cookie_DefaultPath(t *testing.T) {
968+
t.Parallel()
969+
app := New()
970+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
971+
972+
ck := &Cookie{
973+
Name: "p",
974+
Value: "v",
975+
// Path intentionally empty to verify defaulting
976+
}
977+
978+
c.Res().Cookie(ck)
979+
require.Equal(t,
980+
"p=v; path=/; SameSite=Lax",
981+
c.Res().Get(HeaderSetCookie),
982+
)
983+
}
984+
985+
// go test -run Test_Ctx_Cookie_MaxAgeOnly
986+
func Test_Ctx_Cookie_MaxAgeOnly(t *testing.T) {
987+
t.Parallel()
988+
app := New()
989+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
990+
991+
ck := &Cookie{
992+
Name: "ttl",
993+
Value: "v",
994+
MaxAge: 3600,
962995
}
996+
c.Res().Cookie(ck)
997+
998+
require.Equal(t,
999+
"ttl=v; Max-Age=3600; path=/; SameSite=Lax",
1000+
c.Res().Get(HeaderSetCookie),
1001+
)
1002+
}
1003+
1004+
// go test -run Test_Ctx_Cookie_StrictPartitioned
1005+
func Test_Ctx_Cookie_StrictPartitioned(t *testing.T) {
1006+
t.Parallel()
1007+
app := New()
1008+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1009+
1010+
ck := &Cookie{
1011+
Name: "sp",
1012+
Value: "v",
1013+
Secure: true,
1014+
SameSite: CookieSameSiteStrictMode,
1015+
Partitioned: true,
1016+
}
1017+
c.Res().Cookie(ck)
1018+
1019+
require.Equal(t,
1020+
"sp=v; path=/; secure; SameSite=Strict; Partitioned",
1021+
c.Res().Get(HeaderSetCookie),
1022+
)
9631023
}
9641024

9651025
// go test -v -run=^$ -bench=Benchmark_Ctx_Cookie -benchmem -count=4

0 commit comments

Comments
 (0)