diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-30 17:01:41 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-30 17:01:43 -0600 |
| commit | a372feb94ba1dfe119f2b350b77302a243ab17f2 (patch) | |
| tree | b371003072df944feb2d6fe3a60c785e7bfb4f22 /pkg/web/cookie/option_test.go | |
| parent | 85de7a7ecc5260029cadf16af10acae2def9f528 (diff) | |
fix: adjust cookie expiration calculation
Diffstat (limited to 'pkg/web/cookie/option_test.go')
| -rw-r--r-- | pkg/web/cookie/option_test.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/pkg/web/cookie/option_test.go b/pkg/web/cookie/option_test.go new file mode 100644 index 0000000..97913ba --- /dev/null +++ b/pkg/web/cookie/option_test.go @@ -0,0 +1,53 @@ +package cookie + +import ( + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestOption(t *testing.T) { + t.Run("WithPath", func(t *testing.T) { + assert.Equal(t, "/blah", New("name", "value", WithPath("/blah")).Path) + }) + + t.Run("WithHttpOnly", func(t *testing.T) { + assert.False(t, New("x", "v", WithHttpOnly(false)).HttpOnly) + assert.True(t, New("x", "v", WithHttpOnly(true)).HttpOnly) + }) + + t.Run("WithSecure", func(t *testing.T) { + assert.False(t, New("x", "v", WithSecure(false)).Secure) + assert.True(t, New("x", "v", WithSecure(true)).Secure) + }) + + t.Run("WithDomain", func(t *testing.T) { + assert.Equal(t, "example.com", New("x", "v", WithDomain("example.com")).Domain) + }) + + t.Run("WithSameSite", func(t *testing.T) { + assert.Equal(t, http.SameSiteLaxMode, New("x", "v", WithSameSite(http.SameSiteLaxMode)).SameSite) + assert.Equal(t, http.SameSiteStrictMode, New("x", "v", WithSameSite(http.SameSiteStrictMode)).SameSite) + assert.Equal(t, http.SameSiteNoneMode, New("x", "v", WithSameSite(http.SameSiteNoneMode)).SameSite) + }) + + t.Run("WithExpiration", func(t *testing.T) { + now := time.Now() + + t.Run("with future time", func(t *testing.T) { + expires := now.Add(1 * time.Second) + cookie := New("x", "v", WithExpiration(expires)) + assert.Equal(t, expires, cookie.Expires) + assert.Equal(t, 1, cookie.MaxAge) + }) + + t.Run("with past time", func(t *testing.T) { + expires := now.Add(-1 * time.Second) + cookie := New("x", "v", WithExpiration(expires)) + assert.Equal(t, expires, cookie.Expires) + assert.Equal(t, -1, cookie.MaxAge) + }) + }) +} |
