-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.go
104 lines (78 loc) · 2.37 KB
/
cookie_test.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package httpexpect
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCookieFailed(t *testing.T) {
chain := makeChain(newMockReporter(t))
chain.fail("fail")
value := &Cookie{chain, nil}
assert.True(t, value.Raw() == nil)
assert.True(t, value.Name() != nil)
assert.True(t, value.Value() != nil)
assert.True(t, value.Domain() != nil)
assert.True(t, value.Path() != nil)
assert.True(t, value.Expires() != nil)
assert.True(t, value.MaxAge() != nil)
}
func TestCookieGetters(t *testing.T) {
reporter := newMockReporter(t)
NewCookie(reporter, nil).chain.assertFailed(t)
value := NewCookie(reporter, &http.Cookie{
Name: "name",
Value: "value",
Domain: "example.com",
Path: "/path",
Expires: time.Unix(1234, 0),
MaxAge: 123,
})
value.chain.assertOK(t)
value.Name().chain.assertOK(t)
value.Value().chain.assertOK(t)
value.Domain().chain.assertOK(t)
value.Path().chain.assertOK(t)
value.Expires().chain.assertOK(t)
value.MaxAge().chain.assertOK(t)
assert.Equal(t, "name", value.Name().Raw())
assert.Equal(t, "value", value.Value().Raw())
assert.Equal(t, "example.com", value.Domain().Raw())
assert.Equal(t, "/path", value.Path().Raw())
assert.True(t, time.Unix(1234, 0).Equal(value.Expires().Raw()))
assert.Equal(t, time.Duration(123*time.Second), value.MaxAge().Raw())
value.chain.assertOK(t)
}
func TestCookieMaxAge(t *testing.T) {
reporter := newMockReporter(t)
t.Run("unset", func(t *testing.T) {
value := NewCookie(reporter, &http.Cookie{
MaxAge: 0,
})
value.chain.assertOK(t)
require.Nil(t, value.MaxAge().value)
value.MaxAge().NotSet().chain.assertOK(t)
value.MaxAge().IsSet().chain.assertFailed(t)
})
t.Run("zero", func(t *testing.T) {
value := NewCookie(reporter, &http.Cookie{
MaxAge: -1,
})
value.chain.assertOK(t)
require.NotNil(t, value.MaxAge().value)
require.Equal(t, time.Duration(0), *value.MaxAge().value)
value.MaxAge().IsSet().chain.assertOK(t)
value.MaxAge().Equal(0).chain.assertOK(t)
})
t.Run("non-zero", func(t *testing.T) {
value := NewCookie(reporter, &http.Cookie{
MaxAge: 3,
})
value.chain.assertOK(t)
require.NotNil(t, value.MaxAge().value)
require.Equal(t, 3*time.Second, *value.MaxAge().value)
value.MaxAge().IsSet().chain.assertOK(t)
value.MaxAge().Equal(3 * time.Second).chain.assertOK(t)
})
}