Skip to content

Commit

Permalink
fix #1537
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jun 14, 2020
1 parent abd6ed4 commit fb79081
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mvc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions mvc/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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).
Expand Down

0 comments on commit fb79081

Please sign in to comment.