forked from naoina/kocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
143 lines (128 loc) · 3.49 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
package kocha
import (
"fmt"
"reflect"
"strings"
"testing"
"testing/quick"
)
func Test_Constants(t *testing.T) {
actual := SessionExpiresKey
expected := "_kocha._sess._expires"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
func Test_SessionConfig_Validate(t *testing.T) {
newSessionConfig := func() *SessionConfig {
return &SessionConfig{
Name: "testname",
}
}
var config *SessionConfig
if err := config.Validate(); err != nil {
t.Errorf("Expect valid, but returned error")
}
config = newSessionConfig()
if err := config.Validate(); err != nil {
t.Errorf("Expect valid, but returned error: ", err)
}
config = newSessionConfig()
config.Name = ""
if err := config.Validate(); err == nil {
t.Errorf("Expect invalid, but no error returned")
}
config = nil
oldMiddlewares := appConfig.Middlewares
appConfig.Middlewares = append(appConfig.Middlewares, &SessionMiddleware{})
if err := config.Validate(); err == nil {
t.Errorf("Expect invalid, but no error returned")
}
config = newSessionConfig()
config.Store = nil
if err := config.Validate(); err == nil {
t.Errorf("Expect invalid, but no error returned")
}
store := &ValidateTestSessionStore{}
config.Store = store
if err := config.Validate(); err == nil {
t.Errorf("Expect invalid, but no error returned")
}
if !store.validated {
t.Errorf("Expect Validate() is called, but wasn't called")
}
appConfig.Middlewares = oldMiddlewares
}
type ValidateTestSessionStore struct{ validated bool }
func (s *ValidateTestSessionStore) Save(sess Session) string { return "" }
func (s *ValidateTestSessionStore) Load(key string) Session { return nil }
func (s *ValidateTestSessionStore) Validate() error {
s.validated = true
return fmt.Errorf("")
}
func Test_Session_Clear(t *testing.T) {
sess := make(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) {
oldAppConfig := appConfig
appConfig = newTestAppConfig()
defer func() {
appConfig = oldAppConfig
}()
if err := quick.Check(func(k, v string) bool {
expected := make(Session)
expected[k] = v
store := newTestSessionCookieStore()
r := store.Save(expected)
actual := store.Load(r)
return reflect.DeepEqual(actual, expected)
}, nil); err != nil {
t.Error(err)
}
func() {
defer func() {
if err := recover(); err == nil {
t.Error("panic doesn't occurs")
} else if _, ok := err.(ErrSession); !ok {
t.Error("Expect %T, but %T", ErrSession{}, err)
}
}()
store := newTestSessionCookieStore()
store.Load("invalid")
}()
}
func Test_SessionCookieStore_Validate(t *testing.T) {
// tests for validate the key size.
for _, keySize := range []int{16, 24, 32} {
store := &SessionCookieStore{
SecretKey: strings.Repeat("a", keySize),
SigningKey: "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 := &SessionCookieStore{
SecretKey: strings.Repeat("a", keySize),
SigningKey: "a",
}
if err := store.Validate(); err == nil {
t.Errorf("Expect key size %v is invalid, but doesn't returned error", keySize)
}
}
}