recoverhandler_test.go 785 B

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