-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsessions_test.go
220 lines (175 loc) · 5.45 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
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
package sessions_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/goincremental/negroni-sessions"
"github.com/goincremental/negroni-sessions/cookiestore"
"github.com/urfave/negroni"
)
func Test_Sessions(t *testing.T) {
n := negroni.Classic()
store := cookiestore.New([]byte("secret123"))
n.Use(sessions.Sessions("my_session", store))
mux := http.NewServeMux()
mux.HandleFunc("/testsession", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
session.Set("hello", "world")
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/show", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
if session.Get("hello") != "world" {
t.Error("Session writing failed")
}
})
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
n.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
n.ServeHTTP(res2, req2)
}
func Test_SessionsDeleteValue(t *testing.T) {
n := negroni.Classic()
store := cookiestore.New([]byte("secret123"))
n.Use(sessions.Sessions("my_session", store))
mux := http.NewServeMux()
mux.HandleFunc("/testsession", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
session.Set("hello", "world")
session.Delete("hello")
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/show", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
if session.Get("hello") == "world" {
t.Error("Session value deleting failed")
}
fmt.Fprintf(w, "OK")
})
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
n.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
n.ServeHTTP(res2, req2)
}
func Test_Options(t *testing.T) {
n := negroni.Classic()
store := cookiestore.New([]byte("secret123"))
store.Options(sessions.Options{
Domain: "negroni-sessions.goincremental.com",
})
n.Use(sessions.Sessions("my_session", store))
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
session.Set("hello", "world")
session.Options(sessions.Options{
Path: "/foo/bar/bat",
})
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
session.Set("hello", "world")
fmt.Fprintf(w, "OK")
})
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
n.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/foo", nil)
n.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=negroni-sessions.goincremental.com" {
t.Error("Error writing domain with options:", s[1])
}
}
func Test_Flashes(t *testing.T) {
n := negroni.Classic()
store := cookiestore.New([]byte("secret123"))
n.Use(sessions.Sessions("my_session", store))
mux := http.NewServeMux()
mux.HandleFunc("/set", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
session.AddFlash("hello world")
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/show", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
l := len(session.Flashes())
if l != 1 {
t.Error("Flashes count does not equal 1. Equals ", l)
}
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/showagain", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
l := len(session.Flashes())
if l != 0 {
t.Error("flashes count is not 0 after reading. Equals ", l)
}
fmt.Fprintf(w, "OK")
})
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/set", nil)
n.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
n.ServeHTTP(res2, req2)
res3 := httptest.NewRecorder()
req3, _ := http.NewRequest("GET", "/showagain", nil)
req3.Header.Set("Cookie", res2.Header().Get("Set-Cookie"))
n.ServeHTTP(res3, req3)
}
func Test_SessionsClear(t *testing.T) {
n := negroni.Classic()
data := map[string]string{
"hello": "world",
"foo": "bar",
"apples": "oranges",
}
store := cookiestore.New([]byte("secret123"))
n.Use(sessions.Sessions("my_session", store))
mux := http.NewServeMux()
mux.HandleFunc("/testsession", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
for k, v := range data {
session.Set(k, v)
}
session.Clear()
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/show", func(w http.ResponseWriter, req *http.Request) {
session := sessions.GetSession(req)
for k, v := range data {
if session.Get(k) == v {
t.Fatal("Session clear failed")
}
}
fmt.Fprintf(w, "OK")
})
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/testsession", nil)
n.ServeHTTP(res, req)
res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/show", nil)
req2.Header.Set("Cookie", res.Header().Get("Set-Cookie"))
n.ServeHTTP(res2, req2)
}