2020-07-29 18:00:04 +08:00
|
|
|
package handler
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
import (
|
2022-09-10 15:18:52 +08:00
|
|
|
"io"
|
2020-07-26 17:09:05 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-09-10 15:18:52 +08:00
|
|
|
log.SetOutput(io.Discard)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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", nil)
|
|
|
|
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", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(resp, req)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
}
|