-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcookie_test.go
119 lines (90 loc) · 2.89 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
package session
import (
"testing"
"time"
"github.com/valyala/fasthttp"
)
func Test_newCookie(t *testing.T) {
cookie := newCookie()
if cookie == nil {
t.Error("newCookie() return nil")
}
}
func TestCookie_get(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
cookie := newCookie()
key := "key"
value := "value"
ctx.Request.Header.SetCookie(key, value)
if v := cookie.get(ctx, key); string(v) != value {
t.Errorf("cookie.get() == %s, want %s", v, value)
}
}
func TestCookie_set(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
cookie := newCookie()
key := "key"
value := []byte("value")
path := "/"
domain := "domain"
expiration := 10 * time.Second
secure := true
samesite := fasthttp.CookieSameSiteLaxMode
now := time.Now()
cookie.set(ctx, key, value, domain, expiration, secure, samesite)
resultCookie := new(fasthttp.Cookie)
resultCookie.SetKey(key)
ctx.Response.Header.Cookie(resultCookie)
if string(resultCookie.Path()) != path {
t.Errorf("cookie.set() Path == %s, want %s", resultCookie.Path(), path)
}
if !resultCookie.HTTPOnly() {
t.Errorf("cookie.set() HTTPOnly == %v, want %v", false, true)
}
if string(resultCookie.Domain()) != domain {
t.Errorf("cookie.set() Domain == %s, want %s", resultCookie.Domain(), domain)
}
if string(resultCookie.Value()) != string(value) {
t.Errorf("cookie.set() Value == %s, want %s", resultCookie.Value(), value)
}
if resultCookie.Expire().Unix() != now.Add(expiration).Unix() {
t.Errorf("cookie.set() Expire == %v, want %v", resultCookie.Expire(), expiration)
}
if resultCookie.Secure() != secure {
t.Errorf("cookie.set() Secure == %v, want %v", resultCookie.Secure(), secure)
}
if resultCookie.SameSite() != fasthttp.CookieSameSiteLaxMode {
t.Errorf("cookie.set() SameSite == %v, want %v", resultCookie.SameSite(), samesite)
}
if v := ctx.Request.Header.Cookie(key); string(v) != string(value) {
t.Errorf("cookie.set() request value == %s, want %s", v, value)
}
}
func TestCookie_delete(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
cookie := newCookie()
key := "key"
value := []byte("")
path := "/"
expiration := -1 * time.Minute
now := time.Now()
cookie.delete(ctx, key)
resultCookie := new(fasthttp.Cookie)
resultCookie.SetKey(key)
ctx.Response.Header.Cookie(resultCookie)
if string(resultCookie.Path()) != path {
t.Errorf("cookie.set() Path == %s, want %s", resultCookie.Path(), path)
}
if !resultCookie.HTTPOnly() {
t.Errorf("cookie.set() HTTPOnly == %v, want %v", false, true)
}
if string(resultCookie.Value()) != string(value) {
t.Errorf("cookie.set() Value == %s, want %s", resultCookie.Value(), value)
}
if resultCookie.Expire().Unix() != now.Add(expiration).Unix() {
t.Errorf("cookie.set() Expire == %v, want %v", resultCookie.Expire(), expiration)
}
if v := ctx.Request.Header.Cookie(key); string(v) != string(value) {
t.Errorf("cookie.set() request value == %s, want %s", v, value)
}
}