mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
Total coverage for middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -1,33 +1,24 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRecover(t *testing.T) {
|
||||
e := echo.New()
|
||||
e.SetDebug(true)
|
||||
req, _ := http.NewRequest(echo.GET, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
res := echo.NewResponse(w)
|
||||
c := echo.NewContext(req, res, e)
|
||||
rec := httptest.NewRecorder()
|
||||
c := echo.NewContext(req, echo.NewResponse(rec), e)
|
||||
h := func(c *echo.Context) error {
|
||||
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`.")
|
||||
}
|
||||
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
||||
assert.Contains(t, rec.Body.String(), "panic recover")
|
||||
}
|
||||
|
Reference in New Issue
Block a user