forked from urfave/negroni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery_test.go
149 lines (122 loc) · 4.22 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package negroni
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRecovery(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
handlerCalled := false
rec := NewRecovery()
rec.Logger = log.New(buff, "[negroni] ", 0)
rec.ErrorHandlerFunc = func(i interface{}) {
handlerCalled = true
}
n := New()
// replace log for testing
n.Use(rec)
n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
panic("here is a panic!")
}))
n.ServeHTTP(recorder, (*http.Request)(nil))
expect(t, recorder.Header().Get("Content-Type"), "text/plain; charset=utf-8")
expect(t, recorder.Code, http.StatusInternalServerError)
expect(t, handlerCalled, true)
refute(t, recorder.Body.Len(), 0)
refute(t, len(buff.String()), 0)
}
func TestRecovery_noContentTypeOverwrite(t *testing.T) {
recorder := httptest.NewRecorder()
rec := NewRecovery()
rec.Logger = log.New(bytes.NewBuffer([]byte{}), "[negroni] ", 0)
n := New()
n.Use(rec)
n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/javascript; charset=utf-8")
panic("here is a panic!")
}))
n.ServeHTTP(recorder, (*http.Request)(nil))
expect(t, recorder.Header().Get("Content-Type"), "application/javascript; charset=utf-8")
}
func TestRecovery_callbackPanic(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
rec := NewRecovery()
rec.Logger = log.New(buff, "[negroni] ", 0)
rec.ErrorHandlerFunc = func(i interface{}) {
panic("callback panic")
}
n := New()
n.Use(rec)
n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
panic("here is a panic!")
}))
n.ServeHTTP(recorder, (*http.Request)(nil))
expect(t, strings.Contains(buff.String(), "callback panic"), true)
}
type testOutput struct {
*bytes.Buffer
}
func newTestOutput() *testOutput {
buf := bytes.NewBufferString("")
return &testOutput{buf}
}
func (t *testOutput) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *PanicInformation) {
fmt.Fprintf(t, formatInfos(infos))
}
func formatInfos(infos *PanicInformation) string {
return fmt.Sprintf("%s %s", infos.RequestDescription(), infos.RecoveredPanic)
}
func TestRecovery_formatter(t *testing.T) {
recorder := httptest.NewRecorder()
formatter := newTestOutput()
req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", nil)
var element interface{} = "here is a panic!"
expectedInfos := &PanicInformation{RecoveredPanic: element, Request: req}
rec := NewRecovery()
rec.Formatter = formatter
n := New()
n.Use(rec)
n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
panic(element)
}))
n.ServeHTTP(recorder, req)
expect(t, formatInfos(expectedInfos), formatter.String())
}
func TestRecovery_PanicInformation(t *testing.T) {
// Request with query
req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", nil)
var element interface{} = "here is a panic!"
expectedInfos := &PanicInformation{RecoveredPanic: element, Request: req}
expect(t, expectedInfos.RequestDescription(), "GET /somePath?element=true")
// Request without Query
req, _ = http.NewRequest("POST", "http://localhost:3003/somePath", nil)
element = "here is a panic!"
expectedInfos = &PanicInformation{RecoveredPanic: element, Request: req}
expect(t, expectedInfos.RequestDescription(), "POST /somePath")
// Nil request
expectedInfos = &PanicInformation{RecoveredPanic: element, Request: nil}
expect(t, expectedInfos.RequestDescription(), nilRequestMessage)
// Stack
stackValue := "Some Stack element"
expectedInfos = &PanicInformation{RecoveredPanic: element, Request: req, Stack: []byte(stackValue)}
expect(t, expectedInfos.StackAsString(), stackValue)
}
func TestRecovery_HTMLFormatter(t *testing.T) {
recorder := httptest.NewRecorder()
rec := NewRecovery()
rec.Formatter = &HTMLPanicFormatter{}
n := New()
n.Use(rec)
n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
panic("some panic")
}))
n.ServeHTTP(recorder, (*http.Request)(nil))
expect(t, recorder.Header().Get("Content-Type"), "text/html; charset=utf-8")
refute(t, recorder.Body.Len(), 0)
}