recoverhandler_test.go 837 B

123456789101112131415161718192021222324252627282930313233343536
  1. package handler
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func init() {
  11. log.SetOutput(ioutil.Discard)
  12. }
  13. func TestWithPanic(t *testing.T) {
  14. handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. panic("whatever")
  16. }))
  17. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  18. resp := httptest.NewRecorder()
  19. handler.ServeHTTP(resp, req)
  20. assert.Equal(t, http.StatusInternalServerError, resp.Code)
  21. }
  22. func TestWithoutPanic(t *testing.T) {
  23. handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24. }))
  25. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  26. resp := httptest.NewRecorder()
  27. handler.ServeHTTP(resp, req)
  28. assert.Equal(t, http.StatusOK, resp.Code)
  29. }