Commit 4a46dd88 by bergquist

routing: raise panic if duplicate routes are added

parent dbfafa1c
...@@ -129,6 +129,12 @@ func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handl ...@@ -129,6 +129,12 @@ func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handl
h = append(h, rr.subfixHandlers...) h = append(h, rr.subfixHandlers...)
h = append(h, handlers...) h = append(h, handlers...)
for _, r := range rr.routes {
if r.pattern == rr.prefix+pattern && r.method == method {
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: rr.prefix + pattern,
......
...@@ -193,6 +193,23 @@ func TestRouteGroupInserting(t *testing.T) { ...@@ -193,6 +193,23 @@ func TestRouteGroupInserting(t *testing.T) {
} }
} }
func TestDuplicateRoutShouldPanic(t *testing.T) {
defer func() {
if recover() != "cannot add duplicate route" {
t.Errorf("Should cause panic if duplicate routes are added ")
}
}()
rr := NewRouteRegister(func(name string) macaron.Handler {
return emptyHandler(name)
})
rr.Get("/api", emptyHandler("1"))
rr.Get("/api", emptyHandler("1"))
fr := &fakeRouter{}
rr.Register(fr)
}
func TestNamedMiddlewareRouteRegister(t *testing.T) { func TestNamedMiddlewareRouteRegister(t *testing.T) {
testTable := []route{ testTable := []route{
{method: "DELETE", pattern: "/admin", handlers: emptyHandlers(2)}, {method: "DELETE", pattern: "/admin", handlers: emptyHandlers(2)},
......
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