Skip to content

Commit

Permalink
fix route match (#40)
Browse files Browse the repository at this point in the history
* fix route match

* fix route match

* update version

* update version
  • Loading branch information
abahmed committed Jun 15, 2020
1 parent c44f047 commit ed6fbee
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gearbox.go
Expand Up @@ -12,7 +12,7 @@ import (

// Exported constants
const (
Version = "1.0.0" // Version of gearbox
Version = "1.0.1" // Version of gearbox
Name = "Gearbox" // Name of gearbox
// http://patorjk.com/software/taag/#p=display&f=Big%20Money-ne&t=Gearbox
banner = `
Expand Down
6 changes: 4 additions & 2 deletions router.go
Expand Up @@ -320,7 +320,6 @@ func matchEndpointParams(ep *endpoint, paths [][]byte, pathIndex int) (tst, bool
endpointParamsLen := len(endpointParams)
pathsLen := len(paths)

//matched := false
paramIdx := 0
for paramIdx < endpointParamsLen {
if endpointParams[paramIdx].Type == ptMatchAll {
Expand All @@ -338,7 +337,6 @@ func matchEndpointParams(ep *endpoint, paths [][]byte, pathIndex int) (tst, bool
return nil, false
}

//
if len(paths[pathIndex]) == 0 {
pathIndex++
continue
Expand All @@ -358,6 +356,10 @@ func matchEndpointParams(ep *endpoint, paths [][]byte, pathIndex int) (tst, bool
pathIndex++
}

for pathIndex < pathsLen && len(paths[pathIndex]) == 0 {
pathIndex++
}

// There is more parts, so no match
if pathsLen-pathIndex > 0 {
return nil, false
Expand Down
2 changes: 2 additions & 0 deletions router_test.go
Expand Up @@ -299,6 +299,7 @@ func TestConstructRoutingTree(t *testing.T) {
{method: []byte(MethodGet), path: []byte("/account/:name?"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/profile/:name:([a-z]+)?"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/order/:name1/:name2:([a-z]+)?"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/"), handler: emptyHandlersChain},
}

// register routes
Expand Down Expand Up @@ -346,6 +347,7 @@ func TestConstructRoutingTree(t *testing.T) {
{method: []byte(MethodGet), path: []byte("/order/test1"), match: true, params: map[string]string{"name1": "test1"}},
{method: []byte(MethodGet), path: []byte("/order/test1/test2/"), match: true, params: map[string]string{"name1": "test1", "name2": "test2"}},
{method: []byte(MethodPut), path: []byte("/order/test1/test2/test3"), match: false, params: make(map[string]string)},
{method: []byte(MethodGet), path: []byte("/"), match: true, params: make(map[string]string)},
}

// test matching routes
Expand Down

0 comments on commit ed6fbee

Please sign in to comment.