mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
31 lines
785 B
Go
31 lines
785 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestWithPanic(t *testing.T) {
|
|
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
panic("whatever")
|
|
}))
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
|
resp := httptest.NewRecorder()
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusInternalServerError, resp.Code)
|
|
}
|
|
|
|
func TestWithoutPanic(t *testing.T) {
|
|
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
}))
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
|
resp := httptest.NewRecorder()
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
}
|