This commit is contained in:
Anker Jam 2021-01-10 14:22:11 +08:00
parent a93e35244a
commit 87ffa0b730
3 changed files with 50 additions and 40 deletions

View File

@ -197,7 +197,7 @@ func (ctx *Context) RenderMethodResult(result interface{}) {
} }
// Session return session store of this context of request // 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 != nil {
if ctx.Input.CruSession != nil { if ctx.Input.CruSession != nil {
store = ctx.Input.CruSession store = ctx.Input.CruSession

View File

@ -15,9 +15,7 @@
package context package context
import ( import (
"github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/session"
"github.com/beego/beego/v2/server/web/filter/session"
webSession "github.com/beego/beego/v2/server/web/session"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "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) { func TestContext_Session(t *testing.T) {
handler := web.NewControllerRegister() c := NewContext()
if store, err := c.Session(); store != nil || err == nil {
handler.InsertFilterChain( t.FailNow()
"*", }
session.Session( }
webSession.ProviderMemory,
webSession.CfgCookieName(`go_session_id`), func TestContext_Session1(t *testing.T) {
webSession.CfgSetCookie(true), c := Context{}
webSession.CfgGcLifeTime(3600), if store, err := c.Session(); store != nil || err == nil {
webSession.CfgMaxLifeTime(3600), t.FailNow()
webSession.CfgSecure(false), }
webSession.CfgCookieLifeTime(3600), }
),
) func TestContext_Session2(t *testing.T) {
handler.InsertFilterChain( c := NewContext()
"*", c.Input.CruSession = &session.MemSessionStore{}
func(next web.FilterFunc) web.FilterFunc {
return func(ctx *Context) { if store, err := c.Session(); store == nil || err != nil {
if _, err := ctx.Session(); err == nil { t.FailNow()
t.Error()
} }
}
},
)
handler.Any("*", func(ctx *Context) {
ctx.Output.SetStatus(200)
})
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
} }

View File

@ -52,3 +52,35 @@ func TestSession(t *testing.T) {
testRequest(t, handler, "/dataset1/resource1", "GET", 200) 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)
}