-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsessions_test.go
186 lines (152 loc) · 4.25 KB
/
sessions_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
package sessions
import (
"github.com/go-martini/martini"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test_Sessions(t *testing.T) {
m := martini.Classic()
store := NewCookieStore([]byte("secret123"))
m.Use(Sessions("my_session", store))
m.Get("/testsession", func(session Session) string {
session.Set("hello", "world")
return "OK"
})
m.Get("/show", func(session Session) string {
if session.Get("hello") != "world" {
t.Error("Session writing failed")
}
return "OK"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
m.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
m.ServeHTTP(res2, req2)
}
func Test_SessionsDeleteValue(t *testing.T) {
m := martini.Classic()
store := NewCookieStore([]byte("secret123"))
m.Use(Sessions("my_session", store))
m.Get("/testsession", func(session Session) string {
session.Set("hello", "world")
session.Delete("hello")
return "OK"
})
m.Get("/show", func(session Session) string {
if session.Get("hello") == "world" {
t.Error("Session value deleting failed")
}
return "OK"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
m.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
m.ServeHTTP(res2, req2)
}
func Test_Options(t *testing.T) {
m := martini.Classic()
store := NewCookieStore([]byte("secret123"))
store.Options(Options{
Domain: "martini.codegangsta.io",
})
m.Use(Sessions("my_session", store))
m.Get("/", func(session Session) string {
session.Set("hello", "world")
session.Options(Options{
Path: "/foo/bar/bat",
})
return "OK"
})
m.Get("/foo", func(session Session) string {
session.Set("hello", "world")
return "OK"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
m.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/foo", nil)
m.ServeHTTP(res2, req2)
s := strings.Split(res.Header().Get("Set-Cookie"), ";")
if s[1] != " Path=/foo/bar/bat" {
t.Error("Error writing path with options:", s[1])
}
s = strings.Split(res2.Header().Get("Set-Cookie"), ";")
if s[1] != " Domain=martini.codegangsta.io" {
t.Error("Error writing domain with options:", s[1])
}
}
func Test_Flashes(t *testing.T) {
m := martini.Classic()
store := NewCookieStore([]byte("secret123"))
m.Use(Sessions("my_session", store))
m.Get("/set", func(session Session) string {
session.AddFlash("hello world")
return "OK"
})
m.Get("/show", func(session Session) string {
l := len(session.Flashes())
if l != 1 {
t.Error("Flashes count does not equal 1. Equals ", l)
}
return "OK"
})
m.Get("/showagain", func(session Session) string {
l := len(session.Flashes())
if l != 0 {
t.Error("flashes count is not 0 after reading. Equals ", l)
}
return "OK"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/set", nil)
m.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
m.ServeHTTP(res2, req2)
res3 := httptest.NewRecorder()
req3, _ := http.NewRequest("GET", "/showagain", nil)
req3.Header.Set("Cookie", res2.Header().Get("Set-Cookie"))
m.ServeHTTP(res3, req3)
}
func Test_SessionsClear(t *testing.T) {
m := martini.Classic()
data := map[string]string{
"hello": "world",
"foo": "bar",
"apples": "oranges",
}
store := NewCookieStore([]byte("secret123"))
m.Use(Sessions("my_session", store))
m.Get("/testsession", func(session Session) string {
for k, v := range data {
session.Set(k, v)
}
session.Clear()
return "OK"
})
m.Get("/show", func(session Session) string {
for k, v := range data {
if session.Get(k) == v {
t.Fatal("Session clear failed")
}
}
return "OK"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
m.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
m.ServeHTTP(res2, req2)
}