func TestSessionSupport_Expire(t *testing.T) { assert := wcg.NewAssert(t) // default configugration cfg := &SessionConfig{ StoreFactory: nil, Key: "wcgsess", MaxAge: 1, // 3 seconds CookieName: "wcgsess", Domain: "", HTTPOnly: true, Path: "/", } load, save := SessionSupportWithConfig(cfg) r, _ := http.NewRequest("GET", "http://example.com/foo", nil) req := wcg.NewRequest(r) w := httptest.NewRecorder() res := wcg.NewResponse(w, req) load.Process(res, req) req.Session.Set("foo", "bar") save.Process(res, req) sid1 := req.Session.ID time.Sleep(1 * time.Second) // Another request is coming with the same cookie. c := wcg.ParseCookie(w.Header().Get("set-cookie")) r, _ = http.NewRequest("GET", "http://example.com/foo", nil) r.Header.Set("cookie", c.String()) req = wcg.NewRequest(r) w = httptest.NewRecorder() res = wcg.NewResponse(w, req) load.Process(res, req) sid2 := req.Session.ID assert.Not(sid1 == sid2, "Keep session") }
func TestSessionSupport(t *testing.T) { assert := wcg.NewAssert(t) // default configugration cfg := SessionConfigIni load, save := SessionSupport() r, _ := http.NewRequest("GET", "http://example.com/foo", nil) req := wcg.NewRequest(r) w := httptest.NewRecorder() res := wcg.NewResponse(w, req) load.Process(res, req) assert.NotNil(req.Session, "req.Session with SessionSupport") // application set the sessoin data. req.Session.Set("foo", "bar") // Check set-cookie header on response, which should include signed session id. c := wcg.ParseCookie(w.Header().Get("set-cookie")) assert.EqStr(cfg.CookieName, c.Name, "Issued cookie name") assert.NotZero(c.Value, "Issued cookie value") // Run save handler. save.Process(res, req) // Another request is coming with the same session id. r, _ = http.NewRequest("GET", "http://example.com/foo", nil) r.Header.Set("cookie", c.String()) req = wcg.NewRequest(r) w = httptest.NewRecorder() res = wcg.NewResponse(w, req) load.Process(res, req) val, _ := req.Session.Get("foo") assert.EqStr("bar", val, "Stored value check") }