Skip to content

Commit

Permalink
Fix #225 fix route match error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Jun 30, 2021
1 parent 94a119c commit 296f649
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ func (r *Route) Find(url []byte, method string, paramsFunc func(name, value []by

OUTER:
for idx, node := range nodes {
ok := false
for _, item := range target {
if item.urlMatches(node, &matchAllParams) {
ok = item.urlMatches(node, &matchAllParams)
log.Infof(">>>>>>>>>> %s matches %s: %+v",
string(item.node.value),
string(node.value),
ok)
if ok {
matchedIdx = idx
matchesItem = item
if item.node.hasArg && paramsFunc != nil {
Expand All @@ -193,6 +199,11 @@ OUTER:
continue OUTER
}
}
log.Infof("check next item")

if !ok {
return 0, false
}
}

if matchedIdx == len(nodes)-1 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/fagongzi/gateway/pkg/pb/metapb"
"github.com/stretchr/testify/assert"
)

func TestAddWithMethod(t *testing.T) {
Expand Down Expand Up @@ -337,3 +338,24 @@ func TestFind(t *testing.T) {
t.Errorf("expect not matched , but %d", id)
}
}

func TestIssue225(t *testing.T) {
domain1 := "baidu.com"
params := make(map[string][]byte)
paramsFunc := func(name, value []byte) {
params[string(name)] = value
}

r := NewRoute()
r.Add(&metapb.API{
ID: 1,
MatchRule: metapb.MatchAll,
URLPattern: "/a/b/c",
Method: "POST",
Domain: domain1,
})

id, ok := r.Find([]byte("/a/b/d/c"), "POST", paramsFunc)
assert.False(t, ok)
assert.Equal(t, uint64(0), id)
}

0 comments on commit 296f649

Please sign in to comment.