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 misroute when dealing multiple webservice with regex #549

Merged
merged 9 commits into from
May 28, 2024
10 changes: 9 additions & 1 deletion curly.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ func (c CurlyRouter) computeWebserviceScore(requestTokens []string, routeTokens
if len(eachRequestToken) == 0 {
return false, score
}
score += 1
score++

if colon := strings.Index(eachRouteToken, ":"); colon != -1 {
// match by regex
matchesToken, _ := c.regularMatchesPathToken(eachRouteToken, colon, eachRequestToken)
if matchesToken {
score++ // extra score for regex match
}
}
} else {
// not a parameter
if eachRequestToken != eachRouteToken {
Expand Down
20 changes: 20 additions & 0 deletions curly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ func Test_detectWebService(t *testing.T) {
}
}

func Test_detectWebServiceWithRegexPath(t *testing.T) {
router := CurlyRouter{}
holaWS := new(WebService).Path("/{:hola}")
helloWS := new(WebService).Path("/{:hello}")

var wss = []*WebService{holaWS, helloWS}

holaJuanInTokens := tokenizePath("/hola/juan")
selected := router.detectWebService(holaJuanInTokens, wss)
if selected != holaWS {
t.Fatalf("expected holaWS, got %v", selected.rootPath)
}

helloJuanInTokens := tokenizePath("/hello/juan")
selected = router.detectWebService(helloJuanInTokens, wss)
if selected != helloWS {
t.Fatalf("expected helloWS, got %v", selected.rootPath)
}
}

var routeMatchers = []struct {
route string
path string
Expand Down