mirror of https://github.com/gin-gonic/gin.git
fix(router): tree bug where loop index is not decremented. (#3460)
fixes https://github.com/gin-gonic/gin/issues/3459
This commit is contained in:
parent
c58e0d59ca
commit
8eb5f832ba
|
@ -670,3 +670,22 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
|
||||||
w := PerformRequest(router, http.MethodGet, "/not-found")
|
w := PerformRequest(router, http.MethodGet, "/not-found")
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
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)
|
||||||
|
}
|
||||||
|
|
18
tree.go
18
tree.go
|
@ -459,9 +459,9 @@ walk: // Outer loop for walking the tree
|
||||||
// If the path at the end of the loop is not equal to '/' and the current node has no child nodes
|
// If the path at the end of the loop is not equal to '/' and the current node has no child nodes
|
||||||
// the current node needs to roll back to last valid skippedNode
|
// the current node needs to roll back to last valid skippedNode
|
||||||
if path != "/" {
|
if path != "/" {
|
||||||
for l := len(*skippedNodes); l > 0; {
|
for length := len(*skippedNodes); length > 0; length-- {
|
||||||
skippedNode := (*skippedNodes)[l-1]
|
skippedNode := (*skippedNodes)[length-1]
|
||||||
*skippedNodes = (*skippedNodes)[:l-1]
|
*skippedNodes = (*skippedNodes)[:length-1]
|
||||||
if strings.HasSuffix(skippedNode.path, path) {
|
if strings.HasSuffix(skippedNode.path, path) {
|
||||||
path = skippedNode.path
|
path = skippedNode.path
|
||||||
n = skippedNode.node
|
n = skippedNode.node
|
||||||
|
@ -576,9 +576,9 @@ walk: // Outer loop for walking the tree
|
||||||
// If the current path does not equal '/' and the node does not have a registered handle and the most recently matched node has a child node
|
// If the current path does not equal '/' and the node does not have a registered handle and the most recently matched node has a child node
|
||||||
// the current node needs to roll back to last valid skippedNode
|
// the current node needs to roll back to last valid skippedNode
|
||||||
if n.handlers == nil && path != "/" {
|
if n.handlers == nil && path != "/" {
|
||||||
for l := len(*skippedNodes); l > 0; {
|
for length := len(*skippedNodes); length > 0; length-- {
|
||||||
skippedNode := (*skippedNodes)[l-1]
|
skippedNode := (*skippedNodes)[length-1]
|
||||||
*skippedNodes = (*skippedNodes)[:l-1]
|
*skippedNodes = (*skippedNodes)[:length-1]
|
||||||
if strings.HasSuffix(skippedNode.path, path) {
|
if strings.HasSuffix(skippedNode.path, path) {
|
||||||
path = skippedNode.path
|
path = skippedNode.path
|
||||||
n = skippedNode.node
|
n = skippedNode.node
|
||||||
|
@ -633,9 +633,9 @@ walk: // Outer loop for walking the tree
|
||||||
|
|
||||||
// roll back to last valid skippedNode
|
// roll back to last valid skippedNode
|
||||||
if !value.tsr && path != "/" {
|
if !value.tsr && path != "/" {
|
||||||
for l := len(*skippedNodes); l > 0; {
|
for length := len(*skippedNodes); length > 0; length-- {
|
||||||
skippedNode := (*skippedNodes)[l-1]
|
skippedNode := (*skippedNodes)[length-1]
|
||||||
*skippedNodes = (*skippedNodes)[:l-1]
|
*skippedNodes = (*skippedNodes)[:length-1]
|
||||||
if strings.HasSuffix(skippedNode.path, path) {
|
if strings.HasSuffix(skippedNode.path, path) {
|
||||||
path = skippedNode.path
|
path = skippedNode.path
|
||||||
n = skippedNode.node
|
n = skippedNode.node
|
||||||
|
|
Loading…
Reference in New Issue