Add context.HandlerNames() (#1729)
* Add context.HandlerNames() This change adds a HandlerNames method that will return all registered handles in the context, in descending order This is useful for debugging and troubleshooting purposes, especially in large apps * Tests Add tests for HandlerNames * Fix HandlerNames test * Simplify test
This commit is contained in:
parent
d6adc8d0cc
commit
62749f0db4
10
context.go
10
context.go
@ -91,6 +91,16 @@ func (c *Context) HandlerName() string {
|
|||||||
return nameOfFunction(c.handlers.Last())
|
return nameOfFunction(c.handlers.Last())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandlerNames returns a list of all registered handlers for this context in descending order,
|
||||||
|
// following the semantics of HandlerName()
|
||||||
|
func (c *Context) HandlerNames() []string {
|
||||||
|
hn := make([]string, 0, len(c.handlers))
|
||||||
|
for _, val := range c.handlers {
|
||||||
|
hn = append(hn, nameOfFunction(val))
|
||||||
|
}
|
||||||
|
return hn
|
||||||
|
}
|
||||||
|
|
||||||
// Handler returns the main handler.
|
// Handler returns the main handler.
|
||||||
func (c *Context) Handler() HandlerFunc {
|
func (c *Context) Handler() HandlerFunc {
|
||||||
return c.handlers.Last()
|
return c.handlers.Last()
|
||||||
|
@ -340,10 +340,26 @@ func TestContextHandlerName(t *testing.T) {
|
|||||||
assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
|
assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextHandlerNames(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest, func(c *Context) {}, handlerNameTest2}
|
||||||
|
|
||||||
|
names := c.HandlerNames()
|
||||||
|
|
||||||
|
assert.True(t, len(names) == 4)
|
||||||
|
for _, name := range names {
|
||||||
|
assert.Regexp(t, `^(.*/vendor/)?(github\.com/gin-gonic/gin\.){1}(TestContextHandlerNames\.func.*){0,1}(handlerNameTest.*){0,1}`, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handlerNameTest(c *Context) {
|
func handlerNameTest(c *Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handlerNameTest2(c *Context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
var handlerTest HandlerFunc = func(c *Context) {
|
var handlerTest HandlerFunc = func(c *Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user