Skip to content

Commit

Permalink
refs labstack#1412: Fix parent nodes for route insert on node split
Browse files Browse the repository at this point in the history
  • Loading branch information
lammel committed Oct 16, 2019
1 parent d266e4e commit 1377273
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 9 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
// Split node
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames)

// Update parent path for all children to new node
for _, child := range cn.children {
child.parent = n
}

// Reset parent node
cn.kind = skind
cn.label = cn.prefix[0]
Expand Down Expand Up @@ -417,6 +422,10 @@ func (r *Router) Find(method, path string, c Context) {
if np == nil {
break // no further parent nodes in tree, abort
}
var str strings.Builder
str.WriteString(nn.prefix)
str.WriteString(search)
search = str.String()
nn = np
}
if cn != nil { // use the found "any" route and update path
Expand Down
32 changes: 30 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func TestRouterMatchAnyMultiLevel(t *testing.T) {
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "noapi/users/jim", c.Param("*"))
}
func TestRouterMatchAnyMultiLevel2(t *testing.T) {
func TestRouterMatchAnyMultiLevelWithPost(t *testing.T) {
e := New()
r := e.router
handler := func(c Context) error {
Expand All @@ -649,18 +649,46 @@ func TestRouterMatchAnyMultiLevel2(t *testing.T) {
}

// Routes
// e.GET("/api/auth/login", handler)
e.POST("/api/auth/login", handler)
e.POST("/api/auth/forgotPassword", handler)
e.Any("/api/*", handler)
e.Any("/*", handler)

// POST /api/auth/login shall choose login method
c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/auth/login", c)
c.handler(c)
assert.Equal(t, "/api/auth/login", c.Get("path"))
assert.Equal(t, "", c.Param("*"))

// GET /api/auth/login shall choose any route
// c = e.NewContext(nil, nil).(*context)
// r.Find(http.MethodGet, "/api/auth/login", c)
// c.handler(c)
// assert.Equal(t, "/api/*", c.Get("path"))
// assert.Equal(t, "auth/login", c.Param("*"))

// POST /api/auth/logout shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/auth/logout", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "auth/logout", c.Param("*"))

// POST to /api/other/test shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/other/test", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "other/test", c.Param("*"))

// GET to /api/other/test shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/api/other/test", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "other/test", c.Param("*"))

}

func TestRouterMicroParam(t *testing.T) {
Expand Down

0 comments on commit 1377273

Please sign in to comment.