Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
added assert middleware to assert handler's behavior. (fix #2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Oct 27, 2022
1 parent 2994cc7 commit 497dc5f
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buffalo

import (
"net/http"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -123,15 +124,38 @@ func (ms *MiddlewareStack) Replace(mw1 MiddlewareFunc, mw2 MiddlewareFunc) {
ms.stack = stack
}

func (ms *MiddlewareStack) handler(info RouteInfo) Handler {
h := info.Handler
if len(ms.stack) > 0 {
mh := func(_ Handler) Handler {
return h
// assertMiddleware is a hidden middleware that works just befor and after the
// actual handler runs to make it sure everything is OK with the Handler
// specification.
//
// It writes response header with `http.StatusOK` if the request handler exited
// without error but the response status is still zero. Setting response is the
// responsibility of handler but this middleware make it sure the response
// should be compatible with middleware specification.
//
// See also: https://github.com/gobuffalo/buffalo/issues/2339
func assertMiddleware(handler Handler) Handler {
return func(c Context) error {
err := handler(c)
if err != nil {
return err
}

tstack := []MiddlewareFunc{mh}
if res, ok := c.Response().(*Response); ok {
if res.Status == 0 {
res.WriteHeader(http.StatusOK)
c.Logger().Debug("warning: handler exited without setting the response status. 200 OK will be used.")
}
}

return err
}
}

func (ms *MiddlewareStack) handler(info RouteInfo) Handler {
tstack := []MiddlewareFunc{assertMiddleware}

if len(ms.stack) > 0 {
sl := len(ms.stack) - 1
for i := sl; i >= 0; i-- {
mw := ms.stack[i]
Expand All @@ -140,12 +164,13 @@ func (ms *MiddlewareStack) handler(info RouteInfo) Handler {
tstack = append(tstack, mw)
}
}
}

for _, mw := range tstack {
h = mw(h)
}
return h
h := info.Handler
for _, mw := range tstack {
h = mw(h)
}

return h
}

Expand Down

0 comments on commit 497dc5f

Please sign in to comment.