-
Notifications
You must be signed in to change notification settings - Fork 10
/
session_test.go
221 lines (202 loc) · 5.98 KB
/
session_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
package kocha_test
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"testing"
"testing/quick"
"github.com/naoina/kocha"
)
func TestSession(t *testing.T) {
sess := make(kocha.Session)
key := "test_key"
var actual interface{} = sess.Get(key)
var expected interface{} = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Get(%#v) => %#v; want %#v`, key, actual, expected)
}
actual = len(sess)
expected = 0
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`len(Session) => %#v; want %#v`, actual, expected)
}
value := "test_value"
sess.Set(key, value)
actual = sess.Get(key)
expected = value
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); Session.Get(%#v) => %#v; want %#v`, key, value, key, actual, expected)
}
actual = len(sess)
expected = 1
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); len(Session) => %#v; want %#v`, key, value, actual, expected)
}
key2 := "test_key2"
value2 := "test_value2"
sess.Set(key2, value2)
actual = sess.Get(key2)
expected = value2
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); Session.Get(%#v) => %#v; want %#v`, key2, value2, key2, actual, expected)
}
actual = len(sess)
expected = 2
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); len(Session) => %#v; want %#v`, key2, value2, actual, expected)
}
value3 := "test_value3"
sess.Set(key, value3)
actual = sess.Get(key)
expected = value3
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); Session.Get(%#v) => %#v; want %#v`, key, value3, key, actual, expected)
}
actual = len(sess)
expected = 2
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); len(Session) => %#v; want %#v`, key, value3, actual, expected)
}
sess.Clear()
for _, key := range []string{key, key2} {
actual = sess.Get(key)
expected = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Clear(); Session.Get(%#v) => %#v; want %#v`, key, actual, expected)
}
}
actual = len(sess)
expected = 0
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Clear(); len(Session) => %#v; want %#v`, actual, expected)
}
}
func TestSession_Get(t *testing.T) {
sess := make(kocha.Session)
key := "test_key"
var actual interface{} = sess.Get(key)
var expected interface{} = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Get(%#v) => %#v; want %#v`, key, actual, expected)
}
value := "test_value"
sess[key] = value
actual = sess.Get(key)
expected = value
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Get(%#v) => %#v; want %#v`, key, actual, expected)
}
delete(sess, key)
actual = sess.Get(key)
expected = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Get(%#v) => %#v; want %#v`, key, actual, expected)
}
}
func TestSession_Set(t *testing.T) {
sess := make(kocha.Session)
key := "test_key"
var actual interface{} = sess[key]
var expected interface{} = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session[%#v] => %#v; want %#v`, key, actual, expected)
}
value := "test_value"
sess.Set(key, value)
actual = sess[key]
expected = value
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); Session[%#v] => %#v; want %#v`, key, value, key, actual, expected)
}
value2 := "test_value2"
sess.Set(key, value2)
actual = sess[key]
expected = value2
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Set(%#v, %#v); Session[%#v] => %#v; want %#v`, key, value2, key, actual, expected)
}
}
func TestSession_Del(t *testing.T) {
sess := make(kocha.Session)
key := "test_key"
value := "test_value"
sess[key] = value
var actual interface{} = sess[key]
var expected interface{} = value
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session[%#v] => %#v; want %#v`, key, actual, expected)
}
sess.Del(key)
actual = sess[key]
expected = ""
if !reflect.DeepEqual(actual, expected) {
t.Errorf(`Session.Del(%#v); Session[%#v] => %#v; want %#v`, key, key, actual, expected)
}
}
func Test_Session_Clear(t *testing.T) {
sess := make(kocha.Session)
sess["hoge"] = "foo"
sess["bar"] = "baz"
actual := len(sess)
expected := 2
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
sess.Clear()
actual = len(sess)
expected = 0
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
func Test_SessionCookieStore(t *testing.T) {
if err := quick.Check(func(k, v string) bool {
expected := make(kocha.Session)
expected[k] = v
store := kocha.NewTestSessionCookieStore()
r, err := store.Save(expected)
if err != nil {
t.Fatal(err)
}
actual, err := store.Load(r)
if err != nil {
t.Fatal(err)
}
return reflect.DeepEqual(actual, expected)
}, nil); err != nil {
t.Error(err)
}
func() {
store := kocha.NewTestSessionCookieStore()
key := "invalid"
_, err := store.Load(key)
actual := err
expect := fmt.Errorf("kocha: session cookie value too short")
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`SessionCookieStore.Load(%#v) => _, %#v; want %#v`, key, actual, expect)
}
}()
}
func Test_SessionCookieStore_Validate(t *testing.T) {
// tests for validate the key size.
for _, keySize := range []int{16, 24, 32} {
store := &kocha.SessionCookieStore{
SecretKey: base64.StdEncoding.EncodeToString([]byte(strings.Repeat("a", keySize))),
SigningKey: base64.StdEncoding.EncodeToString([]byte("a")),
}
if err := store.Validate(); err != nil {
t.Errorf("Expect key size %v is valid, but returned error: %v", keySize, err)
}
}
// boundary tests
for _, keySize := range []int{15, 17, 23, 25, 31, 33} {
store := &kocha.SessionCookieStore{
SecretKey: base64.StdEncoding.EncodeToString([]byte(strings.Repeat("a", keySize))),
SigningKey: base64.StdEncoding.EncodeToString([]byte("a")),
}
if err := store.Validate(); err == nil {
t.Errorf("Expect key size %v is invalid, but doesn't returned error", keySize)
}
}
}