Commit 4edb1364 by Carl Bergquist Committed by GitHub

fixes bug with invalid handler name for metrics (#29529)

closes https://github.com/grafana/grafana/issues/29487

Signed-off-by: bergquist <carl.bergquist@gmail.com>
parent 8d5b0084
...@@ -118,22 +118,24 @@ func (rr *routeRegister) Register(router Router) { ...@@ -118,22 +118,24 @@ func (rr *routeRegister) Register(router Router) {
func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) { func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) {
h := make([]macaron.Handler, 0) h := make([]macaron.Handler, 0)
fullPattern := rr.prefix + pattern
for _, fn := range rr.namedMiddleware { for _, fn := range rr.namedMiddleware {
h = append(h, fn(pattern)) h = append(h, fn(fullPattern))
} }
h = append(h, rr.subfixHandlers...) h = append(h, rr.subfixHandlers...)
h = append(h, handlers...) h = append(h, handlers...)
for _, r := range rr.routes { for _, r := range rr.routes {
if r.pattern == rr.prefix+pattern && r.method == method { if r.pattern == fullPattern && r.method == method {
panic("cannot add duplicate route") panic("cannot add duplicate route")
} }
} }
rr.routes = append(rr.routes, route{ rr.routes = append(rr.routes, route{
method: method, method: method,
pattern: rr.prefix + pattern, pattern: fullPattern,
handlers: h, handlers: h,
}) })
} }
......
...@@ -214,8 +214,13 @@ func TestNamedMiddlewareRouteRegister(t *testing.T) { ...@@ -214,8 +214,13 @@ func TestNamedMiddlewareRouteRegister(t *testing.T) {
{method: "GET", pattern: "/user/admin/all", handlers: emptyHandlers(5)}, {method: "GET", pattern: "/user/admin/all", handlers: emptyHandlers(5)},
} }
namedMiddlewares := map[string]bool{}
// Setup // Setup
rr := NewRouteRegister(emptyHandler) rr := NewRouteRegister(func(name string) macaron.Handler {
namedMiddlewares[name] = true
return struct{ name string }{name: name}
})
rr.Delete("/admin", emptyHandler("1")) rr.Delete("/admin", emptyHandler("1"))
rr.Get("/down", emptyHandler("1"), emptyHandler("2")) rr.Get("/down", emptyHandler("1"), emptyHandler("2"))
...@@ -247,6 +252,10 @@ func TestNamedMiddlewareRouteRegister(t *testing.T) { ...@@ -247,6 +252,10 @@ func TestNamedMiddlewareRouteRegister(t *testing.T) {
t.Errorf("want %s got %v", testTable[i].pattern, fr.route[i].pattern) t.Errorf("want %s got %v", testTable[i].pattern, fr.route[i].pattern)
} }
if _, exist := namedMiddlewares[testTable[i].pattern]; !exist {
t.Errorf("could not find named route named %s", testTable[i].pattern)
}
if len(testTable[i].handlers) != len(fr.route[i].handlers) { if len(testTable[i].handlers) != len(fr.route[i].handlers) {
t.Errorf("want %d handlers got %d handlers \ntestcase: %v\nroute: %v\n", t.Errorf("want %d handlers got %d handlers \ntestcase: %v\nroute: %v\n",
len(testTable[i].handlers), len(testTable[i].handlers),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment