2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-03-14 22:55:38 +02:00
|
|
|
"bytes"
|
2022-01-24 12:23:41 +02:00
|
|
|
"errors"
|
2020-07-06 16:59:42 +02:00
|
|
|
"fmt"
|
2015-05-18 07:54:29 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2015-05-18 07:54:29 +02:00
|
|
|
"testing"
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2020-07-06 16:59:42 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-05-30 19:54:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-18 07:54:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-03-14 22:55:38 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2016-09-23 07:53:44 +02:00
|
|
|
e.Logger.SetOutput(buf)
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2016-02-18 07:49:31 +02:00
|
|
|
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
|
2015-05-18 07:54:29 +02:00
|
|
|
panic("test")
|
2016-02-15 18:11:29 +02:00
|
|
|
}))
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
2016-03-14 22:55:38 +02:00
|
|
|
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
2020-07-06 16:59:42 +02:00
|
|
|
|
2022-03-16 01:29:42 +02:00
|
|
|
func TestRecoverErrAbortHandler(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
e.Logger.SetOutput(buf)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
}))
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
assert.Fail(t, "expecting `http.ErrAbortHandler`, got `nil`")
|
|
|
|
} else {
|
|
|
|
if err, ok := r.(error); ok {
|
|
|
|
assert.ErrorIs(t, err, http.ErrAbortHandler)
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, "not of error type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
h(c)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
assert.NotContains(t, buf.String(), "PANIC RECOVER")
|
|
|
|
}
|
|
|
|
|
2020-07-06 16:59:42 +02:00
|
|
|
func TestRecoverWithConfig_LogLevel(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
logLevel log.Lvl
|
|
|
|
levelName string
|
|
|
|
}{{
|
|
|
|
logLevel: log.DEBUG,
|
|
|
|
levelName: "DEBUG",
|
|
|
|
}, {
|
|
|
|
logLevel: log.INFO,
|
|
|
|
levelName: "INFO",
|
|
|
|
}, {
|
|
|
|
logLevel: log.WARN,
|
|
|
|
levelName: "WARN",
|
|
|
|
}, {
|
|
|
|
logLevel: log.ERROR,
|
|
|
|
levelName: "ERROR",
|
|
|
|
}, {
|
|
|
|
logLevel: log.OFF,
|
|
|
|
levelName: "OFF",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.levelName, func(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Logger.SetLevel(log.DEBUG)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
e.Logger.SetOutput(buf)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
config := DefaultRecoverConfig
|
|
|
|
config.LogLevel = tt.logLevel
|
|
|
|
h := RecoverWithConfig(config)(echo.HandlerFunc(func(c echo.Context) error {
|
|
|
|
panic("test")
|
|
|
|
}))
|
|
|
|
|
|
|
|
h(c)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
if tt.logLevel == log.OFF {
|
|
|
|
assert.Empty(t, output)
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, output, "PANIC RECOVER")
|
|
|
|
assert.Contains(t, output, fmt.Sprintf(`"level":"%s"`, tt.levelName))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 12:23:41 +02:00
|
|
|
|
|
|
|
func TestRecoverWithConfig_LogErrorFunc(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Logger.SetLevel(log.DEBUG)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
e.Logger.SetOutput(buf)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
testError := errors.New("test")
|
|
|
|
config := DefaultRecoverConfig
|
|
|
|
config.LogErrorFunc = func(c echo.Context, err error, stack []byte) error {
|
|
|
|
msg := fmt.Sprintf("[PANIC RECOVER] %v %s\n", err, stack)
|
|
|
|
if errors.Is(err, testError) {
|
|
|
|
c.Logger().Debug(msg)
|
|
|
|
} else {
|
|
|
|
c.Logger().Error(msg)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("first branch case for LogErrorFunc", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
|
|
|
h := RecoverWithConfig(config)(echo.HandlerFunc(func(c echo.Context) error {
|
|
|
|
panic(testError)
|
|
|
|
}))
|
|
|
|
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
assert.Contains(t, output, "PANIC RECOVER")
|
|
|
|
assert.Contains(t, output, `"level":"DEBUG"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("else branch case for LogErrorFunc", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
|
|
|
h := RecoverWithConfig(config)(echo.HandlerFunc(func(c echo.Context) error {
|
|
|
|
panic("other")
|
|
|
|
}))
|
|
|
|
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
assert.Contains(t, output, "PANIC RECOVER")
|
|
|
|
assert.Contains(t, output, `"level":"ERROR"`)
|
|
|
|
})
|
|
|
|
}
|