예제 #1
0
func TestSessionId(t *testing.T) {
	id := session.NewId()
	t.Log("NewId", id)
	if id == "" {
		t.Error("session id error")
	}
}
예제 #2
0
파일: session.go 프로젝트: jango2015/wk
func (srv *HttpServer) newSession(ctx *HttpContext) {
	id := session.NewId()
	err := SessionDriver.New(id, time.Duration(srv.Config.SessionTimeout)*time.Second)
	if err != nil {
		return
	}

	cookie := &http.Cookie{
		Name:     cookieSessionId,
		Value:    id,
		Path:     `/`,
		HttpOnly: true,
	}
	//Logger.Println("cookie", cookie)
	ctx.SetCookie(cookie)
	ctx.SessionIsNew = true
	ctx.Session = Session(id)
}
예제 #3
0
func TestSessionBasic(t *testing.T) {
	id := session.NewId()
	key := "key"
	value := "value"

	var ok bool
	var err error

	driver := session.GetDriver(driverName)
	if driver == nil {
		t.Error("GetDriver", "return nil", driverName)
		return
	}

	if name := driver.Name(); name != driverName {
		successAndEq(t, "Name", nil, driverName, name)
	}

	if err := driver.New(id, time.Second); err != nil {
		t.Error(t, "New", err)
		return
	}

	ok, err = driver.Add(id, key, value)
	successAndEq(t, "Add", err, true, ok)

	ok, err = driver.Add(id, key, value)
	successAndEq(t, "Add Again", err, false, ok)

	v, ok, err := driver.Get(id, key)
	successAndEq(t, "Get err", err, true, ok)
	successAndEq(t, "Get ok", err, value, v)

	value = value + "_new"
	err = driver.Set(id, key, value)
	successAndEq(t, "Set", err, nil, nil)

	v, ok, err = driver.Get(id, key)
	successAndEq(t, "Get after set", err, value, v)

	keys, err := driver.Keys(id)
	successAndEq(t, "Keys", err, key, keys[0])

	err = driver.Remove(id, key)
	successAndEq(t, "Remove", err, nil, nil)

	v, ok, err = driver.Get(id, key)
	successAndEq(t, "Get after remove", err, false, ok)
	successAndEq(t, "Get after remove", err, nil, v)

	keys, err = driver.Keys(id)
	successAndEq(t, "Keys after remove", err, 0, len(keys))

	ok, err = driver.Exists(id)
	successAndEq(t, "Exists", err, true, ok)

	err = driver.Abandon(id)
	successAndEq(t, "Abandon", err, nil, nil)

	ok, err = driver.Exists(id)
	successAndEq(t, "Exists after Abandon", err, false, ok)
}