Added ConvertHandler function and tests

This commit is contained in:
mitchelljfs 2018-10-19 00:29:00 -07:00
parent 9626cf65d4
commit 5632b6b2db
2 changed files with 53 additions and 17 deletions

View File

@ -176,11 +176,11 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
functions := routeInterface.(routeFunctions) functions := routeInterface.(routeFunctions)
for _, m := range functions.middleware { for i := len(functions.middleware) - 1; i >= 0; i-- {
functions.handler = m(functions.handler) functions.handler = functions.middleware[i](functions.handler)
} }
for _, m := range r.middleware { for i := len(r.middleware) - 1; i >= 0; i-- {
functions.handler = m(functions.handler) functions.handler = r.middleware[i](functions.handler)
} }
functions.handler(ctx) functions.handler(ctx)
@ -190,6 +190,29 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
return response return response
} }
// ConvertHandler converts a pre-existing APIGHandler to an APIGMiddleware type.
// The before boolean determines whether the converted handler runs before and
// causes a return on error, or after and does not return if the first handler fails.
func ConvertHandler(handler APIGHandler, before bool) APIGMiddleware {
if before {
return func(next APIGHandler) APIGHandler {
return func(ctx *APIGContext) {
handler(ctx)
if ctx.Err != nil {
return
}
next(ctx)
}
}
}
return func(first APIGHandler) APIGHandler {
return func(ctx *APIGContext) {
first(ctx)
handler(ctx)
}
}
}
// NOTE: Begin helper functions. // NOTE: Begin helper functions.
type routeFunctions struct { type routeFunctions struct {
handler APIGHandler handler APIGHandler

View File

@ -15,16 +15,13 @@ func TestRouterSpec(t *testing.T) {
Convey("Given an instantiated router with an error reporting middleware, headers, and prefix", t, func() { Convey("Given an instantiated router with an error reporting middleware, headers, and prefix", t, func() {
request := events.APIGatewayProxyRequest{} request := events.APIGatewayProxyRequest{}
errorReporter := func(handler APIGHandler) APIGHandler { errorReporter := func(ctx *APIGContext) {
return func(ctx *APIGContext) { if ctx.Err != nil {
handler(ctx) ctx.Body, _ = json.Marshal(map[string]string{"error": ctx.Err.Error()})
if ctx.Err != nil { if strings.Contains(ctx.Err.Error(), "record not found") {
ctx.Body, _ = json.Marshal(map[string]string{"error": ctx.Err.Error()}) ctx.Status = 204
if strings.Contains(ctx.Err.Error(), "record not found") { } else if ctx.Status != 204 && ctx.Status < 400 {
ctx.Status = 204 ctx.Status = 400
} else if ctx.Status != 204 && ctx.Status < 400 {
ctx.Status = 400
}
} }
} }
} }
@ -36,7 +33,7 @@ func TestRouterSpec(t *testing.T) {
"Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Credentials": "true",
}, },
Middleware: []APIGMiddleware{ Middleware: []APIGMiddleware{
errorReporter, ConvertHandler(errorReporter, false),
}, },
}) })
@ -162,20 +159,36 @@ func TestRouterSpec(t *testing.T) {
}) })
Convey("When the handler func does return a status < 400", func() { Convey("When the handler func does return a status < 400", func() {
middlefunc1 := func(handler APIGHandler) APIGHandler { middlefunc1 := func(ctx *APIGContext) {
ctx.Status = http.StatusOK
}
middlefunc2 := func(handler APIGHandler) APIGHandler {
return func(ctx *APIGContext) { return func(ctx *APIGContext) {
ctx.Status = http.StatusOK ctx.Status = http.StatusOK
ctx.Err = errors.New("bad request") ctx.Err = errors.New("bad request")
if ctx.Err != nil {
return
}
handler(ctx) handler(ctx)
} }
} }
middlefunc3 := func(ctx *APIGContext) {
ctx.Err = errors.New("bad request")
}
hdlrfunc := func(ctx *APIGContext) { hdlrfunc := func(ctx *APIGContext) {
ctx.Status = http.StatusOK ctx.Status = http.StatusOK
ctx.Body = []byte("hello") ctx.Body = []byte("hello")
} }
Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() { Convey("And a Get handler expecting the pattern /listings/{id}/state/{event} is defined", func() {
rtr.Get("/listings/{id}/state/{event}", hdlrfunc, middlefunc1) rtr.Get(
"/listings/{id}/state/{event}",
hdlrfunc,
ConvertHandler(middlefunc1, true),
ConvertHandler(middlefunc3, true),
middlefunc2,
)
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