1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 19:53:19 +00:00

🚀 improve routing behavior

This commit is contained in:
ReneWerner87 2020-08-07 14:38:40 +02:00
parent 582b10a6d4
commit 2283fd2756
2 changed files with 24 additions and 21 deletions

View File

@ -256,7 +256,11 @@ func findParamLen(s string, segments []routeSegment, currIndex int) int {
}
// get the length to the next constant part
if false == nextSeg.IsParam {
if constPosition := strings.Index(s, nextSeg.Const); constPosition != -1 {
searchString := nextSeg.Const
if len(searchString) > 1 {
searchString = utils.TrimRight(nextSeg.Const, slashDelimiter)
}
if constPosition := strings.Index(s, searchString); constPosition != -1 {
return constPosition
}
}

View File

@ -128,12 +128,11 @@ func Test_Path_matchParams(t *testing.T) {
{url: "/foobar", params: []string{""}, match: true},
{url: "/", params: []string{""}, match: false},
})
// TODO: fix it
//testCase("/a*cde*g/", []testparams{
// {url: "/abbbcdefffg", params: []string{"bbb", "fff"}, match: true},
// {url: "/acdeg", params: []string{"", ""}, match: true},
// {url: "/", params: nil, match: false},
//})
testCase("/a*cde*g/", []testparams{
{url: "/abbbcdefffg", params: []string{"bbb", "fff"}, match: true},
{url: "/acdeg", params: []string{"", ""}, match: true},
{url: "/", params: nil, match: false},
})
testCase("/*v1*/proxy", []testparams{
{url: "/customer/v1/cart/proxy", params: []string{"customer/", "/cart"}, match: true},
{url: "/v1/proxy", params: []string{"", ""}, match: true},
@ -162,7 +161,7 @@ func Test_Path_matchParams(t *testing.T) {
testCase("/api/v1/:param/abc/*", []testparams{
{url: "/api/v1/well/abc/wildcard", params: []string{"well", "wildcard"}, match: true},
{url: "/api/v1/well/abc/", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/abc", params: nil, match: false},
{url: "/api/v1/well/abc", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/ttt", params: nil, match: false},
})
testCase("/api/:day/:month?/:year?", []testparams{
@ -235,19 +234,19 @@ func Test_Path_matchParams(t *testing.T) {
{url: "xyz/", params: nil, match: false},
})
// TODO: fix this
//testCase("/api/*/:param?", []testparams{
// {url: "/api/", params: []string{"", ""}, match: true},
// {url: "/api/joker", params: []string{"joker", ""}, match: true},
// {url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
// {url: "/api/joker//batman", params: []string{"joker//batman", "batman"}, match: true},
// {url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
// {url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
// {url: "/api/joker/batman/robin/1/", params: []string{"joker/batman/robin/1", ""}, match: true},
// {url: "/api/joker-batman/robin/1", params: []string{"joker-batman/robin", "1"}, match: true},
// {url: "/api/joker-batman-robin/1", params: []string{"joker-batman-robin", "1"}, match: true},
// {url: "/api/joker-batman-robin-1", params: []string{"joker-batman-robin-1", ""}, match: true},
// {url: "/api", params: []string{"", ""}, match: true},
//})
testCase("/api/*/:param?", []testparams{
{url: "/api/", params: []string{"", ""}, match: true},
{url: "/api/joker", params: []string{"joker", ""}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
//{url: "/api/joker//batman", params: []string{"joker//batman", "batman"}, match: true},
//{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
//{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
//{url: "/api/joker/batman/robin/1/", params: []string{"joker/batman/robin/1", ""}, match: true},
//{url: "/api/joker-batman/robin/1", params: []string{"joker-batman/robin", "1"}, match: true},
//{url: "/api/joker-batman-robin/1", params: []string{"joker-batman-robin", "1"}, match: true},
//{url: "/api/joker-batman-robin-1", params: []string{"joker-batman-robin-1", ""}, match: true},
{url: "/api", params: []string{"", ""}, match: true},
})
//testCase("/api/*/:param", []testparams{
// {url: "/api/test/abc", params: []string{"test", "abc"}, match: true},
// {url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},