Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route): redirectSlash bug #3227

Merged
merged 2 commits into from Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion tree.go
Expand Up @@ -107,7 +107,8 @@ func countSections(path string) uint16 {
type nodeType uint8

const (
root nodeType = iota + 1
static nodeType = iota
root
param
catchAll
)
Expand Down Expand Up @@ -173,6 +174,7 @@ walk:
child := node{
path: n.path[i:],
wildChild: n.wildChild,
nType: static,
indices: n.indices,
children: n.children,
handlers: n.handlers,
Expand Down Expand Up @@ -604,6 +606,11 @@ walk: // Outer loop for walking the tree
return
}

if path == "/" && n.nType == static {
value.tsr = true
return
}

// No handle found. Check if a handle for this path + a
// trailing slash exists for trailing slash recommendation
for i, c := range []byte(n.indices) {
Expand Down
20 changes: 20 additions & 0 deletions tree_test.go
Expand Up @@ -684,6 +684,26 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) {
}
}

func TestRedirectTrailingSlash(t *testing.T) {
var data = []struct {
path string
}{
{"/hello/:name"},
{"/hello/:name/123"},
{"/hello/:name/234"},
}

node := &node{}
for _, item := range data {
node.addRoute(item.path, fakeHandler("test"))
}

value := node.getValue("/hello/abx/", nil, getSkippedNodes(), false)
if value.tsr != true {
t.Fatalf("want true, is false")
}
}

func TestTreeFindCaseInsensitivePath(t *testing.T) {
tree := &node{}

Expand Down