recovery_test.go 651 B

12345678910111213141516171819202122232425262728293031
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "os"
  8. )
  9. func TestRecovery(t *testing.T) {
  10. buff := bytes.NewBufferString("")
  11. recorder := httptest.NewRecorder()
  12. recorder.Body = buff
  13. n := NewWithLog(NewLogger(os.Stdout))
  14. n.Use(Recovery(true))
  15. n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
  16. panic("here is a panic!")
  17. }))
  18. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  19. if err != nil {
  20. t.Error(err)
  21. }
  22. n.ServeHTTP(recorder, req)
  23. expect(t, recorder.Code, http.StatusInternalServerError)
  24. refute(t, recorder.Body.Len(), 0)
  25. refute(t, len(buff.String()), 0)
  26. }