-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.go
58 lines (45 loc) · 1.38 KB
/
sessions.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
package webtestutil
import (
"os"
"http"
"gorilla.googlecode.com/hg/gorilla/sessions"
)
// Session data
var sessionData sessions.SessionData
// Transient testing session store with easy inspection and manipulation
type TestingSessionStore struct {
// List of encoders registered for this store.
encoders []sessions.SessionEncoder
}
// Register the testing session store and use it as the default store key
func RegisterTestingStore() {
sessions.DefaultSessionFactory.SetStore("testing", new(TestingSessionStore))
sessions.DefaultStoreKey = "testing"
}
// Set the session data
func SetSessionData(data sessions.SessionData) {
sessionData = data
}
func SessionData() sessions.SessionData {
if sessionData == nil {
sessionData = make(sessions.SessionData)
}
return sessionData
}
// Reset the session data, should be called in each test using the session
func ResetSession() {
sessionData = nil
}
func (s *TestingSessionStore) Load(r *http.Request, key string, info *sessions.SessionInfo) {
info.Data = SessionData()
}
func (s *TestingSessionStore) Save(r *http.Request, w http.ResponseWriter, key string, info *sessions.SessionInfo) (bool, os.Error) {
sessionData = info.Data
return true, nil
}
func (s *TestingSessionStore) Encoders() []sessions.SessionEncoder {
return s.encoders
}
func (s *TestingSessionStore) SetEncoders(encoders ...sessions.SessionEncoder) {
s.encoders = encoders
}