Skip to content

Commit

Permalink
fix #168
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Sep 12, 2019
1 parent 70dcbcc commit 28fbf72
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
44 changes: 16 additions & 28 deletions pkg/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,52 +172,40 @@ func (r *Route) Find(url []byte, method string, paramsFunc func(name, value []by

var matchAllParams bytes.Buffer
nodes = removeSlash(nodes...)
target := r.root
target := []*routeItem{r.root}
var matchesItem *routeItem
matchedIdx := 0
for idx, node := range nodes {
if target.urlMatches(node, &matchAllParams) {
matchedIdx = idx
if target.node.hasArg {
if paramsFunc != nil {
paramsFunc(target.node.argName, node.value)
}
}

continue
}

matched := false
for _, item := range target.children {
OUTER:
for idx, node := range nodes {
for _, item := range target {
if item.urlMatches(node, &matchAllParams) {
target = item
matched = true
matchedIdx = idx
matchesItem = item
if item.node.hasArg && paramsFunc != nil {
paramsFunc(item.node.argName, node.value)
}

if item.node.hasArg {
if paramsFunc != nil {
paramsFunc(item.node.argName, node.value)
}
if !item.node.isMatchAllConstString() {
target = item.children // find in children
}
break
}
}

if !matched {
break
continue OUTER
}
}
}

if matchedIdx == len(nodes)-1 {
if target.apis == nil {
if matchesItem.apis == nil {
return 0, false
}

if paramsFunc != nil && len(matchAllParams.Bytes()) > 0 {
paramsFunc(matchAll, matchAllParams.Bytes()[1:])
}
if id, ok := target.apis[method]; ok {
if id, ok := matchesItem.apis[method]; ok {
return id, true
} else if id, ok := target.apis["*"]; ok {
} else if id, ok := matchesItem.apis["*"]; ok {
return id, true
}

Expand Down
40 changes: 39 additions & 1 deletion pkg/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ func TestAddWithMethod(t *testing.T) {
}
}

func TestMatchesSame(t *testing.T) {
r := NewRoute()
err := r.Add(&metapb.API{
ID: 1,
URLPattern: "/(string):a/(string):b/(string):c",
Method: "*",
})
if err != nil {
t.Errorf("add error")
}

params := make(map[string]string)
id, _ := r.Find([]byte("/a/b/c"), "GET", func(name, value []byte) {
params[string(name)] = string(value)
})
if id != 1 {
t.Errorf("expect matched 1, but %d", id)
}

if params["a"] != "a" {
t.Errorf("expect param value a, but %s", params["a"])
}
if params["b"] != "b" {
t.Errorf("expect param value b, but %s", params["b"])
}
if params["c"] != "c" {
t.Errorf("expect param value c, but %s", params["c"])
}
}

func TestAdd(t *testing.T) {
r := NewRoute()
err := r.Add(&metapb.API{
Expand Down Expand Up @@ -162,10 +192,18 @@ func TestFindWithStar(t *testing.T) {
Method: "*",
})

id, _ := r.Find([]byte("/users/1"), "GET", nil)
params := make(map[string]string)
id, _ := r.Find([]byte("/users/1"), "GET", func(name, value []byte) {
params[string(name)] = string(value)
})

if id != 1 {
t.Errorf("expect matched 1, but %d", id)
}

if params["*"] != "users/1" {
t.Errorf("expect param value users/1, but %s", params["*"])
}
}

func TestFindMatchAll(t *testing.T) {
Expand Down

0 comments on commit 28fbf72

Please sign in to comment.