From 0a3bbc641efda807af3e54e7f37b562def35e729 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 30 Apr 2025 15:20:25 -0600 Subject: test: add test for each cookie option --- pkg/web/cookie/cookie_test.go | 39 ------------------------------ pkg/web/cookie/expire.go | 7 ++++++ pkg/web/cookie/new.go | 49 ------------------------------------- pkg/web/cookie/new_test.go | 19 +++++++++++++++ pkg/web/cookie/option.go | 56 +++++++++++++++++++++++++++++++++++++++++++ pkg/web/cookie/reset.go | 4 ---- pkg/web/cookie/reset_test.go | 25 +++++++++++++++++++ pkg/web/cookie/test_option.go | 52 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 159 insertions(+), 92 deletions(-) delete mode 100644 pkg/web/cookie/cookie_test.go create mode 100644 pkg/web/cookie/expire.go create mode 100644 pkg/web/cookie/new_test.go create mode 100644 pkg/web/cookie/option.go create mode 100644 pkg/web/cookie/reset_test.go create mode 100644 pkg/web/cookie/test_option.go (limited to 'pkg') diff --git a/pkg/web/cookie/cookie_test.go b/pkg/web/cookie/cookie_test.go deleted file mode 100644 index 798d13d..0000000 --- a/pkg/web/cookie/cookie_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package cookie - -import ( - "net/http" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/xlgmokha/x/pkg/env" -) - -func TestCookie(t *testing.T) { - t.Run("New", func(t *testing.T) { - t.Run("returns a cookie pinned to the HOST", func(t *testing.T) { - env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { - cookie := New("name", "value") - assert.Equal(t, "sparkle.example.com", cookie.Domain) - assert.True(t, cookie.HttpOnly) - assert.True(t, cookie.Secure) - assert.Equal(t, http.SameSiteStrictMode, cookie.SameSite) - }) - }) - }) - - t.Run("Reset", func(t *testing.T) { - env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { - result := Reset("example") - - assert.Equal(t, -1, result.MaxAge) - assert.Equal(t, time.Unix(0, 0), result.Expires) - assert.Empty(t, result.Value) - assert.Equal(t, time.Unix(0, 0), result.Expires) - assert.True(t, result.HttpOnly) - assert.True(t, result.Secure) - assert.Equal(t, http.SameSiteDefaultMode, result.SameSite) - assert.Equal(t, "sparkle.example.com", result.Domain) - }) - }) -} diff --git a/pkg/web/cookie/expire.go b/pkg/web/cookie/expire.go new file mode 100644 index 0000000..b44fc38 --- /dev/null +++ b/pkg/web/cookie/expire.go @@ -0,0 +1,7 @@ +package cookie + +import "net/http" + +func Expire(w http.ResponseWriter, name string) { + http.SetCookie(w, Reset(name)) +} diff --git a/pkg/web/cookie/new.go b/pkg/web/cookie/new.go index b809b4e..d762f4f 100644 --- a/pkg/web/cookie/new.go +++ b/pkg/web/cookie/new.go @@ -2,7 +2,6 @@ package cookie import ( "net/http" - "time" "github.com/xlgmokha/x/pkg/env" "github.com/xlgmokha/x/pkg/x" @@ -23,51 +22,3 @@ func New(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { ) return x.New[*http.Cookie](options...) } - -func With(with func(*http.Cookie)) x.Option[*http.Cookie] { - return func(c *http.Cookie) *http.Cookie { - with(c) - return c - } -} - -func WithPath(value string) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Path = value - }) -} - -func WithHttpOnly(value bool) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.HttpOnly = value - }) -} - -func WithSecure(value bool) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Secure = value - }) -} - -func WithDomain(value string) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Domain = value - }) -} - -func WithSameSite(value http.SameSite) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.SameSite = value - }) -} - -func WithExpiration(expires time.Time) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Expires = expires - if expires.Before(time.Now()) { - c.MaxAge = -1 - } else { - c.MaxAge = int(time.Until(expires).Seconds()) - } - }) -} diff --git a/pkg/web/cookie/new_test.go b/pkg/web/cookie/new_test.go new file mode 100644 index 0000000..84fac25 --- /dev/null +++ b/pkg/web/cookie/new_test.go @@ -0,0 +1,19 @@ +package cookie + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/xlgmokha/x/pkg/env" +) + +func TestNew(t *testing.T) { + env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { + cookie := New("name", "value") + assert.Equal(t, "sparkle.example.com", cookie.Domain) + assert.True(t, cookie.HttpOnly) + assert.True(t, cookie.Secure) + assert.Equal(t, http.SameSiteStrictMode, cookie.SameSite) + }) +} diff --git a/pkg/web/cookie/option.go b/pkg/web/cookie/option.go new file mode 100644 index 0000000..3f2cc93 --- /dev/null +++ b/pkg/web/cookie/option.go @@ -0,0 +1,56 @@ +package cookie + +import ( + "net/http" + "time" + + "github.com/xlgmokha/x/pkg/x" +) + +func With(with func(*http.Cookie)) x.Option[*http.Cookie] { + return func(c *http.Cookie) *http.Cookie { + with(c) + return c + } +} + +func WithPath(value string) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.Path = value + }) +} + +func WithHttpOnly(value bool) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.HttpOnly = value + }) +} + +func WithSecure(value bool) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.Secure = value + }) +} + +func WithDomain(value string) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.Domain = value + }) +} + +func WithSameSite(value http.SameSite) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.SameSite = value + }) +} + +func WithExpiration(expires time.Time) x.Option[*http.Cookie] { + return With(func(c *http.Cookie) { + c.Expires = expires + if expires.Before(time.Now()) { + c.MaxAge = -1 + } else { + c.MaxAge = int(time.Until(expires).Seconds()) + } + }) +} diff --git a/pkg/web/cookie/reset.go b/pkg/web/cookie/reset.go index cfb1830..167cdb6 100644 --- a/pkg/web/cookie/reset.go +++ b/pkg/web/cookie/reset.go @@ -19,7 +19,3 @@ func Reset(name string) *http.Cookie { WithDomain(env.Fetch("HOST", "localhost")), ) } - -func Expire(w http.ResponseWriter, name string) { - http.SetCookie(w, Reset(name)) -} diff --git a/pkg/web/cookie/reset_test.go b/pkg/web/cookie/reset_test.go new file mode 100644 index 0000000..6291eac --- /dev/null +++ b/pkg/web/cookie/reset_test.go @@ -0,0 +1,25 @@ +package cookie + +import ( + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/xlgmokha/x/pkg/env" +) + +func TestReset(t *testing.T) { + env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { + result := Reset("example") + + assert.Equal(t, -1, result.MaxAge) + assert.Equal(t, time.Unix(0, 0), result.Expires) + assert.Empty(t, result.Value) + assert.Equal(t, time.Unix(0, 0), result.Expires) + assert.True(t, result.HttpOnly) + assert.True(t, result.Secure) + assert.Equal(t, http.SameSiteDefaultMode, result.SameSite) + assert.Equal(t, "sparkle.example.com", result.Domain) + }) +} diff --git a/pkg/web/cookie/test_option.go b/pkg/web/cookie/test_option.go new file mode 100644 index 0000000..caf1497 --- /dev/null +++ b/pkg/web/cookie/test_option.go @@ -0,0 +1,52 @@ +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() + 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))) + assert.Equal(t, expires, cookie.Expires) + assert.Equal(t, time.Until(expires).Seconds(), cookie.MaxAge) + }) + + t.Run("with past time", func(t *testing.T) { + cookie := New("x", "v", WithExpiration(now.Add(-1*time.Second))) + assert.Equal(t, expires, cookie.Expires) + assert.Equal(t, -1, cookie.MaxAge) + }) + }) +} -- cgit v1.2.3