-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
global_test.go
55 lines (46 loc) · 1.11 KB
/
global_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
package session
import (
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/icza/mighty"
)
func globalHandler(w http.ResponseWriter, r *http.Request) {
if sess := Get(r); sess == nil {
sess = NewSession()
sess.SetAttr("counter", 1)
Add(sess, w)
w.Header().Set("test", "0")
} else {
if sess.Attr("counter") == 1 {
sess.SetAttr("counter", 2)
w.Header().Set("test", "1")
} else {
Remove(sess, w)
w.Header().Set("test", "2")
}
}
}
func TestGlobal(t *testing.T) {
eq := mighty.Eq(t)
Global.Close()
Global = NewCookieManagerOptions(NewInMemStore(),
&CookieMngrOptions{AllowHTTP: true, CookieMaxAge: time.Hour})
defer Close()
server := httptest.NewServer(http.HandlerFunc(globalHandler))
defer server.Close()
jar, err := cookiejar.New(nil)
eq(nil, err)
client := &http.Client{Jar: jar}
// 3 iterations: Create, Change, Remove session
// And a 4th: it should be Create again due to Remove
for i := 0; i < 4; i++ {
resp, err := client.Get(server.URL)
eq(nil, err)
eq(strconv.Itoa(i%3), resp.Header.Get("test"))
resp.Body.Close()
}
}