-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsessions_test.go
More file actions
57 lines (46 loc) · 1.2 KB
/
sessions_test.go
File metadata and controls
57 lines (46 loc) · 1.2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package abclientstate
import (
"crypto/rand"
"net/http/httptest"
"testing"
"time"
"github.com/gorilla/sessions"
"github.com/aarondl/authboss/v3"
)
func TestExpiry(t *testing.T) {
t.Parallel()
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
t.Fatal(err)
}
storer := NewSessionStorer("x", key, nil)
cookieStore := storer.Store.(*sessions.CookieStore)
cookieStore.MaxAge(1)
cookieStore.Options.HttpOnly = false
cookieStore.Options.Secure = false
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
oldState, err := storer.ReadState(r)
if err != nil {
t.Fatal(err)
}
evs := []authboss.ClientStateEvent{{Kind: authboss.ClientStateEventPut, Key: "hello", Value: "World"}}
if err := storer.WriteState(w, oldState, evs); err != nil {
t.Fatal(err)
}
w.Flush()
cookie := w.Header().Get("Set-Cookie")
if len(cookie) == 0 {
t.Error("it should have set a cookie")
}
time.Sleep(time.Second * 2)
r = httptest.NewRequest("GET", "/", nil)
r.Header.Set("Cookie", cookie)
oldState, err = storer.ReadState(r)
if err != nil {
t.Fatal(err)
}
if val, ok := oldState.Get("hello"); ok || len(val) != 0 {
t.Error("it should not have had a value:", val)
}
}