Skip to content

Commit

Permalink
Not add a trailing slash in the optional paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Apr 11, 2020
1 parent 3a4e181 commit dd25891
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
10 changes: 3 additions & 7 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,6 @@ walk:
}

end++
if len(path) > start+end && path[start+end] == '/' {
// Include trailing slash for a better lookup
end++
}

newPath += path[questionMarkIndex+1 : start+end]

path = path[:questionMarkIndex] + path[questionMarkIndex+1:] // remove '?'
Expand All @@ -198,8 +193,9 @@ walk:
newPath += path[:questionMarkIndex]

// include the path without the wildcard
if !gotils.StringSliceInclude(paths, path[:start-1]) {
paths = append(paths, path[:start-1])
// -2 due to remove the '/' and '{'
if !gotils.StringSliceInclude(paths, path[:start-2]) {
paths = append(paths, path[:start-2])
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ func TestGetOptionalPath(t *testing.T) {
tsr bool
handler fasthttp.RequestHandler
}{
{"/show/{name}", true, nil},
{"/show/{name}/", false, handler},
{"/show/{name}/{surname}", true, nil},
{"/show/{name}/{surname}/", false, handler},
{"/show/{name}/{surname}/at", true, nil},
{"/show/{name}/{surname}/at/", false, handler},
{"/show/{name}/{surname}/at/{address}", true, nil},
{"/show/{name}/{surname}/at/{address}/", false, handler},
{"/show/{name}/{surname}/at/{address}/{id}", true, nil},
{"/show/{name}/{surname}/at/{address}/{id}/", false, handler},
{"/show/{name}", false, handler},
{"/show/{name}/", true, nil},
{"/show/{name}/{surname}", false, handler},
{"/show/{name}/{surname}/", true, nil},
{"/show/{name}/{surname}/at", false, handler},
{"/show/{name}/{surname}/at/", true, nil},
{"/show/{name}/{surname}/at/{address}", false, handler},
{"/show/{name}/{surname}/at/{address}/", true, nil},
{"/show/{name}/{surname}/at/{address}/{id}", false, handler},
{"/show/{name}/{surname}/at/{address}/{id}/", true, nil},
{"/show/{name}/{surname}/at/{address}/{id}/{phone:.*}", false, handler},
{"/show/{name}/{surname}/at/{address}/{id}/{phone:.*}/", true, nil},
}
Expand Down

0 comments on commit dd25891

Please sign in to comment.