-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookieSession_test.go
180 lines (171 loc) · 3.34 KB
/
cookieSession_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
package gosecuresessions
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
cok "github.com/GolangToolKits/go-secure-cookies"
)
func TestCookieSession_Set(t *testing.T) {
type fields struct {
id string
name string
values map[any]any
cookies cok.Cookies
path string
domain string
maxAge int
}
type args struct {
key string
value any
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{},
args: args{
key: "test1",
value: "a test 1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
//cookies: tt.fields.cookies,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
s.Set(tt.args.key, tt.args.value)
if tt.name == "test 1" && s.Get("test1") != "a test 1" {
t.Fail()
}
})
}
}
func TestCookieSession_Get(t *testing.T) {
type fields struct {
id string
name string
values map[any]any
cookies cok.Cookies
path string
domain string
maxAge int
}
type args struct {
key string
}
tests := []struct {
name string
fields fields
args args
want any
}{
// TODO: Add test cases.
{
name: "test 1",
args: args{
key: "test1",
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
//cookies: tt.fields.cookies,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
if got := s.Get(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CookieSession.Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestCookieSession_Save(t *testing.T) {
var cf ConfigOptions
cf.MaxAge = 3600
m, err := NewSessionManager("dsdfs6dfs61dssdfsdfdsdsfsdsdllsd", cf)
if err != nil {
fmt.Println(err)
}
v := make(map[any]any)
v["test1"] = "test11111"
v["test2"] = "test22222"
tw := httptest.NewRecorder()
tr, _ := http.NewRequest("POST", "/test/test1", nil)
type fields struct {
id string
name string
values map[any]any
manager *Manager
path string
domain string
maxAge int
}
type args struct {
w http.ResponseWriter
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
name: "test_session",
values: v,
manager: m.(*Manager),
path: "/",
maxAge: 3600,
},
args: args{
w: tw,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
manager: tt.fields.manager,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
if err := s.Save(tt.args.w); (err != nil) != tt.wantErr {
t.Errorf("CookieSession.Save() error = %v, wantErr %v", err, tt.wantErr)
}
cook := tw.Result().Cookies()
if len(cook) > 0 {
tr.AddCookie(cook[0])
}
ses := m.NewSession(tr, "test_session")
if ses.Get("test2") != "test22222" {
t.Fail()
}
})
}
}