Merge branch 'develop' of https://github.com/holooooo/beego into develop

This commit is contained in:
holooooo 2021-04-19 18:18:12 +08:00
commit 29849ddb36
8 changed files with 203 additions and 9 deletions

View File

@ -1,5 +1,5 @@
# developing
- Web mock and test support. [4565](https://github.com/beego/beego/pull/4565)
- Web mock and test support. [4565](https://github.com/beego/beego/pull/4565) [4574](https://github.com/beego/beego/pull/4574)
- Error codes definition of cache module. [4493](https://github.com/beego/beego/pull/4493)
- Remove generateCommentRoute http hook. Using `bee generate routers` commands instead.[4486](https://github.com/beego/beego/pull/4486) [bee PR 762](https://github.com/beego/bee/pull/762)
- Fix: /abc.html/aaa match /abc/aaa. [4459](https://github.com/beego/beego/pull/4459)
@ -32,6 +32,7 @@
- Feat: add token bucket ratelimit filter [4508](https://github.com/beego/beego/pull/4508)
- Improve: Avoid ignoring mistakes that need attention [4548](https://github.com/beego/beego/pull/4548)
- Integration: DeepSource [4560](https://github.com/beego/beego/pull/4560)
- Integration: Remove unnecessary function call [4577](https://github.com/beego/beego/pull/4577)

View File

@ -521,7 +521,7 @@ func init() {
err := InitGlobalInstance("ini", "conf/app.conf")
if err != nil {
logs.Warn("init global config instance failed. If you donot use this, just ignore it. ", err)
logs.Debug("init global config instance failed. If you donot use this, just ignore it. ", err)
}
}

View File

@ -16,6 +16,7 @@ package mock
import (
"bytes"
"context"
"fmt"
"github.com/beego/beego/v2/server/web"
"github.com/stretchr/testify/assert"
@ -46,3 +47,18 @@ 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))
}

View File

@ -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
}

125
server/web/mock/session.go Normal file
View File

@ -0,0 +1,125 @@
// 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) {
// we do anything since we don't need to mock GC
}
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) {
// Support in the future if necessary, now I think we don't need to implement this
}
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),
}
}

View 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)
}

View File

@ -70,15 +70,11 @@ var provides = make(map[string]Provider)
var SLogger = NewSessionLog(os.Stderr)
// Register makes a session provide available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
// If provider is nil, it panic
func Register(name string, provide Provider) {
if provide == nil {
panic("session: Register provide is nil")
}
if _, dup := provides[name]; dup {
panic("session: Register called twice for provider " + name)
}
provides[name] = provide
}

View File

@ -294,8 +294,8 @@ func (t *Tree) Match(pattern string, ctx *context.Context) (runObject interface{
func (t *Tree) match(treePattern string, pattern string, wildcardValues []string, ctx *context.Context) (runObject interface{}) {
if len(pattern) > 0 {
i := 0
for ; i < len(pattern) && pattern[i] == '/'; i++ {
i, l := 0, len(pattern)
for ; i < l && pattern[i] == '/'; i++ {
}
pattern = pattern[i:]
}