Skip to content

Commit 91fd40b

Browse files
committed
fix(radix/node): tsr for childs that becomes a final path
1 parent b5e63c0 commit 91fd40b

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

radix/node.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ func (n *node) setHandler(handler fasthttp.RequestHandler, fullPath string) (*no
127127
}
128128

129129
if n.path != "/" && !foundTSR {
130-
childTSR := newNode("/")
131-
childTSR.tsr = true
132-
n.children = append(n.children, childTSR)
130+
if strings.HasSuffix(n.path, "/") {
131+
n.split(len(n.path) - 1)
132+
n.tsr = true
133+
} else {
134+
childTSR := newNode("/")
135+
childTSR.tsr = true
136+
n.children = append(n.children, childTSR)
137+
}
133138
}
134139

135140
return n, nil

radix/node_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
366366
"/no/a",
367367
"/no/b",
368368
"/api/hello/{name}",
369+
"/foo/data/hello",
370+
"/foo/",
369371
}
370372
for _, route := range routes {
371373
recv := catchPanic(func() {
@@ -391,6 +393,8 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
391393
"/admin/config/",
392394
"/admin/config/permissions/",
393395
"/doc/",
396+
"/foo/data/hello/",
397+
"/foo",
394398
}
395399
for _, route := range tsrRoutes {
396400
handler, tsr := tree.Get(route, nil)

radix/tree_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ func Test_Tree(t *testing.T) {
154154
},
155155
},
156156
},
157+
{
158+
args: args{
159+
path: "/data/orders",
160+
reqPath: "/data/orders/",
161+
handler: generateHandler(),
162+
},
163+
want: want{
164+
tsr: true,
165+
params: nil,
166+
},
167+
},
168+
{
169+
args: args{
170+
path: "/data/",
171+
reqPath: "/data",
172+
handler: generateHandler(),
173+
},
174+
want: want{
175+
tsr: true,
176+
params: nil,
177+
},
178+
},
157179
}
158180

159181
tree := New()

0 commit comments

Comments
 (0)