-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery_test.go
86 lines (73 loc) · 2.3 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package recovery
import (
"github.com/stretchr/testify/assert"
"github.com/vanng822/r2router"
"github.com/codegangsta/negroni"
//"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestSeeforRecovery(t *testing.T) {
router := r2router.NewSeeforRouter()
rec := NewRecovery()
router.Before(rec.Handler)
router.Get("/user/keys/:id", func(w http.ResponseWriter, r *http.Request, p r2router.Params) {
panic("This shouldn't crash Seefor")
})
ts := httptest.NewServer(router)
defer ts.Close()
// get
res, err := http.Get(ts.URL + "/user/keys/testing")
assert.Nil(t, err)
content, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, res.StatusCode, http.StatusInternalServerError)
assert.Equal(t, string(content), "Internal Server Error")
}
func TestSeeforRecoveryMiddlewarePanic(t *testing.T) {
router := r2router.NewSeeforRouter()
rec := NewRecovery()
rec.PrintStack = true
router.Before(rec.Handler)
router.Get("/user/keys/:id", func(w http.ResponseWriter, r *http.Request, p r2router.Params) {
panic("This shouldn't crash Seefor")
})
router.Before(r2router.WrapBeforeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("Middleware panic")
})))
ts := httptest.NewServer(router)
defer ts.Close()
// get
res, err := http.Get(ts.URL + "/user/keys/testing")
assert.Nil(t, err)
content, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, res.StatusCode, http.StatusInternalServerError)
assert.Contains(t, string(content), "Middleware panic")
assert.NotContains(t, string(content), "This shouldn't crash Seefor")
}
func TestSeeforRecoveryPrintStack(t *testing.T) {
router := r2router.NewRouter()
n := negroni.New()
rec := NewRecovery()
rec.PrintStack = true
n.UseFunc(rec.HandlerFuncWithNext)
router.Get("/user/keys/:id", func(w http.ResponseWriter, r *http.Request, p r2router.Params) {
panic("This shouldn't crash Seefor")
})
n.UseHandler(router)
ts := httptest.NewServer(n)
defer ts.Close()
// get
res, err := http.Get(ts.URL + "/user/keys/testing")
assert.Nil(t, err)
content, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, res.StatusCode, http.StatusInternalServerError)
assert.Contains(t, string(content), "This shouldn't crash Seefor")
}