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) defer func() { a.NotError(sess.Release()) }() // 设置值 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") }
// 测试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) defer func() { a.NotError(sess.Release()) }() uid, found := sess.Get("uid") a.True(found) a.Equal(uid, "2") }