Merge pull request #1 from whip-networks/ch33079/fix-the-season-watch-unwatch-endpoint
[ch33079] Fixed the route parser
This commit is contained in:
commit
773501d92c
2
go.mod
2
go.mod
|
@ -1,5 +1,7 @@
|
||||||
module github.com/whip-networks/lambdarouter
|
module github.com/whip-networks/lambdarouter
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/aws/aws-lambda-go v1.10.0
|
github.com/aws/aws-lambda-go v1.10.0
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aws/aws-lambda-go/events"
|
"github.com/aws/aws-lambda-go/events"
|
||||||
|
@ -81,7 +82,10 @@ func (r Router) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
|
||||||
path := req.Path
|
path := req.Path
|
||||||
|
|
||||||
for param, value := range req.PathParameters {
|
for param, value := range req.PathParameters {
|
||||||
path = strings.Replace(path, value, "{"+param+"}", -1)
|
r := regexp.MustCompile(fmt.Sprintf("/(%s)(/|$)", value))
|
||||||
|
path = r.ReplaceAllStringFunc(path, func(m string) string {
|
||||||
|
return strings.Replace(m, value, fmt.Sprintf("{%s}", param), -1)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
i, found := r.events.Get([]byte(req.HTTPMethod + path))
|
i, found := r.events.Get([]byte(req.HTTPMethod + path))
|
||||||
|
|
|
@ -51,6 +51,7 @@ func TestRouter(t *testing.T) {
|
||||||
desc(t, 4, "insert routes with the specified prefix succesfully")
|
desc(t, 4, "insert routes with the specified prefix succesfully")
|
||||||
r.Group("/ding", func(r *Router) {
|
r.Group("/ding", func(r *Router) {
|
||||||
r.Post("dong/{door}", handler)
|
r.Post("dong/{door}", handler)
|
||||||
|
r.Post("{dong}/door", handler)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,6 +90,20 @@ func TestRouter(t *testing.T) {
|
||||||
_, err = r.Invoke(ctx, nil)
|
_, err = r.Invoke(ctx, nil)
|
||||||
|
|
||||||
a.Error(err)
|
a.Error(err)
|
||||||
|
|
||||||
|
e = events.APIGatewayProxyRequest{
|
||||||
|
Path: "/prefix/ding/talal/door",
|
||||||
|
HTTPMethod: http.MethodPost,
|
||||||
|
PathParameters: map[string]string{"dong": "talal"},
|
||||||
|
}
|
||||||
|
|
||||||
|
desc(t, 4, "should succesfully route and invoke a defined route")
|
||||||
|
ejson, _ = json.Marshal(e)
|
||||||
|
|
||||||
|
res, err = r.Invoke(ctx, ejson)
|
||||||
|
|
||||||
|
a.NoError(err)
|
||||||
|
a.Exactly("null", string(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue