mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
parent
ed5964716b
commit
4afef007a4
|
@ -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
|
||||
|
|
|
@ -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).
|
||||
|
|
Loading…
Reference in New Issue
Block a user