diff --git a/server/web/context/context.go b/server/web/context/context.go index edbf14f5..099729d0 100644 --- a/server/web/context/context.go +++ b/server/web/context/context.go @@ -197,7 +197,7 @@ func (ctx *Context) RenderMethodResult(result interface{}) { } // Session return session store of this context of request -func (ctx *Context) Session() (store session.Store,err error){ +func (ctx *Context) Session() (store session.Store, err error) { if ctx.Input != nil { if ctx.Input.CruSession != nil { store = ctx.Input.CruSession diff --git a/server/web/context/context_test.go b/server/web/context/context_test.go index 7fdb310f..977c3cbf 100644 --- a/server/web/context/context_test.go +++ b/server/web/context/context_test.go @@ -15,9 +15,7 @@ package context import ( - "github.com/beego/beego/v2/server/web" - "github.com/beego/beego/v2/server/web/filter/session" - webSession "github.com/beego/beego/v2/server/web/session" + "github.com/beego/beego/v2/server/web/session" "net/http" "net/http/httptest" "testing" @@ -49,45 +47,25 @@ func TestXsrfReset_01(t *testing.T) { } } -func testRequest(t *testing.T, handler *web.ControllerRegister, path string, method string, code int) { - r, _ := http.NewRequest(method, path, nil) - w := httptest.NewRecorder() - handler.ServeHTTP(w, r) - - if w.Code != code { - t.Errorf("%s, %s: %d, supposed to be %d", path, method, w.Code, code) +func TestContext_Session(t *testing.T) { + c := NewContext() + if store, err := c.Session(); store != nil || err == nil { + t.FailNow() } } -func TestContext_Session(t *testing.T) { - handler := web.NewControllerRegister() +func TestContext_Session1(t *testing.T) { + c := Context{} + if store, err := c.Session(); store != nil || err == nil { + t.FailNow() + } +} - handler.InsertFilterChain( - "*", - session.Session( - webSession.ProviderMemory, - webSession.CfgCookieName(`go_session_id`), - webSession.CfgSetCookie(true), - webSession.CfgGcLifeTime(3600), - webSession.CfgMaxLifeTime(3600), - webSession.CfgSecure(false), - webSession.CfgCookieLifeTime(3600), - ), - ) - handler.InsertFilterChain( - "*", - func(next web.FilterFunc) web.FilterFunc { - return func(ctx *Context) { - if _, err := ctx.Session(); err == nil { - t.Error() - } +func TestContext_Session2(t *testing.T) { + c := NewContext() + c.Input.CruSession = &session.MemSessionStore{} - } - }, - ) - handler.Any("*", func(ctx *Context) { - ctx.Output.SetStatus(200) - }) - - testRequest(t, handler, "/dataset1/resource1", "GET", 200) + if store, err := c.Session(); store == nil || err != nil { + t.FailNow() + } } \ No newline at end of file diff --git a/server/web/filter/session/filter_test.go b/server/web/filter/session/filter_test.go index 03a88afd..687789a5 100644 --- a/server/web/filter/session/filter_test.go +++ b/server/web/filter/session/filter_test.go @@ -52,3 +52,35 @@ func TestSession(t *testing.T) { testRequest(t, handler, "/dataset1/resource1", "GET", 200) } + +func TestSession1(t *testing.T) { + handler := web.NewControllerRegister() + handler.InsertFilterChain( + "*", + Session( + session.ProviderMemory, + session.CfgCookieName(`go_session_id`), + session.CfgSetCookie(true), + session.CfgGcLifeTime(3600), + session.CfgMaxLifeTime(3600), + session.CfgSecure(false), + session.CfgCookieLifeTime(3600), + ), + ) + handler.InsertFilterChain( + "*", + func(next web.FilterFunc) web.FilterFunc { + return func(ctx *webContext.Context) { + if store, err := ctx.Session(); store == nil || err != nil { + t.Error(`store should not be nil`) + } + next(ctx) + } + }, + ) + handler.Any("*", func(ctx *webContext.Context) { + ctx.Output.SetStatus(200) + }) + + testRequest(t, handler, "/dataset1/resource1", "GET", 200) +}