recovery_test.go 853 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package gin
  2. import (
  3. "bytes"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "testing"
  9. )
  10. // TestPanicInHandler assert that panic has been recovered.
  11. func TestPanicInHandler(t *testing.T) {
  12. req, _ := http.NewRequest("GET", "/", nil)
  13. w := httptest.NewRecorder()
  14. // Disable panic logs for testing
  15. log.SetOutput(bytes.NewBuffer(nil))
  16. r := Default()
  17. r.GET("/", func(_ *Context) {
  18. panic("Oupps, Houston, we have a problem")
  19. })
  20. r.ServeHTTP(w, req)
  21. // restore logging
  22. log.SetOutput(os.Stderr)
  23. if w.Code != 500 {
  24. t.Errorf("Response code should be Internal Server Error, was: %s", w.Code)
  25. }
  26. bodyAsString := w.Body.String()
  27. // fixme:
  28. if bodyAsString != "" {
  29. t.Errorf("Response body should be empty, was %s", bodyAsString)
  30. }
  31. //fixme:
  32. if len(w.HeaderMap) != 0 {
  33. t.Errorf("No headers should be provided, was %s", w.HeaderMap)
  34. }
  35. }