forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.go
309 lines (268 loc) · 8.41 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package fasthttp
import (
"strings"
"testing"
"time"
)
func TestCookieValueWithEqualAndSpaceChars(t *testing.T) {
testCookieValueWithEqualAndSpaceChars(t, "sth1", "/", "MTQ2NjU5NTcwN3xfUVduVXk4aG9jSmZaNzNEb1dGa1VjekY1bG9vMmxSWlJBZUN2Q1ZtZVFNMTk2YU9YaWtCVmY1eDRWZXd3M3Q5RTJRZnZMbk5mWklSSFZJcVlXTDhiSFFHWWdpdFVLd1hwbXR2UUN4QlJ1N3BITFpkS3Y4PXzDvPNn6JVDBFB2wYVYPHdkdlZBm6n1_0QB3_GWwE40Tg ==")
testCookieValueWithEqualAndSpaceChars(t, "sth2", "/", "123")
testCookieValueWithEqualAndSpaceChars(t, "sth3", "/", "123 == 1")
}
func testCookieValueWithEqualAndSpaceChars(t *testing.T, expectedName, expectedPath, expectedValue string) {
var c Cookie
c.SetKey(expectedName)
c.SetPath(expectedPath)
c.SetValue(expectedValue)
s := c.String()
var c1 Cookie
if err := c1.Parse(s); err != nil {
t.Fatalf("unexpected error: %s", err)
}
name := c1.Key()
if string(name) != expectedName {
t.Fatalf("unexpected name %q. Expecting %q", name, expectedName)
}
path := c1.Path()
if string(path) != expectedPath {
t.Fatalf("unexpected path %q. Expecting %q", path, expectedPath)
}
value := c1.Value()
if string(value) != expectedValue {
t.Fatalf("unexpected value %q. Expecting %q", value, expectedValue)
}
}
func TestCookieSecureHttpOnly(t *testing.T) {
var c Cookie
if err := c.Parse("foo=bar; HttpOnly; secure"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !c.Secure() {
t.Fatalf("secure must be set")
}
if !c.HTTPOnly() {
t.Fatalf("HttpOnly must be set")
}
s := c.String()
if !strings.Contains(s, "; secure") {
t.Fatalf("missing secure flag in cookie %q", s)
}
if !strings.Contains(s, "; HttpOnly") {
t.Fatalf("missing HttpOnly flag in cookie %q", s)
}
}
func TestCookieSecure(t *testing.T) {
var c Cookie
if err := c.Parse("foo=bar; secure"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !c.Secure() {
t.Fatalf("secure must be set")
}
s := c.String()
if !strings.Contains(s, "; secure") {
t.Fatalf("missing secure flag in cookie %q", s)
}
if err := c.Parse("foo=bar"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if c.HTTPOnly() {
t.Fatalf("Unexpected secure flag set")
}
s = c.String()
if strings.Contains(s, "secure") {
t.Fatalf("unexpected secure flag in cookie %q", s)
}
}
func TestCookieMaxAge(t *testing.T) {
var c Cookie
maxAge := 100
if err := c.Parse("foo=bar; max-age=100"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if maxAge != c.MaxAge() {
t.Fatalf("max-age must be set")
}
s := c.String()
if !strings.Contains(s, "; max-age=100") {
t.Fatalf("missing max-age flag in cookie %q", s)
}
if err := c.Parse("foo=bar; expires=Tue, 10 Nov 2009 23:00:00 GMT; max-age=100;"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if maxAge != c.MaxAge() {
t.Fatalf("max-age ignored")
}
s = c.String()
if s != "foo=bar; max-age=100" {
t.Fatalf("missing max-age in cookie %q", s)
}
expires := time.Unix(100, 0)
c.SetExpire(expires)
s = c.String()
if s != "foo=bar; max-age=100" {
t.Fatalf("expires should be ignored due to max-age: %q", s)
}
c.SetMaxAge(0)
s = c.String()
if s != "foo=bar; expires=Thu, 01 Jan 1970 00:01:40 GMT" {
t.Fatalf("missing expires %q", s)
}
}
func TestCookieHttpOnly(t *testing.T) {
var c Cookie
if err := c.Parse("foo=bar; HttpOnly"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !c.HTTPOnly() {
t.Fatalf("HTTPOnly must be set")
}
s := c.String()
if !strings.Contains(s, "; HttpOnly") {
t.Fatalf("missing HttpOnly flag in cookie %q", s)
}
if err := c.Parse("foo=bar"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if c.HTTPOnly() {
t.Fatalf("Unexpected HTTPOnly flag set")
}
s = c.String()
if strings.Contains(s, "HttpOnly") {
t.Fatalf("unexpected HttpOnly flag in cookie %q", s)
}
}
func TestCookieAcquireReleaseSequential(t *testing.T) {
testCookieAcquireRelease(t)
}
func TestCookieAcquireReleaseConcurrent(t *testing.T) {
ch := make(chan struct{}, 10)
for i := 0; i < 10; i++ {
go func() {
testCookieAcquireRelease(t)
ch <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
}
func testCookieAcquireRelease(t *testing.T) {
c := AcquireCookie()
key := "foo"
c.SetKey(key)
value := "bar"
c.SetValue(value)
domain := "foo.bar.com"
c.SetDomain(domain)
path := "/foi/bar/aaa"
c.SetPath(path)
s := c.String()
c.Reset()
if err := c.Parse(s); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if string(c.Key()) != key {
t.Fatalf("unexpected cookie name %q. Expecting %q", c.Key(), key)
}
if string(c.Value()) != value {
t.Fatalf("unexpected cookie value %q. Expecting %q", c.Value(), value)
}
if string(c.Domain()) != domain {
t.Fatalf("unexpected domain %q. Expecting %q", c.Domain(), domain)
}
if string(c.Path()) != path {
t.Fatalf("unexpected path %q. Expecting %q", c.Path(), path)
}
ReleaseCookie(c)
}
func TestCookieParse(t *testing.T) {
testCookieParse(t, "foo", "foo")
testCookieParse(t, "foo=bar", "foo=bar")
testCookieParse(t, "foo=", "foo=")
testCookieParse(t, `foo="bar"`, "foo=bar")
testCookieParse(t, `"foo"=bar`, `"foo"=bar`)
testCookieParse(t, "foo=bar; Domain=aaa.com; PATH=/foo/bar", "foo=bar; domain=aaa.com; path=/foo/bar")
testCookieParse(t, "foo=bar; max-age= 101 ; expires= Tue, 10 Nov 2009 23:00:00 GMT", "foo=bar; max-age=101")
testCookieParse(t, " xxx = yyy ; path=/a/b;;;domain=foobar.com ; expires= Tue, 10 Nov 2009 23:00:00 GMT ; ;;",
"xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b")
}
func testCookieParse(t *testing.T, s, expectedS string) {
var c Cookie
if err := c.Parse(s); err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := string(c.Cookie())
if result != expectedS {
t.Fatalf("unexpected cookies %q. Expecting %q. Original %q", result, expectedS, s)
}
}
func TestCookieAppendBytes(t *testing.T) {
c := &Cookie{}
testCookieAppendBytes(t, c, "", "bar", "bar")
testCookieAppendBytes(t, c, "foo", "", "foo=")
testCookieAppendBytes(t, c, "ффф", "12 лодлы", "ффф=12 лодлы")
c.SetDomain("foobar.com")
testCookieAppendBytes(t, c, "a", "b", "a=b; domain=foobar.com")
c.SetPath("/a/b")
testCookieAppendBytes(t, c, "aa", "bb", "aa=bb; domain=foobar.com; path=/a/b")
c.SetExpire(CookieExpireDelete)
testCookieAppendBytes(t, c, "xxx", "yyy", "xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b")
}
func testCookieAppendBytes(t *testing.T, c *Cookie, key, value, expectedS string) {
c.SetKey(key)
c.SetValue(value)
result := string(c.AppendBytes(nil))
if result != expectedS {
t.Fatalf("Unexpected cookie %q. Expecting %q", result, expectedS)
}
}
func TestParseRequestCookies(t *testing.T) {
testParseRequestCookies(t, "", "")
testParseRequestCookies(t, "=", "")
testParseRequestCookies(t, "foo", "foo")
testParseRequestCookies(t, "=foo", "foo")
testParseRequestCookies(t, "bar=", "bar=")
testParseRequestCookies(t, "xxx=aa;bb=c; =d; ;;e=g", "xxx=aa; bb=c; d; e=g")
testParseRequestCookies(t, "a;b;c; d=1;d=2", "a; b; c; d=1; d=2")
testParseRequestCookies(t, " %D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc ;s%20s=aaa ", "%D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc; s%20s=aaa")
}
func testParseRequestCookies(t *testing.T, s, expectedS string) {
cookies := parseRequestCookies(nil, []byte(s))
ss := string(appendRequestCookieBytes(nil, cookies))
if ss != expectedS {
t.Fatalf("Unexpected cookies after parsing: %q. Expecting %q. String to parse %q", ss, expectedS, s)
}
}
func TestAppendRequestCookieBytes(t *testing.T) {
testAppendRequestCookieBytes(t, "=", "")
testAppendRequestCookieBytes(t, "foo=", "foo=")
testAppendRequestCookieBytes(t, "=bar", "bar")
testAppendRequestCookieBytes(t, "привет=a bc&s s=aaa", "привет=a bc; s s=aaa")
}
func testAppendRequestCookieBytes(t *testing.T, s, expectedS string) {
var cookies []argsKV
for _, ss := range strings.Split(s, "&") {
tmp := strings.SplitN(ss, "=", 2)
if len(tmp) != 2 {
t.Fatalf("Cannot find '=' in %q, part of %q", ss, s)
}
cookies = append(cookies, argsKV{
key: []byte(tmp[0]),
value: []byte(tmp[1]),
})
}
prefix := "foobar"
result := string(appendRequestCookieBytes([]byte(prefix), cookies))
if result[:len(prefix)] != prefix {
t.Fatalf("unexpected prefix %q. Expecting %q for cookie %q", result[:len(prefix)], prefix, s)
}
result = result[len(prefix):]
if result != expectedS {
t.Fatalf("Unexpected result %q. Expecting %q for cookie %q", result, expectedS, s)
}
}