summaryrefslogtreecommitdiff
path: root/pkg/web/cookie
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-30 17:01:41 -0600
committermo khan <mo@mokhan.ca>2025-04-30 17:01:43 -0600
commita372feb94ba1dfe119f2b350b77302a243ab17f2 (patch)
treeb371003072df944feb2d6fe3a60c785e7bfb4f22 /pkg/web/cookie
parent85de7a7ecc5260029cadf16af10acae2def9f528 (diff)
fix: adjust cookie expiration calculation
Diffstat (limited to 'pkg/web/cookie')
-rw-r--r--pkg/web/cookie/option.go3
-rw-r--r--pkg/web/cookie/option_test.go (renamed from pkg/web/cookie/test_option.go)9
2 files changed, 7 insertions, 5 deletions
diff --git a/pkg/web/cookie/option.go b/pkg/web/cookie/option.go
index 3f2cc93..58a2e93 100644
--- a/pkg/web/cookie/option.go
+++ b/pkg/web/cookie/option.go
@@ -50,7 +50,8 @@ func WithExpiration(expires time.Time) x.Option[*http.Cookie] {
if expires.Before(time.Now()) {
c.MaxAge = -1
} else {
- c.MaxAge = int(time.Until(expires).Seconds())
+ duration := time.Until(expires).Round(time.Second)
+ c.MaxAge = int(duration.Seconds())
}
})
}
diff --git a/pkg/web/cookie/test_option.go b/pkg/web/cookie/option_test.go
index caf1497..97913ba 100644
--- a/pkg/web/cookie/test_option.go
+++ b/pkg/web/cookie/option_test.go
@@ -35,16 +35,17 @@ func TestOption(t *testing.T) {
t.Run("WithExpiration", func(t *testing.T) {
now := time.Now()
- expires := now.Add(1 * time.Second)
t.Run("with future time", func(t *testing.T) {
- cookie := New("x", "v", WithExpiration(now.Add(1*time.Second)))
+ expires := now.Add(1 * time.Second)
+ cookie := New("x", "v", WithExpiration(expires))
assert.Equal(t, expires, cookie.Expires)
- assert.Equal(t, time.Until(expires).Seconds(), cookie.MaxAge)
+ assert.Equal(t, 1, cookie.MaxAge)
})
t.Run("with past time", func(t *testing.T) {
- cookie := New("x", "v", WithExpiration(now.Add(-1*time.Second)))
+ expires := now.Add(-1 * time.Second)
+ cookie := New("x", "v", WithExpiration(expires))
assert.Equal(t, expires, cookie.Expires)
assert.Equal(t, -1, cookie.MaxAge)
})