2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
|
|
|
e := echo.New()
|
2015-05-20 03:59:05 +02:00
|
|
|
e.SetDebug(true)
|
2015-05-18 07:54:29 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
res := &echo.Response{Writer: w}
|
|
|
|
c := echo.NewContext(req, res, e)
|
|
|
|
h := func(c *echo.Context) *echo.HTTPError {
|
|
|
|
panic("test")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status
|
|
|
|
Recover()(h)(c)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
|
|
t.Errorf("expected status `500`, got %d.", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Body
|
|
|
|
s := w.Body.String()
|
|
|
|
if !strings.Contains(s, "panic recover") {
|
|
|
|
t.Error("expected body contains `panice recover`.")
|
|
|
|
}
|
|
|
|
}
|