forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.go
161 lines (137 loc) · 4.6 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
package fasthttp
import (
"strings"
"testing"
"time"
)
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; domain=aaa.com; path=/foo/bar", "foo=bar; domain=aaa.com; path=/foo/bar")
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. Expected %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 лодлы", "%D1%84%D1%84%D1%84=12%20%D0%BB%D0%BE%D0%B4%D0%BB%D1%8B")
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. Expected %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. Expected %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 b;c&s s=aaa", "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc; s%20s=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. Expected %q for cookie %q", result[:len(prefix)], prefix, s)
}
result = result[len(prefix):]
if result != expectedS {
t.Fatalf("Unexpected result %q. Expected %q for cookie %q", result, expectedS, s)
}
}