Skip to content
Myr

Add context.HandlerNames() (#1729)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Luis GG · committed by 田欧 · parent d6adc8d · Browse files · 2 files+26 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−0 removed

  • other:+26 added−0 removed

2 files+26 added−0 removed

modified:context_test.go+16 added
338 unmodified lines
339340 assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())341}342343func TestContextHandlerNames(t *testing.T) {344 c, _ := CreateTestContext(httptest.NewRecorder())345 c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest, func(c *Context) {}, handlerNameTest2}346347 names := c.HandlerNames()348349 assert.True(t, len(names) == 4)350 for _, name := range names {351 assert.Regexp(t, `^(.*/vendor/)?(github\.com/gin-gonic/gin\.){1}(TestContextHandlerNames\.func.*){0,1}(handlerNameTest.*){0,1}`, name)352 }353}354355func handlerNameTest(c *Context) {356357}358359func handlerNameTest2(c *Context) {360361}362363var handlerTest HandlerFunc = func(c *Context) {364365}366
1407 unmodified lines
modified:context.go+10 added
89 unmodified lines
90func (c *Context) HandlerName() string {91 return nameOfFunction(c.handlers.Last())92}9394// HandlerNames returns a list of all registered handlers for this context in descending order,95// following the semantics of HandlerName()96func (c *Context) HandlerNames() []string {97 hn := make([]string, 0, len(c.handlers))98 for _, val := range c.handlers {99 hn = append(hn, nameOfFunction(val))100 }101 return hn102}103104// Handler returns the main handler.105func (c *Context) Handler() HandlerFunc {106 return c.handlers.Last()107}
897 unmodified lines