github.com/lingyao2333/mo-zero@v1.4.1/rest/handler/recoverhandler_test.go (about) 1 package handler 2 3 import ( 4 "io" 5 "log" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func init() { 14 log.SetOutput(io.Discard) 15 } 16 17 func TestWithPanic(t *testing.T) { 18 handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 19 panic("whatever") 20 })) 21 22 req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody) 23 resp := httptest.NewRecorder() 24 handler.ServeHTTP(resp, req) 25 assert.Equal(t, http.StatusInternalServerError, resp.Code) 26 } 27 28 func TestWithoutPanic(t *testing.T) { 29 handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 })) 31 32 req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody) 33 resp := httptest.NewRecorder() 34 handler.ServeHTTP(resp, req) 35 assert.Equal(t, http.StatusOK, resp.Code) 36 }