Exemple #1
0
func testSession(a *assert.Assertion, inst *sess.Instance, w http.ResponseWriter, r *http.Request) {
	// 获取session接口实例
	sess, err := inst.StartSession(w, r)
	a.NotError(err)
	a.NotNil(sess)

	// 设置值
	sess.Set("uid", 1)
	uid, found := sess.Get("uid")
	a.True(found)
	a.Equal(uid, 1)

	// 不存在的值
	username := sess.MustGet("username", "abc")
	a.Equal(username, "abc")

	// 添加值
	sess.Set("username", "caixw")
	username = sess.MustGet("username", "abc")
	a.Equal(username, "caixw")

	// 修改值
	sess.Set("uid", "2")
	uid, found = sess.Get("uid")
	a.True(found)
	a.Equal(uid, "2")
}
Exemple #2
0
// 测试cookie能否正常使用
func testSession2(a *assert.Assertion, inst *sess.Instance, w http.ResponseWriter, r *http.Request) {
	sess, err := inst.StartSession(w, r)
	a.NotError(err)
	a.NotNil(sess)

	uid, found := sess.Get("uid")
	a.True(found)
	a.Equal(uid, "2")
}
Exemple #3
0
// 测试freeSession之后,值是否被清空
func freeSession2(a *assert.Assertion, inst *sess.Instance, w http.ResponseWriter, r *http.Request) {
	sess, err := inst.StartSession(w, r)
	a.NotError(err)
	a.NotNil(sess)
	defer func() {
		a.NotError(sess.Release())
	}()

	uid, found := sess.Get("uid")
	a.False(found)
	a.Equal(uid, nil)
}
Exemple #4
0
func freeSession(a *assert.Assertion, inst *sess.Instance, w http.ResponseWriter, r *http.Request) {
	err := inst.EndSession(w, r)
	a.NotError(err)
}