diff --git a/curly.go b/curly.go index bb8d3c0..6fd2bcd 100644 --- a/curly.go +++ b/curly.go @@ -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 { diff --git a/curly_test.go b/curly_test.go index d42eed3..8a16c38 100644 --- a/curly_test.go +++ b/curly_test.go @@ -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