summaryrefslogtreecommitdiff
path: root/pkg/web/cookie/cookie_test.go
blob: 2451a4f07c4e4fc301cbc9ae3a93a8f869f07873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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) {
		t.Run("returns an expired cookie", func(t *testing.T) {
			result := Reset("example")

			assert.Equal(t, -1, result.MaxAge)
			assert.Equal(t, time.Unix(0, 0), result.Expires)
			assert.Empty(t, result.Value)
		})
	})
}