Changed the NewAPIGRouter input to a struct; Added response Headers to
config struct
This commit is contained in:
parent
fa2a855e97
commit
5a9f5e2bf3
26
router.go
26
router.go
|
@ -41,15 +41,25 @@ type APIGRouter struct {
|
|||
request *events.APIGatewayProxyRequest
|
||||
endpoints map[string]*radix.Tree
|
||||
params map[string]string
|
||||
svcprefix string
|
||||
prefix string
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
// APIGRouterConfig is used as the input to NewAPIGRouter, request is your incoming
|
||||
// apig request and prefix will be stripped of all incoming request paths. Headers
|
||||
// will be sent with all responses.
|
||||
type APIGRouterConfig struct {
|
||||
Request *events.APIGatewayProxyRequest
|
||||
Prefix string
|
||||
Headers map[string]string
|
||||
}
|
||||
|
||||
// NOTE: Begin router methods.
|
||||
|
||||
// NewAPIGRouter creates a new router using the request and a prefix to strip from your incoming requests.
|
||||
func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRouter {
|
||||
// NewAPIGRouter creates a new router using the given router config.
|
||||
func NewAPIGRouter(cfg *APIGRouterConfig) *APIGRouter {
|
||||
return &APIGRouter{
|
||||
request: r,
|
||||
request: cfg.Request,
|
||||
endpoints: map[string]*radix.Tree{
|
||||
post: radix.New(),
|
||||
get: radix.New(),
|
||||
|
@ -57,8 +67,9 @@ func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRout
|
|||
patch: radix.New(),
|
||||
delete: radix.New(),
|
||||
},
|
||||
params: map[string]string{},
|
||||
svcprefix: svcprefix,
|
||||
params: map[string]string{},
|
||||
prefix: cfg.Prefix,
|
||||
headers: cfg.Headers,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +108,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
|
|||
err error
|
||||
|
||||
endpointTree = r.endpoints[r.request.HTTPMethod]
|
||||
path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix)
|
||||
path = strings.TrimPrefix(r.request.Path, r.prefix)
|
||||
response = events.APIGatewayProxyResponse{}
|
||||
splitPath = stripSlashesAndSplit(path)
|
||||
)
|
||||
|
@ -158,6 +169,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
|
|||
|
||||
response.StatusCode = status
|
||||
response.Body = string(respbody)
|
||||
response.Headers = r.headers
|
||||
return response
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,14 @@ func TestRouterSpec(t *testing.T) {
|
|||
|
||||
Convey("Given an instantiated router", t, func() {
|
||||
request := events.APIGatewayProxyRequest{}
|
||||
rtr := NewAPIGRouter(&request, "shipping")
|
||||
rtr := NewAPIGRouter(&APIGRouterConfig{
|
||||
Request: &request,
|
||||
Prefix: "/shipping",
|
||||
Headers: map[string]string{
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
},
|
||||
})
|
||||
|
||||
Convey("When the handler func does NOT return an error", func() {
|
||||
hdlrfunc := func(ctx *APIGContext) {
|
||||
|
@ -42,11 +49,15 @@ func TestRouterSpec(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
Convey("The router will return the expected status and body", func() {
|
||||
Convey("The router will return the expected status, body, and headers", func() {
|
||||
response := rtr.Respond()
|
||||
|
||||
So(response.StatusCode, ShouldEqual, http.StatusOK)
|
||||
So(response.Body, ShouldEqual, "hello")
|
||||
So(response.Headers, ShouldResemble, map[string]string{
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue