diff --git a/CHANGELOG.md b/CHANGELOG.md index fc786fb1..ea3a4bc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix 4480: log format incorrect. [4482](https://github.com/beego/beego/pull/4482) - Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391) - Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385) +- Fix 4590: Forget to check URL when FilterChain invoke `next()`. [4593](https://github.com/beego/beego/pull/4593) - Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385) - Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386) - Fix 4444: panic when 404 not found. [4446](https://github.com/beego/beego/pull/4446) diff --git a/server/web/filter_chain_test.go b/server/web/filter_chain_test.go index 2a428b78..ee19b1e9 100644 --- a/server/web/filter_chain_test.go +++ b/server/web/filter_chain_test.go @@ -15,16 +15,15 @@ package web import ( + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" - "github.com/stretchr/testify/assert" - "github.com/beego/beego/v2/server/web/context" ) -func TestControllerRegister_InsertFilterChain(t *testing.T) { +func TestControllerRegisterInsertFilterChain(t *testing.T) { InsertFilterChain("/*", func(next FilterFunc) FilterFunc { return func(ctx *context.Context) { @@ -46,3 +45,58 @@ func TestControllerRegister_InsertFilterChain(t *testing.T) { assert.Equal(t, "filter-chain", w.Header().Get("filter")) } + +func TestFilterChainRouter(t *testing.T) { + + + app := NewHttpSever() + + const filterNonMatch = "filter-chain-non-match" + app.InsertFilterChain("/app/nonMatch/before/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("filter", filterNonMatch) + next(ctx) + } + }) + + const filterAll = "filter-chain-all" + app.InsertFilterChain("/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("filter", filterAll) + next(ctx) + } + }) + + app.InsertFilterChain("/app/nonMatch/after/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("filter", filterNonMatch) + next(ctx) + } + }) + + app.InsertFilterChain("/app/match/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("match", "yes") + next(ctx) + } + }) + + r, _ := http.NewRequest("GET", "/app/match", nil) + w := httptest.NewRecorder() + + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.Equal(t, "yes", w.Header().Get("match")) + + r, _ = http.NewRequest("GET", "/app/match1", nil) + w = httptest.NewRecorder() + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.NotEqual(t, "yes", w.Header().Get("match")) + + r, _ = http.NewRequest("GET", "/app/nonMatch", nil) + w = httptest.NewRecorder() + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.NotEqual(t, "yes", w.Header().Get("match")) +} diff --git a/server/web/router.go b/server/web/router.go index 7e78c76c..93c77e7d 100644 --- a/server/web/router.go +++ b/server/web/router.go @@ -486,7 +486,11 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter // } func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) { root := p.chainRoot - filterFunc := chain(root.filterFunc) + //filterFunc := chain(root.filterFunc) + filterFunc := chain(func(ctx *beecontext.Context) { + var preFilterParams map[string]string + root.filter(ctx, p.getUrlPath(ctx), preFilterParams) + }) opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive)) p.chainRoot = newFilterRouter(pattern, filterFunc, opts...) p.chainRoot.next = root