forked from go-chi/chi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recoverer_test.go
42 lines (33 loc) · 994 Bytes
/
recoverer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package middleware
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/go-chi/chi/v5"
)
func panicingHandler(http.ResponseWriter, *http.Request) { panic("foo") }
func TestRecoverer(t *testing.T) {
r := chi.NewRouter()
oldRecovererErrorWriter := recovererErrorWriter
defer func() { recovererErrorWriter = oldRecovererErrorWriter }()
buf := &bytes.Buffer{}
recovererErrorWriter = buf
r.Use(Recoverer)
r.Get("/", panicingHandler)
ts := httptest.NewServer(r)
defer ts.Close()
res, _ := testRequest(t, ts, "GET", "/", nil)
assertEqual(t, res.StatusCode, http.StatusInternalServerError)
lines := strings.Split(buf.String(), "\n")
for _, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "->") {
if !strings.Contains(line, "panicingHandler") {
t.Fatalf("First func call line should refer to panicingHandler, but actual line:\n%v\n", line)
}
return
}
}
t.Fatal("First func call line should start with ->.")
}