diff options
| author | mo khan <mo@mokhan.ca> | 2025-05-07 09:06:48 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-05-07 09:06:48 -0700 |
| commit | ef050c428a0a893607314a4d5d8d441e445e630a (patch) | |
| tree | e6c7e651f1fa75225053bb3c0f28c29ff15f4306 /pkg/web | |
| parent | 16641c74b7247f5b5c059f5726fbc724fe3858e4 (diff) | |
refactor: move cookie to web package
Diffstat (limited to 'pkg/web')
| -rw-r--r-- | pkg/web/cookie.go (renamed from pkg/web/cookie/new.go) | 13 | ||||
| -rw-r--r-- | pkg/web/cookie/expire.go | 17 | ||||
| -rw-r--r-- | pkg/web/cookie/new_test.go | 19 | ||||
| -rw-r--r-- | pkg/web/cookie/reset.go | 19 | ||||
| -rw-r--r-- | pkg/web/cookie/reset_test.go | 25 | ||||
| -rw-r--r-- | pkg/web/cookie_test.go | 41 |
6 files changed, 52 insertions, 82 deletions
diff --git a/pkg/web/cookie/new.go b/pkg/web/cookie.go index be0241d..d4b1555 100644 --- a/pkg/web/cookie/new.go +++ b/pkg/web/cookie.go @@ -1,4 +1,4 @@ -package cookie +package web import ( "net/http" @@ -8,7 +8,7 @@ import ( "github.com/xlgmokha/x/pkg/x" ) -func New(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { +func NewCookie(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { options = x.Prepend[x.Option[*http.Cookie]]( options, cookie.WithName(name), @@ -22,3 +22,12 @@ func New(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { return x.New[*http.Cookie](options...) } + +func ExpireCookie(w http.ResponseWriter, name string) { + cookie.Expire(w, name, + cookie.WithPath("/"), + cookie.WithDomain(env.Fetch("HOST", "localhost")), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + ) +} diff --git a/pkg/web/cookie/expire.go b/pkg/web/cookie/expire.go deleted file mode 100644 index 7d90274..0000000 --- a/pkg/web/cookie/expire.go +++ /dev/null @@ -1,17 +0,0 @@ -package cookie - -import ( - "net/http" - - "github.com/xlgmokha/x/pkg/cookie" - "github.com/xlgmokha/x/pkg/env" -) - -func Expire(w http.ResponseWriter, name string) { - cookie.Expire(w, name, - cookie.WithPath("/"), - cookie.WithDomain(env.Fetch("HOST", "localhost")), - cookie.WithHttpOnly(true), - cookie.WithSecure(true), - ) -} diff --git a/pkg/web/cookie/new_test.go b/pkg/web/cookie/new_test.go deleted file mode 100644 index 5c9e92c..0000000 --- a/pkg/web/cookie/new_test.go +++ /dev/null @@ -1,19 +0,0 @@ -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.SameSiteDefaultMode, cookie.SameSite) - }) -} diff --git a/pkg/web/cookie/reset.go b/pkg/web/cookie/reset.go deleted file mode 100644 index 39625e6..0000000 --- a/pkg/web/cookie/reset.go +++ /dev/null @@ -1,19 +0,0 @@ -package cookie - -import ( - "net/http" - - "github.com/xlgmokha/x/pkg/cookie" - "github.com/xlgmokha/x/pkg/env" -) - -func Reset(name string) *http.Cookie { - return cookie.Reset( - name, - cookie.WithPath("/"), - cookie.WithHttpOnly(true), - cookie.WithSecure(true), - cookie.WithSameSite(http.SameSiteDefaultMode), - cookie.WithDomain(env.Fetch("HOST", "localhost")), - ) -} diff --git a/pkg/web/cookie/reset_test.go b/pkg/web/cookie/reset_test.go deleted file mode 100644 index 6291eac..0000000 --- a/pkg/web/cookie/reset_test.go +++ /dev/null @@ -1,25 +0,0 @@ -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.go b/pkg/web/cookie_test.go new file mode 100644 index 0000000..60cf7cf --- /dev/null +++ b/pkg/web/cookie_test.go @@ -0,0 +1,41 @@ +package web + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/xlgmokha/x/pkg/env" +) + +func TestNewCookie(t *testing.T) { + env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { + cookie := NewCookie("name", "value") + assert.Equal(t, "sparkle.example.com", cookie.Domain) + assert.True(t, cookie.HttpOnly) + assert.True(t, cookie.Secure) + assert.Equal(t, http.SameSiteDefaultMode, cookie.SameSite) + }) +} + +func TestExpireCookie(t *testing.T) { + env.With(env.Vars{"HOST": "sparkle.example.com"}, func() { + w := httptest.NewRecorder() + + ExpireCookie(w, "example") + + result, err := http.ParseSetCookie(w.Header().Get("Set-Cookie")) + require.NoError(t, err) + + assert.Empty(t, result.Value) + assert.Equal(t, "sparkle.example.com", result.Domain) + assert.Equal(t, -1, result.MaxAge) + assert.Equal(t, time.Unix(0, 0).Unix(), result.Expires.Unix()) + assert.True(t, result.HttpOnly) + assert.True(t, result.Secure) + assert.Zero(t, result.SameSite) + }) +} |
