mock session module
This commit is contained in:
parent
140db01d4e
commit
641a791265
@ -16,6 +16,7 @@ package mock
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/server/web"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -45,4 +46,19 @@ func TestMockContext(t *testing.T) {
|
||||
func (c *TestController) HelloWorld() {
|
||||
name := c.GetString("name")
|
||||
c.Ctx.WriteString(fmt.Sprintf("name=%s", name))
|
||||
}
|
||||
|
||||
func (c *TestController) HelloSession() {
|
||||
err := c.SessionRegenerateID()
|
||||
if err != nil {
|
||||
c.Ctx.WriteString("error")
|
||||
return
|
||||
}
|
||||
c.SetSession("name", "Tom")
|
||||
c.Ctx.WriteString("set")
|
||||
}
|
||||
|
||||
func (c *TestController) HelloSessionName() {
|
||||
name := c.CruSession.Get(context.Background(), "name")
|
||||
c.Ctx.WriteString(name.(string))
|
||||
}
|
||||
@ -59,3 +59,11 @@ func (m *HttpResponse) JsonUnmarshal(value interface{}) error {
|
||||
func (m *HttpResponse) BodyToString() string {
|
||||
return string(m.body)
|
||||
}
|
||||
|
||||
// Reset will reset the status to init status
|
||||
// Usually, you want to reuse this instance you may need to call Reset
|
||||
func (m *HttpResponse) Reset() {
|
||||
m.body = make([]byte, 0)
|
||||
m.header = make(http.Header)
|
||||
m.StatusCode = 0
|
||||
}
|
||||
|
||||
123
server/web/mock/session.go
Normal file
123
server/web/mock/session.go
Normal file
@ -0,0 +1,123 @@
|
||||
// Copyright 2021 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/beego/beego/v2/server/web"
|
||||
"github.com/beego/beego/v2/server/web/session"
|
||||
"github.com/google/uuid"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewSessionProvider create new SessionProvider
|
||||
// and you could use it to mock data
|
||||
// Parameter "name" is the real SessionProvider you used
|
||||
func NewSessionProvider(name string) *SessionProvider {
|
||||
sp := newSessionProvider()
|
||||
session.Register(name, sp)
|
||||
web.GlobalSessions, _ = session.NewManager(name, session.NewManagerConfig())
|
||||
return sp
|
||||
}
|
||||
|
||||
// SessionProvider will replace session provider with "mock" provider
|
||||
type SessionProvider struct {
|
||||
Store *SessionStore
|
||||
}
|
||||
|
||||
func newSessionProvider() *SessionProvider {
|
||||
return &SessionProvider{
|
||||
Store: newSessionStore(),
|
||||
}
|
||||
}
|
||||
|
||||
// SessionInit do nothing
|
||||
func (s *SessionProvider) SessionInit(ctx context.Context, gclifetime int64, config string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SessionRead return Store
|
||||
func (s *SessionProvider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
|
||||
return s.Store, nil
|
||||
}
|
||||
|
||||
// SessionExist always return true
|
||||
func (s *SessionProvider) SessionExist(ctx context.Context, sid string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// SessionRegenerate create new Store
|
||||
func (s *SessionProvider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
|
||||
s.Store = newSessionStore()
|
||||
return s.Store, nil
|
||||
}
|
||||
|
||||
// SessionDestroy reset Store to nil
|
||||
func (s *SessionProvider) SessionDestroy(ctx context.Context, sid string) error {
|
||||
s.Store = nil;
|
||||
return nil
|
||||
}
|
||||
|
||||
// SessionAll return 0
|
||||
func (s *SessionProvider) SessionAll(ctx context.Context) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// SessionGC do nothing
|
||||
func (s *SessionProvider) SessionGC(ctx context.Context) {
|
||||
}
|
||||
|
||||
|
||||
type SessionStore struct {
|
||||
sid string
|
||||
values map[interface{}]interface{}
|
||||
}
|
||||
|
||||
func (s *SessionStore) Set(ctx context.Context, key, value interface{}) error {
|
||||
s.values[key]=value
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SessionStore) Get(ctx context.Context, key interface{}) interface{} {
|
||||
return s.values[key]
|
||||
}
|
||||
|
||||
func (s *SessionStore) Delete(ctx context.Context, key interface{}) error {
|
||||
delete(s.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SessionStore) SessionID(ctx context.Context) string {
|
||||
return s.sid
|
||||
}
|
||||
|
||||
// SessionRelease do nothing
|
||||
func (s *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
func (s *SessionStore) Flush(ctx context.Context) error {
|
||||
s.values = make(map[interface{}]interface{}, 4)
|
||||
return nil
|
||||
}
|
||||
|
||||
func newSessionStore() *SessionStore {
|
||||
return &SessionStore{
|
||||
sid: uuid.New().String(),
|
||||
values: make(map[interface{}]interface{}, 4),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
48
server/web/mock/session_test.go
Normal file
48
server/web/mock/session_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2021 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package mock
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/beego/beego/v2/server/web"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSessionProvider(t *testing.T) {
|
||||
|
||||
sp := NewSessionProvider("file")
|
||||
assert.NotNil(t, sp)
|
||||
|
||||
req, err := http.NewRequest("GET", "http://localhost:8080/hello?name=tom", bytes.NewReader([]byte{}))
|
||||
assert.Nil(t, err)
|
||||
ctx, resp := NewMockContext(req)
|
||||
ctrl := &TestController{
|
||||
Controller: web.Controller{
|
||||
Ctx: ctx,
|
||||
},
|
||||
}
|
||||
ctrl.HelloSession()
|
||||
result := resp.BodyToString()
|
||||
assert.Equal(t, "set", result)
|
||||
|
||||
resp.Reset()
|
||||
ctrl.HelloSessionName()
|
||||
result = resp.BodyToString()
|
||||
|
||||
assert.Equal(t, "Tom", result)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user