-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.go
More file actions
39 lines (34 loc) · 1.04 KB
/
session_test.go
File metadata and controls
39 lines (34 loc) · 1.04 KB
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
package gobookmarks
import (
"github.com/gorilla/sessions"
"net/http/httptest"
"testing"
)
// Test that getSession clears outdated sessions and returns a fresh one.
func Test_getSessionClearsOldVersion(t *testing.T) {
Config.SessionName = "testsession"
SessionStore = sessions.NewCookieStore([]byte("secret-key"))
version = "current"
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
// create a session with an old version
s, _ := SessionStore.New(req, Config.SessionName)
s.Values["version"] = "old"
s.Values["GithubUser"] = &User{Login: "old"}
if err := s.Save(req, w); err != nil {
t.Fatalf("save old session: %v", err)
}
cookie := w.Result().Cookies()[0]
req.AddCookie(cookie)
w = httptest.NewRecorder()
newSession, err := getSession(w, req)
if err != nil {
t.Fatalf("getSession: %v", err)
}
if len(newSession.Values) != 0 {
t.Fatalf("expected empty session, got %#v", newSession.Values)
}
if h := w.Header().Get("Set-Cookie"); h == "" {
t.Fatalf("expected Set-Cookie header to clear session")
}
}