-
Notifications
You must be signed in to change notification settings - Fork 3
/
panic_test.go
76 lines (65 loc) · 1.27 KB
/
panic_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
package mars
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPanicInAction(t *testing.T) {
startFakeBookingApp()
TRACE = log.New(ioutil.Discard, "", 0)
INFO = TRACE
WARN = TRACE
ERROR = TRACE
DevMode = false
ts := httptest.NewServer(Handler)
defer ts.Close()
res, err := http.Get(ts.URL + "/boom")
if err != nil {
log.Fatal(err)
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if !strings.Contains(string(resp), "OMG") {
t.Error("Unable to get panic description, got:\n", resp)
}
}
func containsAll(raw []byte, list ...string) bool {
s := string(raw)
for _, i := range list {
if !strings.Contains(s, i) {
return false
}
}
return true
}
func TestPanicInDevMode(t *testing.T) {
startFakeBookingApp()
TRACE = log.New(ioutil.Discard, "", 0)
INFO = TRACE
WARN = TRACE
ERROR = TRACE
DevMode = true
ts := httptest.NewServer(Handler)
defer ts.Close()
res, err := http.Get(ts.URL + "/boom")
if err != nil {
log.Fatal(err)
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if !containsAll(resp,
"mars/fakeapp_test.go",
"Hotels.Boom",
"OMG") {
t.Error("Unable to get full panic info, got:\n", string(resp))
}
}