🐛 Fix wrong route matching for not greedy parameter (#1017)

* 🐛 Fix wrong route matching for not greedy parameter
#1016

Co-authored-by: Fenny <fenny@gofiber.io>
pull/1022/head
RW 2020-11-13 16:27:23 +01:00 committed by GitHub
parent df78ede567
commit 6ffc89f18b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -287,6 +287,11 @@ func findParamLen(s string, segment *routeSegment) int {
return constPosition
}
} else if constPosition := strings.Index(s, segment.ComparePart); constPosition != -1 {
// if the compare part was found, but contains a slash although this part is not greedy, then it must not match
// example: /api/:param/fixedEnd -> path: /api/123/456/fixedEnd = no match , /api/123/fixedEnd = match
if !segment.IsGreedy && strings.IndexByte(s[:constPosition], slashDelimiter) != -1 {
return 0
}
return constPosition
}

View File

@ -183,6 +183,10 @@ func Test_Path_matchParams(t *testing.T) {
{url: "/api/v1/", params: nil, match: false},
{url: "/api/v1/something", params: nil, match: false},
})
testCase("/api/:param/fixedEnd", []testparams{
{url: "/api/abc/fixedEnd", params: []string{"abc"}, match: true},
{url: "/api/abc/def/fixedEnd", params: nil, match: false},
})
testCase("/shop/product/::filter/color::color/size::size", []testparams{
{url: "/shop/product/:test/color:blue/size:xs", params: []string{"test", "blue", "xs"}, match: true},
{url: "/shop/product/test/color:blue/size:xs", params: nil, match: false},
@ -430,6 +434,10 @@ func Benchmark_Path_matchParams(t *testing.B) {
}
}
benchCase("/api/:param/fixedEnd", []testparams{
{url: "/api/abc/fixedEnd", params: []string{"abc"}, match: true},
{url: "/api/abc/def/fixedEnd", params: nil, match: false},
})
benchCase("/api/v1/:param/*", []testparams{
{url: "/api/v1/entity", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/", params: []string{"entity", ""}, match: true},