2015-05-17 22:54:29 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-03-14 13:55:38 -07:00
|
|
|
"bytes"
|
2015-05-17 22:54:29 -07:00
|
|
|
"net/http"
|
2016-09-22 22:53:44 -07:00
|
|
|
"net/http/httptest"
|
2015-05-17 22:54:29 -07:00
|
|
|
"testing"
|
2015-05-30 10:54:55 -07:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-17 22:54:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-03-14 13:55:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
2016-09-22 22:53:44 -07:00
|
|
|
e.Logger.SetOutput(buf)
|
2017-02-22 15:57:12 -08:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-09-22 22:53:44 -07:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 10:21:23 -07:00
|
|
|
c := e.NewContext(req, rec)
|
2016-02-17 21:49:31 -08:00
|
|
|
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
|
2015-05-17 22:54:29 -07:00
|
|
|
panic("test")
|
2016-02-15 08:11:29 -08:00
|
|
|
}))
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2016-09-22 22:53:44 -07:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
2016-03-14 13:55:38 -07:00
|
|
|
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
2015-05-17 22:54:29 -07:00
|
|
|
}
|