forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.go
210 lines (173 loc) · 5.05 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package httpexpect
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCookie_FailedChain(t *testing.T) {
check := func(value *Cookie, isNil bool) {
value.chain.assert(t, failure)
if isNil {
assert.Nil(t, value.Raw())
} else {
assert.NotNil(t, value.Raw())
}
value.Alias("foo")
value.Name().chain.assert(t, failure)
value.Value().chain.assert(t, failure)
value.Domain().chain.assert(t, failure)
value.Path().chain.assert(t, failure)
value.Expires().chain.assert(t, failure)
value.MaxAge().chain.assert(t, failure)
value.ContainsMaxAge()
value.NotContainsMaxAge()
}
t.Run("failed chain", func(t *testing.T) {
chain := newMockChain(t, flagFailed)
value := newCookie(chain, &http.Cookie{})
check(value, false)
})
t.Run("nil value", func(t *testing.T) {
chain := newMockChain(t)
value := newCookie(chain, nil)
check(value, true)
})
t.Run("failed chain, nil value", func(t *testing.T) {
chain := newMockChain(t, flagFailed)
value := newCookie(chain, nil)
check(value, true)
})
}
func TestCookie_Constructors(t *testing.T) {
cookie := &http.Cookie{
Name: "Test",
Value: "Test_val",
Domain: "example.com",
Path: "/path",
Expires: time.Unix(1234, 0),
MaxAge: 123,
}
t.Run("reporter", func(t *testing.T) {
reporter := newMockReporter(t)
value := NewCookie(reporter, cookie)
value.Name().IsEqual("Test")
value.Value().IsEqual("Test_val")
value.Domain().IsEqual("example.com")
value.Expires().IsEqual(time.Unix(1234, 0))
value.MaxAge().IsEqual(123 * time.Second)
value.chain.assert(t, success)
})
t.Run("config", func(t *testing.T) {
reporter := newMockReporter(t)
value := NewCookieC(Config{
Reporter: reporter,
}, cookie)
value.Name().IsEqual("Test")
value.Value().IsEqual("Test_val")
value.Domain().IsEqual("example.com")
value.Expires().IsEqual(time.Unix(1234, 0))
value.MaxAge().IsEqual(123 * time.Second)
value.chain.assert(t, success)
})
t.Run("chain", func(t *testing.T) {
chain := newMockChain(t)
value := newCookie(chain, cookie)
assert.NotSame(t, value.chain, &chain)
assert.Equal(t, value.chain.context.Path, chain.context.Path)
})
}
func TestCookie_Raw(t *testing.T) {
reporter := newMockReporter(t)
data := http.Cookie{}
value := NewCookie(reporter, &data)
assert.Same(t, &data, value.Raw())
value.chain.assert(t, success)
}
func TestCookie_Alias(t *testing.T) {
reporter := newMockReporter(t)
value := NewCookie(reporter, &http.Cookie{
MaxAge: 0,
})
assert.Equal(t, []string{"Cookie()"}, value.chain.context.Path)
assert.Equal(t, []string{"Cookie()"}, value.chain.context.AliasedPath)
value.Alias("foo")
assert.Equal(t, []string{"Cookie()"}, value.chain.context.Path)
assert.Equal(t, []string{"foo"}, value.chain.context.AliasedPath)
childValue := value.Domain()
assert.Equal(t, []string{"Cookie()", "Domain()"}, childValue.chain.context.Path)
assert.Equal(t, []string{"foo", "Domain()"}, childValue.chain.context.AliasedPath)
}
func TestCookie_Getters(t *testing.T) {
reporter := newMockReporter(t)
value := NewCookie(reporter, &http.Cookie{
Name: "name",
Value: "value",
Domain: "example.com",
Path: "/path",
Expires: time.Unix(1234, 0),
MaxAge: 123,
})
value.chain.assert(t, success)
value.Name().chain.assert(t, success)
value.Value().chain.assert(t, success)
value.Domain().chain.assert(t, success)
value.Path().chain.assert(t, success)
value.Expires().chain.assert(t, success)
value.MaxAge().chain.assert(t, success)
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, 123*time.Second, value.MaxAge().Raw())
value.chain.assert(t, success)
}
func TestCookie_MaxAge(t *testing.T) {
cases := []struct {
name string
maxAge int
wantContainsMaxAge chainResult
wantDuration time.Duration
}{
{
name: "unset",
maxAge: 0,
wantContainsMaxAge: failure,
},
{
name: "zero",
maxAge: -1,
wantContainsMaxAge: success,
wantDuration: time.Duration(0),
},
{
name: "non-zero",
maxAge: 3,
wantContainsMaxAge: success,
wantDuration: time.Duration(3) * time.Second,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
data := &http.Cookie{
MaxAge: tc.maxAge,
}
NewCookie(reporter, data).ContainsMaxAge().
chain.assert(t, tc.wantContainsMaxAge)
NewCookie(reporter, data).NotContainsMaxAge().
chain.assert(t, !tc.wantContainsMaxAge)
if tc.wantContainsMaxAge {
require.NotNil(t,
NewCookie(reporter, data).MaxAge().value)
require.Equal(t, tc.wantDuration,
*NewCookie(reporter, data).MaxAge().value)
} else {
require.Nil(t,
NewCookie(reporter, data).MaxAge().value)
}
})
}
}