示例#1
0
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()
	}
}
示例#2
0
func TestSessionAttrs(t *testing.T) {
	eq := mighty.Eq(t)
	s := NewSession()

	eq(nil, s.Attr("a"))
	s.SetAttr("a", 1)
	eq(1, s.Attr("a"))
	eq(1, len(s.Attrs()))

	s.SetAttr("a", nil)
	eq(nil, s.Attr("a"))
	eq(0, len(s.Attrs()))
}
示例#3
0
func TestInMemStoreSessCleaner(t *testing.T) {
	eq := mighty.Eq(t)

	st := NewInMemStoreOptions(&InMemStoreOptions{SessCleanerInterval: 10 * time.Millisecond})
	defer st.Close()

	s := NewSessionOptions(&SessOptions{Timeout: 50 * time.Millisecond})
	st.Add(s)
	eq(s, st.Get(s.Id()))

	time.Sleep(30 * time.Millisecond)
	eq(s, st.Get(s.Id()))

	time.Sleep(80 * time.Millisecond)
	eq(nil, st.Get(s.Id()))
}
示例#4
0
func TestSessOptions(t *testing.T) {
	eq := mighty.Eq(t)

	so := &SessOptions{
		Attrs:    map[string]interface{}{"a": 1},
		CAttrs:   map[string]interface{}{"ca": 2},
		IdLength: 9,
		Timeout:  43 * time.Minute,
	}

	s := NewSessionOptions(so)

	eq(true, reflect.DeepEqual(s.Attrs(), so.Attrs))

	for k, v := range so.CAttrs {
		eq(v, s.CAttr(k))
	}

	data, err := base64.URLEncoding.DecodeString(s.Id())
	eq(nil, err)
	eq(so.IdLength, len(data))

	eq(so.Timeout, s.Timeout())
}