-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecovery_test.go
45 lines (35 loc) · 1013 Bytes
/
recovery_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
43
44
45
package gomw
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldRecoverFromPanicWithLog(t *testing.T) {
next := &nextHandler{run: func() { panic("Some Error") }, Mutex: &sync.Mutex{}}
l := &clogger{}
rh := Recovery(l)(next)
r, _ := http.NewRequest("GET", "/url", nil)
w := httptest.NewRecorder()
rh.ServeHTTP(w, r)
assert.True(t, next.called)
assert.Equal(t, "panic: Some Error", l.log)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
func TestShouldRecoverFromPanic(t *testing.T) {
next := &nextHandler{run: func() { panic("Some Error") }, Mutex: &sync.Mutex{}}
rh := Recovery(nil)(next)
r, _ := http.NewRequest("GET", "/url", nil)
w := httptest.NewRecorder()
rh.ServeHTTP(w, r)
assert.True(t, next.called)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
type clogger struct {
log string
}
func (l *clogger) Println(args ...interface{}) {
l.log = fmt.Sprintf(args[0].(string), args[1:]...)
}