예제 #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")
}
예제 #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")
}