Changed test cases to use pattern known to cause issue; updated path

param replacement algorithm to fix said issue
This commit is contained in:
mitchelljfs 2018-07-18 16:15:07 -07:00
parent 820e7f7793
commit fa2a855e97
2 changed files with 29 additions and 18 deletions

View File

@ -99,16 +99,24 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
endpointTree = r.endpoints[r.request.HTTPMethod] endpointTree = r.endpoints[r.request.HTTPMethod]
path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix) path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix)
response = events.APIGatewayProxyResponse{} response = events.APIGatewayProxyResponse{}
splitPath = stripSlashesAndSplit(path)
) )
for k := range r.params { for k := range r.params {
p := strings.TrimPrefix(k, "{") pname := strings.TrimPrefix(k, "{")
p = strings.TrimSuffix(p, "}") pname = strings.TrimSuffix(pname, "}")
if r.request.PathParameters[p] != "" { if r.request.PathParameters[pname] != "" {
path = strings.Replace(path, r.request.PathParameters[p], k, -1) 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 { if handlersInterface, ok = endpointTree.Get(path); !ok {
respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"}) respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"})

View File

@ -22,8 +22,8 @@ func TestRouterSpec(t *testing.T) {
ctx.Err = nil ctx.Err = nil
} }
Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() { Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() {
rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) rtr.Get("/listings/{id}/state/{event}", hdlrfunc)
rtr.Post("/orders", func(ctx *APIGContext) {}) rtr.Post("/orders", func(ctx *APIGContext) {})
rtr.Put("/orders", func(ctx *APIGContext) {}) rtr.Put("/orders", func(ctx *APIGContext) {})
rtr.Patch("/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() { Convey("And the request matches the pattern and the path params are filled", func() {
request.HTTPMethod = http.MethodGet 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{ request.PathParameters = map[string]string{
"id": "4d50ff90-66e3-4047-bf37-0ca25837e41d", "id": "57",
"event": "list",
} }
request.RequestContext.Authorizer = map[string]interface{}{ request.RequestContext.Authorizer = map[string]interface{}{
"claims": 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() { So(func() {
rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) rtr.Get("/listings/{id}/state/{event}", hdlrfunc)
}, ShouldPanicWith, "endpoint already existent") }, 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() { Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() {
rtr.Get("/orders/filter/by_user/{id}", hdlrfunc) rtr.Get("/listings/{id}/state/{event}", hdlrfunc)
Convey("And the request matches the pattern and the path params are filled", func() { Convey("And the request matches the pattern and the path params are filled", func() {
request.HTTPMethod = http.MethodGet 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{ 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() { Convey("The router will return the expected status and body", func() {
@ -143,14 +145,15 @@ func TestRouterSpec(t *testing.T) {
ctx.Err = nil ctx.Err = nil
} }
Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() { Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() {
rtr.Get("/orders/filter/by_user/{id}", middlefunc1, middlefunc2, hdlrfunc) rtr.Get("/listings/{id}/state/{event}", middlefunc1, middlefunc2, hdlrfunc)
Convey("And the request matches the pattern and the path params are filled", func() { Convey("And the request matches the pattern and the path params are filled", func() {
request.HTTPMethod = http.MethodGet 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{ 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() { Convey("The router will return the expected status and body", func() {