Skip to content

Commit

Permalink
tree: Fix bug where loop index is not decremented.
Browse files Browse the repository at this point in the history
Also add test to validate fix.

fixes #3459

Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
  • Loading branch information
kristiansvalland committed Jan 3, 2023
1 parent 79a61b9 commit f4dfe65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions routes_test.go
Expand Up @@ -670,3 +670,22 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
w := PerformRequest(router, http.MethodGet, "/not-found")
assert.Equal(t, http.StatusNotFound, w.Code)
}

func TestEngineHandleMethodNotAllowedCornerCase(t *testing.T) {
r := New()
r.HandleMethodNotAllowed = true

base := r.Group("base")
base.GET("/metrics", handlerTest1)

v1 := base.Group("v1")

v1.GET("/:id/devices", handlerTest1)
v1.GET("/user/:id/groups", handlerTest1)

v1.GET("/orgs/:id", handlerTest1)
v1.DELETE("/orgs/:id", handlerTest1)

w := PerformRequest(r, "GET", "/base/v1/user/groups")
assert.Equal(t, http.StatusNotFound, w.Code)
}
3 changes: 3 additions & 0 deletions tree.go
Expand Up @@ -462,6 +462,7 @@ walk: // Outer loop for walking the tree
for l := len(*skippedNodes); l > 0; {
skippedNode := (*skippedNodes)[l-1]
*skippedNodes = (*skippedNodes)[:l-1]
l--
if strings.HasSuffix(skippedNode.path, path) {
path = skippedNode.path
n = skippedNode.node
Expand Down Expand Up @@ -579,6 +580,7 @@ walk: // Outer loop for walking the tree
for l := len(*skippedNodes); l > 0; {
skippedNode := (*skippedNodes)[l-1]
*skippedNodes = (*skippedNodes)[:l-1]
l--
if strings.HasSuffix(skippedNode.path, path) {
path = skippedNode.path
n = skippedNode.node
Expand Down Expand Up @@ -636,6 +638,7 @@ walk: // Outer loop for walking the tree
for l := len(*skippedNodes); l > 0; {
skippedNode := (*skippedNodes)[l-1]
*skippedNodes = (*skippedNodes)[:l-1]
l--
if strings.HasSuffix(skippedNode.path, path) {
path = skippedNode.path
n = skippedNode.node
Expand Down

0 comments on commit f4dfe65

Please sign in to comment.