Skip to content

Commit

Permalink
recoverer: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
drakkan committed May 30, 2021
1 parent b8ea886 commit a49e143
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions middleware/recoverer_test.go
@@ -0,0 +1,47 @@
package middleware

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi/v5"
)

func TestRecoverer(t *testing.T) {
w := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(Recoverer)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic("panic")
})

req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(w, req)
if w.Result().StatusCode != http.StatusInternalServerError {
t.Fatalf("Panic not recovered")
}
}

func TestRecovererAbortHandler(t *testing.T) {
defer func() {
rcv := recover()
if rcv != http.ErrAbortHandler {
t.Fatalf("http.ErrAbortHandler should not be recovered")
}
}()

w := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(Recoverer)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic(http.ErrAbortHandler)
})

req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(w, req)
}

0 comments on commit a49e143

Please sign in to comment.