Skip to content

Commit e305e21

Browse files
thinkerouappleboy
authored andcommitted
fix(route): redirectSlash bug (#3227)
fixes #2959 fixes #2282 fixes #2211
1 parent ed049dd commit e305e21

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

tree.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ func countSections(path string) uint16 {
107107
type nodeType uint8
108108

109109
const (
110-
root nodeType = iota + 1
110+
static nodeType = iota
111+
root
111112
param
112113
catchAll
113114
)
@@ -173,6 +174,7 @@ walk:
173174
child := node{
174175
path: n.path[i:],
175176
wildChild: n.wildChild,
177+
nType: static,
176178
indices: n.indices,
177179
children: n.children,
178180
handlers: n.handlers,
@@ -604,6 +606,11 @@ walk: // Outer loop for walking the tree
604606
return
605607
}
606608

609+
if path == "/" && n.nType == static {
610+
value.tsr = true
611+
return
612+
}
613+
607614
// No handle found. Check if a handle for this path + a
608615
// trailing slash exists for trailing slash recommendation
609616
for i, c := range []byte(n.indices) {

tree_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,26 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) {
684684
}
685685
}
686686

687+
func TestRedirectTrailingSlash(t *testing.T) {
688+
var data = []struct {
689+
path string
690+
}{
691+
{"/hello/:name"},
692+
{"/hello/:name/123"},
693+
{"/hello/:name/234"},
694+
}
695+
696+
node := &node{}
697+
for _, item := range data {
698+
node.addRoute(item.path, fakeHandler("test"))
699+
}
700+
701+
value := node.getValue("/hello/abx/", nil, getSkippedNodes(), false)
702+
if value.tsr != true {
703+
t.Fatalf("want true, is false")
704+
}
705+
}
706+
687707
func TestTreeFindCaseInsensitivePath(t *testing.T) {
688708
tree := &node{}
689709

0 commit comments

Comments
 (0)