move getting session store to web context
This commit is contained in:
parent
1f475585e5
commit
31f79c2ee2
@ -29,6 +29,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -195,6 +196,22 @@ func (ctx *Context) RenderMethodResult(result interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Session return session store of this context of request
|
||||||
|
func (ctx *Context) Session() (store session.Store,err error){
|
||||||
|
if ctx.Input != nil {
|
||||||
|
if ctx.Input.CruSession != nil {
|
||||||
|
store = ctx.Input.CruSession
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
err = errors.New(`no valid session store(please initialize session)`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = errors.New(`no valid input`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Response is a wrapper for the http.ResponseWriter
|
// Response is a wrapper for the http.ResponseWriter
|
||||||
// Started: if true, response was already written to so the other handler will not be executed
|
// Started: if true, response was already written to so the other handler will not be executed
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
|||||||
@ -15,6 +15,9 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
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"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -45,3 +48,46 @@ func TestXsrfReset_01(t *testing.T) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
handler := web.NewControllerRegister()
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
handler.Any("*", func(ctx *Context) {
|
||||||
|
ctx.Output.SetStatus(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
||||||
|
}
|
||||||
@ -2,7 +2,6 @@ package session
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"github.com/beego/beego/v2/core/logs"
|
"github.com/beego/beego/v2/core/logs"
|
||||||
"github.com/beego/beego/v2/server/web"
|
"github.com/beego/beego/v2/server/web"
|
||||||
webContext "github.com/beego/beego/v2/server/web/context"
|
webContext "github.com/beego/beego/v2/server/web/context"
|
||||||
@ -11,12 +10,6 @@ import (
|
|||||||
|
|
||||||
//Session maintain session for web service
|
//Session maintain session for web service
|
||||||
//Session new a session storage and store it into webContext.Context
|
//Session new a session storage and store it into webContext.Context
|
||||||
//
|
|
||||||
//params:
|
|
||||||
//ctx: pointer of beego web context
|
|
||||||
//storeKey: set the storage key in ctx.Input
|
|
||||||
//
|
|
||||||
//if you want to get session storage, just see GetStore
|
|
||||||
func Session(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain {
|
func Session(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain {
|
||||||
sessionConfig := session.NewManagerConfig(options...)
|
sessionConfig := session.NewManagerConfig(options...)
|
||||||
sessionManager, _ := session.NewManager(string(providerType), sessionConfig)
|
sessionManager, _ := session.NewManager(string(providerType), sessionConfig)
|
||||||
@ -37,23 +30,6 @@ func Session(providerType session.ProviderType, options ...session.ManagerConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
next(ctx)
|
next(ctx)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetStore get session storage in beego web context
|
|
||||||
func GetStore(ctx *webContext.Context) (store session.Store, err error) {
|
|
||||||
if ctx == nil {
|
|
||||||
err = errors.New(`ctx is nil`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if s := ctx.Input.CruSession; s != nil {
|
|
||||||
store = s
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
err = errors.New(`can not get a valid session store`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"github.com/beego/beego/v2/server/web"
|
"github.com/beego/beego/v2/server/web"
|
||||||
webContext "github.com/beego/beego/v2/server/web/context"
|
webContext "github.com/beego/beego/v2/server/web/context"
|
||||||
"github.com/beego/beego/v2/server/web/session"
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
@ -53,62 +52,3 @@ func TestSession(t *testing.T) {
|
|||||||
|
|
||||||
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetStore(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) {
|
|
||||||
var (
|
|
||||||
checkKey = `asodiuasdk1j)AS(87`
|
|
||||||
checkValue = `ASsd-09812-3`
|
|
||||||
|
|
||||||
store session.Store
|
|
||||||
err error
|
|
||||||
|
|
||||||
c = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
if store, err = GetStore(ctx); err == nil {
|
|
||||||
if store == nil {
|
|
||||||
t.Error(`store should not be nil`)
|
|
||||||
} else {
|
|
||||||
_ = store.Set(c, checkKey, checkValue)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
next(ctx)
|
|
||||||
|
|
||||||
if store != nil {
|
|
||||||
if v := store.Get(c, checkKey); v != checkValue {
|
|
||||||
t.Error(v, `is not equals to`, checkValue)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error(`store should not be nil`)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
handler.Any("*", func(ctx *webContext.Context) {
|
|
||||||
ctx.Output.SetStatus(200)
|
|
||||||
})
|
|
||||||
|
|
||||||
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user