diff --git a/gin.go b/gin.go index 30fa5ac536..159646a4ec 100644 --- a/gin.go +++ b/gin.go @@ -265,7 +265,7 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { root := engine.trees.get(method) if root == nil { root = new(node) - // root.fullPath = "/" + root.fullPath = "/" engine.trees = append(engine.trees, methodTree{method: method, root: root}) } root.addRoute(path, handlers) @@ -448,7 +448,7 @@ func (engine *Engine) handleHTTPRequest(c *Context) { // if value.params != nil { // c.Params = *value.params // } - // c.fullPath = value.fullPath + c.fullPath = value.fullPath c.Next() c.writermem.WriteHeaderNow() return diff --git a/routes_test.go b/routes_test.go index d6c513379f..360ade6215 100644 --- a/routes_test.go +++ b/routes_test.go @@ -579,42 +579,42 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) { assert.Equal(t, 0, w.Body.Len()) } -// func TestRouteContextHoldsFullPath(t *testing.T) { -// router := New() - -// // Test routes -// routes := []string{ -// "/simple", -// "/project/:name", -// "/", -// "/news/home", -// "/news", -// "/simple-two/one", -// "/simple-two/one-two", -// "/project/:name/build/*params", -// "/project/:name/bui", -// } - -// for _, route := range routes { -// actualRoute := route -// router.GET(route, func(c *Context) { -// // For each defined route context should contain its full path -// assert.Equal(t, actualRoute, c.FullPath()) -// c.AbortWithStatus(http.StatusOK) -// }) -// } - -// for _, route := range routes { -// w := performRequest(router, http.MethodGet, route) -// assert.Equal(t, http.StatusOK, w.Code) -// } - -// // Test not found -// router.Use(func(c *Context) { -// // For not found routes full path is empty -// assert.Equal(t, "", c.FullPath()) -// }) - -// w := performRequest(router, http.MethodGet, "/not-found") -// assert.Equal(t, http.StatusNotFound, w.Code) -// } +func TestRouteContextHoldsFullPath(t *testing.T) { + router := New() + + // Test routes + routes := []string{ + "/simple", + "/project/:name", + "/", + "/news/home", + "/news", + "/simple-two/one", + "/simple-two/one-two", + "/project/:name/build/*params", + "/project/:name/bui", + } + + for _, route := range routes { + actualRoute := route + router.GET(route, func(c *Context) { + // For each defined route context should contain its full path + assert.Equal(t, actualRoute, c.FullPath()) + c.AbortWithStatus(http.StatusOK) + }) + } + + for _, route := range routes { + w := performRequest(router, http.MethodGet, route) + assert.Equal(t, http.StatusOK, w.Code) + } + + // Test not found + router.Use(func(c *Context) { + // For not found routes full path is empty + assert.Equal(t, "", c.FullPath()) + }) + + w := performRequest(router, http.MethodGet, "/not-found") + assert.Equal(t, http.StatusNotFound, w.Code) +} diff --git a/tree.go b/tree.go index 74481be815..0a6ad013d4 100644 --- a/tree.go +++ b/tree.go @@ -101,7 +101,7 @@ type node struct { nType nodeType maxParams uint16 wildChild bool - // fullPath string + fullPath string } // increments priority of the given child and reorders if necessary. @@ -141,7 +141,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) { return } - // parentFullPathIndex := 0 + parentFullPathIndex := 0 walk: for { @@ -164,7 +164,7 @@ walk: children: n.children, handlers: n.handlers, priority: n.priority - 1, - // fullPath: n.fullPath, + fullPath: n.fullPath, } // Update maxParams (max of all children) @@ -180,7 +180,7 @@ walk: n.path = path[:i] n.handlers = nil n.wildChild = false - // n.fullPath = fullPath[:parentFullPathIndex+i] + n.fullPath = fullPath[:parentFullPathIndex+i] } // Make new node a child of this node @@ -188,7 +188,7 @@ walk: path = path[i:] if n.wildChild { - // parentFullPathIndex += len(n.path) + parentFullPathIndex += len(n.path) n = n.children[0] n.priority++ @@ -222,7 +222,7 @@ walk: // slash after param if n.nType == param && c == '/' && len(n.children) == 1 { - // parentFullPathIndex += len(n.path) + parentFullPathIndex += len(n.path) n = n.children[0] n.priority++ continue walk @@ -231,7 +231,7 @@ walk: // Check if a child with the next path byte exists for i, max := 0, len(n.indices); i < max; i++ { if c == n.indices[i] { - // parentFullPathIndex += len(n.path) + parentFullPathIndex += len(n.path) i = n.incrementChildPrio(i) n = n.children[i] continue walk @@ -244,7 +244,7 @@ walk: n.indices += string([]byte{c}) child := &node{ // maxParams: numParams, - // fullPath: fullPath, + fullPath: fullPath, } n.children = append(n.children, child) n.incrementChildPrio(len(n.indices) - 1) @@ -326,7 +326,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) nType: param, path: wildcard, // maxParams: numParams, - // fullPath: fullPath, + fullPath: fullPath, } n.children = []*node{child} n = child @@ -341,7 +341,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) child := &node{ // maxParams: numParams, priority: 1, - // fullPath: fullPath, + fullPath: fullPath, } n.children = []*node{child} n = child @@ -375,7 +375,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) wildChild: true, nType: catchAll, // maxParams: 1, - // fullPath: fullPath, + fullPath: fullPath, } // update maxParams of the parent node // if n.maxParams < 1 { @@ -393,7 +393,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) handlers: handlers, priority: 1, // maxParams: 1, - // fullPath: fullPath, + fullPath: fullPath, } n.children = []*node{child} @@ -403,7 +403,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) // If no wildcard was found, simply insert the path and handle n.path = path n.handlers = handlers - // n.fullPath = fullPath + n.fullPath = fullPath } // nodeValue holds return values of (*Node).getValue method @@ -411,7 +411,7 @@ type nodeValue struct { handlers HandlersChain params *Params tsr bool - // fullPath string + fullPath string } // getValue returns the handle registered with the given path (key). The values of @@ -505,7 +505,7 @@ walk: // Outer loop for walking the tree } if value.handlers = n.handlers; value.handlers != nil { - // value.fullPath = n.fullPath + value.fullPath = n.fullPath return } if len(n.children) == 1 { @@ -554,7 +554,7 @@ walk: // Outer loop for walking the tree } value.handlers = n.handlers - // value.fullPath = n.fullPath + value.fullPath = n.fullPath return default: @@ -565,7 +565,7 @@ walk: // Outer loop for walking the tree // We should have reached the node containing the handle. // Check if this node has a handle registered. if value.handlers = n.handlers; value.handlers != nil { - // value.fullPath = n.fullPath + value.fullPath = n.fullPath return }