From fa2a855e975c5674433457be5360292cb7328eb5 Mon Sep 17 00:00:00 2001 From: mitchelljfs Date: Wed, 18 Jul 2018 16:15:07 -0700 Subject: [PATCH] Changed test cases to use pattern known to cause issue; updated path param replacement algorithm to fix said issue --- router.go | 16 ++++++++++++---- router_test.go | 31 +++++++++++++++++-------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/router.go b/router.go index 9644924..5432322 100644 --- a/router.go +++ b/router.go @@ -99,15 +99,23 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { endpointTree = r.endpoints[r.request.HTTPMethod] path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix) response = events.APIGatewayProxyResponse{} + splitPath = stripSlashesAndSplit(path) ) for k := range r.params { - p := strings.TrimPrefix(k, "{") - p = strings.TrimSuffix(p, "}") - if r.request.PathParameters[p] != "" { - path = strings.Replace(path, r.request.PathParameters[p], k, -1) + pname := strings.TrimPrefix(k, "{") + pname = strings.TrimSuffix(pname, "}") + if r.request.PathParameters[pname] != "" { + pval := r.request.PathParameters[pname] + for i, v := range splitPath { + if v == pval { + splitPath[i] = k + } + } + } } + path = "/" + strings.Join(splitPath, "/") if handlersInterface, ok = endpointTree.Get(path); !ok { respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"}) diff --git a/router_test.go b/router_test.go index d08e94c..bd8102d 100644 --- a/router_test.go +++ b/router_test.go @@ -22,8 +22,8 @@ func TestRouterSpec(t *testing.T) { ctx.Err = nil } - Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() { - rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) + Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() { + rtr.Get("/listings/{id}/state/{event}", hdlrfunc) rtr.Post("/orders", func(ctx *APIGContext) {}) rtr.Put("/orders", func(ctx *APIGContext) {}) rtr.Patch("/orders", func(ctx *APIGContext) {}) @@ -31,9 +31,10 @@ func TestRouterSpec(t *testing.T) { Convey("And the request matches the pattern and the path params are filled", func() { request.HTTPMethod = http.MethodGet - request.Path = "/shipping/orders/filter/by_user/4d50ff90-66e3-4047-bf37-0ca25837e41d" + request.Path = "/shipping/listings/57/state/list" request.PathParameters = map[string]string{ - "id": "4d50ff90-66e3-4047-bf37-0ca25837e41d", + "id": "57", + "event": "list", } request.RequestContext.Authorizer = map[string]interface{}{ "claims": map[string]interface{}{ @@ -61,9 +62,9 @@ func TestRouterSpec(t *testing.T) { }) }) - Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined AGAIN", func() { + Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined AGAIN", func() { So(func() { - rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) + rtr.Get("/listings/{id}/state/{event}", hdlrfunc) }, ShouldPanicWith, "endpoint already existent") }) @@ -106,14 +107,15 @@ func TestRouterSpec(t *testing.T) { } - Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() { - rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) + Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() { + rtr.Get("/listings/{id}/state/{event}", hdlrfunc) Convey("And the request matches the pattern and the path params are filled", func() { request.HTTPMethod = http.MethodGet - request.Path = "/shipping/orders/filter/by_user/4d50ff90-66e3-4047-bf37-0ca25837e41d" + request.Path = "/shipping/listings/57/state/list" request.PathParameters = map[string]string{ - "id": "4d50ff90-66e3-4047-bf37-0ca25837e41d", + "id": "57", + "event": "list", } Convey("The router will return the expected status and body", func() { @@ -143,14 +145,15 @@ func TestRouterSpec(t *testing.T) { ctx.Err = nil } - Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() { - rtr.Get("/orders/filter/by_user/{id}", middlefunc1, middlefunc2, hdlrfunc) + Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() { + rtr.Get("/listings/{id}/state/{event}", middlefunc1, middlefunc2, hdlrfunc) Convey("And the request matches the pattern and the path params are filled", func() { request.HTTPMethod = http.MethodGet - request.Path = "/shipping/orders/filter/by_user/4d50ff90-66e3-4047-bf37-0ca25837e41d" + request.Path = "/shipping/listings/57/state/list" request.PathParameters = map[string]string{ - "id": "4d50ff90-66e3-4047-bf37-0ca25837e41d", + "id": "57", + "event": "list", } Convey("The router will return the expected status and body", func() {