Skip to content

Commit

Permalink
fix(radix/node): tsr for childs that becomes a final path
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Feb 27, 2023
1 parent b5e63c0 commit 91fd40b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
11 changes: 8 additions & 3 deletions radix/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ func (n *node) setHandler(handler fasthttp.RequestHandler, fullPath string) (*no
}

if n.path != "/" && !foundTSR {
childTSR := newNode("/")
childTSR.tsr = true
n.children = append(n.children, childTSR)
if strings.HasSuffix(n.path, "/") {
n.split(len(n.path) - 1)
n.tsr = true
} else {
childTSR := newNode("/")
childTSR.tsr = true
n.children = append(n.children, childTSR)
}
}

return n, nil
Expand Down
4 changes: 4 additions & 0 deletions radix/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/no/a",
"/no/b",
"/api/hello/{name}",
"/foo/data/hello",
"/foo/",
}
for _, route := range routes {
recv := catchPanic(func() {
Expand All @@ -391,6 +393,8 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/admin/config/",
"/admin/config/permissions/",
"/doc/",
"/foo/data/hello/",
"/foo",
}
for _, route := range tsrRoutes {
handler, tsr := tree.Get(route, nil)
Expand Down
22 changes: 22 additions & 0 deletions radix/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ func Test_Tree(t *testing.T) {
},
},
},
{
args: args{
path: "/data/orders",
reqPath: "/data/orders/",
handler: generateHandler(),
},
want: want{
tsr: true,
params: nil,
},
},
{
args: args{
path: "/data/",
reqPath: "/data",
handler: generateHandler(),
},
want: want{
tsr: true,
params: nil,
},
},
}

tree := New()
Expand Down

0 comments on commit 91fd40b

Please sign in to comment.