Example #1
0
// setupSession
func (srv *HttpServer) configSession() error {
	if !srv.Config.SessionEnable {
		Logger.Println("Session: Enable=", srv.Config.SessionEnable)
		return nil
	}

	name := srv.Config.SessionDriver
	if name == "" {
		name = defaultSessionDriver
	}
	SessionDriver = session.GetDriver(name)
	if SessionDriver == nil {
		return errors.New("session driver is nil:" + name)
	}

	//name = SessionDriver.Name()
	if srv.Config.PluginConfig != nil {
		if node, ok := srv.Config.PluginConfig.Child(name); ok && node != nil {
			SessionDriver.Init(node.Dump())
		} else {
			SessionDriver.Init("")
		}
	} else {
		SessionDriver.Init("")
	}

	Logger.Printf("Session: Enable=%v; Timeout =%d; Driver=%s %v \n",
		srv.Config.SessionEnable, srv.Config.SessionTimeout, srv.Config.SessionDriver, SessionDriver)

	return nil
}
Example #2
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)
}