예제 #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")
}
예제 #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)
}
예제 #4
0
func freeSession(a *assert.Assertion, inst *sess.Instance, w http.ResponseWriter, r *http.Request) {
	err := inst.EndSession(w, r)
	a.NotError(err)
}
예제 #5
0
func hello(a *assert.Assertion, w http.ResponseWriter) {
	n, err := w.Write([]byte("hello world"))
	a.NotError(err)
	a.NotEmpty(n)
}