Merge pull request #4593 from flycash/fix4590

Fix 4590: Forget to check URL when FilterChain invoke next()
This commit is contained in:
Ming Deng 2021-04-30 07:37:37 -07:00 committed by GitHub
commit 1ac3c5abed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 4 deletions

View File

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

View File

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

View File

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