From ed6fbeee686e8f792f3d21aad18653fc22f2828b Mon Sep 17 00:00:00 2001 From: Abdelrahman Ahmed Date: Mon, 15 Jun 2020 11:27:21 +0200 Subject: [PATCH] fix route match (#40) * fix route match * fix route match * update version * update version --- gearbox.go | 2 +- router.go | 6 ++++-- router_test.go | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gearbox.go b/gearbox.go index 93dad2b..dc3f27b 100644 --- a/gearbox.go +++ b/gearbox.go @@ -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 = ` diff --git a/router.go b/router.go index 0c5c0d8..12412c2 100644 --- a/router.go +++ b/router.go @@ -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 { @@ -338,7 +337,6 @@ func matchEndpointParams(ep *endpoint, paths [][]byte, pathIndex int) (tst, bool return nil, false } - // if len(paths[pathIndex]) == 0 { pathIndex++ continue @@ -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 diff --git a/router_test.go b/router_test.go index 6a73a2f..18d39cf 100644 --- a/router_test.go +++ b/router_test.go @@ -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 @@ -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