From c558e039d5399c5f569868150d3b5cb955a7d890 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 10 Feb 2020 20:35:19 +0200 Subject: [PATCH] fix #1446 Former-commit-id: 6ac95ef58bf3dd0dac8bed2100495fa9908f41d4 --- _examples/routing/conditional-chain/main.go | 11 +++++++++-- .../routing/conditional-chain/main_test.go | 17 +++++++++++++++++ context/handler.go | 3 ++- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 _examples/routing/conditional-chain/main_test.go diff --git a/_examples/routing/conditional-chain/main.go b/_examples/routing/conditional-chain/main.go index 521b0923..a7a871f7 100644 --- a/_examples/routing/conditional-chain/main.go +++ b/_examples/routing/conditional-chain/main.go @@ -4,7 +4,7 @@ import ( "github.com/kataras/iris/v12" ) -func main() { +func newApp() *iris.Application { app := iris.New() v1 := app.Party("/api/v1") @@ -15,7 +15,8 @@ func main() { } onlyWhenFilter1 := func(ctx iris.Context) { - ctx.Application().Logger().Infof("admin: %s", ctx.Params()) + ctx.Application().Logger().Infof("admin: %#+v", ctx.URLParams()) + ctx.Writef("Admin\n") ctx.Next() } @@ -43,6 +44,12 @@ func main() { ctx.HTML("requested: /api/v1/users") }) + return app +} + +func main() { + app := newApp() + // http://localhost:8080/api/v1/users // http://localhost:8080/api/v1/users?admin=true app.Run(iris.Addr(":8080")) diff --git a/_examples/routing/conditional-chain/main_test.go b/_examples/routing/conditional-chain/main_test.go new file mode 100644 index 00000000..ca0bb139 --- /dev/null +++ b/_examples/routing/conditional-chain/main_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "testing" + + "github.com/kataras/iris/v12/httptest" +) + +func TestNewConditionalHandler(t *testing.T) { + app := newApp() + e := httptest.New(t, app) + + e.GET("/api/v1/users").Expect().Status(httptest.StatusOK). + Body().Equal("requested: /api/v1/users") + e.GET("/api/v1/users").WithQuery("admin", "true").Expect().Status(httptest.StatusOK). + Body().Equal("Admin\n

Hello Admin


requested: /api/v1/users") +} diff --git a/context/handler.go b/context/handler.go index d4d938bb..79e810f1 100644 --- a/context/handler.go +++ b/context/handler.go @@ -90,6 +90,7 @@ func NewConditionalHandler(filter Filter, handlers ...Handler) Handler { // to check and modify the per-request handlers chain at runtime. currIdx := ctx.HandlerIndex(-1) currHandlers := ctx.Handlers() + if currIdx == len(currHandlers)-1 { // if this is the last handler of the chain // just add to the last the new handlers and call Next to fire those. @@ -98,7 +99,7 @@ func NewConditionalHandler(filter Filter, handlers ...Handler) Handler { return } // otherwise insert the new handlers in the middle of the current executed chain and the next chain. - newHandlers := append(currHandlers[:currIdx], append(handlers, currHandlers[currIdx+1:]...)...) + newHandlers := append(currHandlers[:currIdx+1], append(handlers, currHandlers[currIdx+1:]...)...) ctx.SetHandlers(newHandlers) ctx.Next() return