Skip to content

Commit

Permalink
fix misroute when dealing multiple webservice with regex (#549)
Browse files Browse the repository at this point in the history
* better naming for routing related variable

* extra score on regularMatchesPathToken when detectWebServices

* add unittest

* little tweaks

* prefix each for vars in for.loop

* rebase

* code lint
  • Loading branch information
haitch committed May 28, 2024
1 parent 77fc969 commit e5fed1c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit e5fed1c

Please sign in to comment.