From 641a7912654f10314d2bd61081eef119d851afa9 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 13 Apr 2021 19:38:20 +0800 Subject: [PATCH 1/4] mock session module --- server/web/mock/context_test.go | 16 +++++ server/web/mock/response.go | 8 +++ server/web/mock/session.go | 123 ++++++++++++++++++++++++++++++++ server/web/mock/session_test.go | 48 +++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 server/web/mock/session.go create mode 100644 server/web/mock/session_test.go diff --git a/server/web/mock/context_test.go b/server/web/mock/context_test.go index 12798804..b5c23fab 100644 --- a/server/web/mock/context_test.go +++ b/server/web/mock/context_test.go @@ -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)) } \ No newline at end of file diff --git a/server/web/mock/response.go b/server/web/mock/response.go index ccaf002c..cd190a6e 100644 --- a/server/web/mock/response.go +++ b/server/web/mock/response.go @@ -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 +} diff --git a/server/web/mock/session.go b/server/web/mock/session.go new file mode 100644 index 00000000..e3bf6872 --- /dev/null +++ b/server/web/mock/session.go @@ -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), + } +} + + + diff --git a/server/web/mock/session_test.go b/server/web/mock/session_test.go new file mode 100644 index 00000000..6d21f7c5 --- /dev/null +++ b/server/web/mock/session_test.go @@ -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) +} + From 452dae86be4d9c81e6c192c837703f4dfda75691 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Wed, 14 Apr 2021 23:13:11 +0800 Subject: [PATCH 2/4] allow register mock --- CHANGELOG.md | 2 +- core/config/ini.go | 2 +- server/web/mock/context_test.go | 2 +- server/web/mock/session.go | 2 ++ server/web/session/session.go | 6 +----- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4712dc6..19dc852e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/core/config/ini.go b/core/config/ini.go index f2877ce8..205760c2 100644 --- a/core/config/ini.go +++ b/core/config/ini.go @@ -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) } } diff --git a/server/web/mock/context_test.go b/server/web/mock/context_test.go index b5c23fab..734c9abf 100644 --- a/server/web/mock/context_test.go +++ b/server/web/mock/context_test.go @@ -54,7 +54,7 @@ func (c *TestController) HelloSession() { c.Ctx.WriteString("error") return } - c.SetSession("name", "Tom") + _ = c.SetSession("name", "Tom") c.Ctx.WriteString("set") } diff --git a/server/web/mock/session.go b/server/web/mock/session.go index e3bf6872..d633468d 100644 --- a/server/web/mock/session.go +++ b/server/web/mock/session.go @@ -77,6 +77,7 @@ func (s *SessionProvider) SessionAll(ctx context.Context) int { // SessionGC do nothing func (s *SessionProvider) SessionGC(ctx context.Context) { + // we do anything since we don't need to mock GC } @@ -105,6 +106,7 @@ func (s *SessionStore) SessionID(ctx context.Context) string { // 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 { diff --git a/server/web/session/session.go b/server/web/session/session.go index f0b7e292..154db92a 100644 --- a/server/web/session/session.go +++ b/server/web/session/session.go @@ -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 } From 305d2205532d47ab086f13b929dbcf2ccf95f1bc Mon Sep 17 00:00:00 2001 From: byene0923 Date: Thu, 15 Apr 2021 14:44:57 +0800 Subject: [PATCH 3/4] improve code quality --- server/web/tree.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/web/tree.go b/server/web/tree.go index 79f3da7a..2a8ca489 100644 --- a/server/web/tree.go +++ b/server/web/tree.go @@ -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:] } From 69e50c6675495f9c5a99eb033ce644b70c532a5e Mon Sep 17 00:00:00 2001 From: byene0923 Date: Thu, 15 Apr 2021 15:30:08 +0800 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4712dc6..cc660b72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)