From fb790813eb9f506573abf8d9296821b5dbe4d1b4 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 14 Jun 2020 08:44:41 +0300 Subject: [PATCH] fix #1537 --- mvc/controller.go | 19 +++++++++++++++++-- mvc/controller_test.go | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mvc/controller.go b/mvc/controller.go index 69ae1d0a87..4529c81653 100644 --- a/mvc/controller.go +++ b/mvc/controller.go @@ -236,6 +236,18 @@ func (c *ControllerActivator) isReservedMethod(name string) bool { return false } +func (c *ControllerActivator) isReservedMethodHandler(method, path string) bool { + for _, routes := range c.routes { + for _, r := range routes { + if r.Method == method && r.Path == path { + return true + } + } + } + + return false +} + func (c *ControllerActivator) markAsWebsocket() { c.servesWebsocket = true c.attachInjector() @@ -296,8 +308,10 @@ func (c *ControllerActivator) addErr(err error) bool { // Just like `Party#Handle`, it returns the `*router.Route`, if failed // then it logs the errors and it returns nil, you can check the errors // programmatically by the `Party#GetReporter`. +// +// Handle will add a route to the "funcName". func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) *router.Route { - routes := c.HandleMany(method, path, funcName, middleware...) + routes := c.handleMany(method, path, funcName, false, middleware...) if len(routes) == 0 { return nil } @@ -315,13 +329,14 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware . // func (*Controller) BeforeActivation(b mvc.BeforeActivation) { // b.HandleMany("GET", "/path /path1" /path2", "HandlePath") // } +// HandleMany will override any routes of this "funcName". func (c *ControllerActivator) HandleMany(method, path, funcName string, middleware ...context.Handler) []*router.Route { return c.handleMany(method, path, funcName, true, middleware...) } func (c *ControllerActivator) handleMany(method, path, funcName string, override bool, middleware ...context.Handler) []*router.Route { if method == "" || path == "" || funcName == "" || - c.isReservedMethod(funcName) { + (c.isReservedMethod(funcName) && c.isReservedMethodHandler(method, path)) { // isReservedMethod -> if it's already registered // by a previous Handle or analyze methods internally. return nil diff --git a/mvc/controller_test.go b/mvc/controller_test.go index 3b695dbc92..ecf4ba8d7b 100644 --- a/mvc/controller_test.go +++ b/mvc/controller_test.go @@ -469,12 +469,21 @@ type testControllerActivateListener struct { func (c *testControllerActivateListener) BeforeActivation(b BeforeActivation) { b.Dependencies().Register(&testBindType{title: "overrides the dependency but not the field"}) // overrides the `Register` previous calls. + + // b.Handle("POST", "/me/tos-read", "MeTOSRead") + // b.Handle("GET", "/me/tos-read", "MeTOSRead") + // OR: + b.HandleMany("GET POST", "/me/tos-read", "MeTOSRead") } func (c *testControllerActivateListener) Get() string { return c.TitlePointer.title } +func (c *testControllerActivateListener) MeTOSRead() string { + return "MeTOSRead" +} + func TestControllerActivateListener(t *testing.T) { app := iris.New() New(app).Handle(new(testControllerActivateListener)) @@ -493,6 +502,11 @@ func TestControllerActivateListener(t *testing.T) { e := httptest.New(t, app) e.GET("/").Expect().Status(iris.StatusOK). Body().Equal("overrides the dependency but not the field") + e.GET("/me/tos-read").Expect().Status(iris.StatusOK). + Body().Equal("MeTOSRead") + e.POST("/me/tos-read").Expect().Status(iris.StatusOK). + Body().Equal("MeTOSRead") + e.GET("/manual").Expect().Status(iris.StatusOK). Body().Equal("overrides the dependency but not the field") e.GET("/manual2").Expect().Status(iris.StatusOK).