forked from honeybadger-io/honeybadger-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honeybadger_test.go
327 lines (258 loc) · 6.67 KB
/
honeybadger_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package honeybadger
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/pborman/uuid"
"github.com/stretchr/testify/mock"
)
var (
mux *http.ServeMux
ts *httptest.Server
requests []*HTTPRequest
defaultConfig = *Config
)
type MockedHandler struct {
mock.Mock
}
func (h *MockedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.Called()
}
type HTTPRequest struct {
Request *http.Request
Body []byte
}
func (h *HTTPRequest) decodeJSON() hash {
var dat hash
err := json.Unmarshal(h.Body, &dat)
if err != nil {
panic(err)
}
return dat
}
func newHTTPRequest(r *http.Request) *HTTPRequest {
body, _ := ioutil.ReadAll(r.Body)
return &HTTPRequest{r, body}
}
func setup(t *testing.T) {
mux = http.NewServeMux()
ts = httptest.NewServer(mux)
requests = []*HTTPRequest{}
mux.HandleFunc("/v1/notices",
func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, r, "POST")
requests = append(requests, newHTTPRequest(r))
w.WriteHeader(201)
fmt.Fprint(w, `{"id":"87ded4b4-63cc-480a-b50c-8abe1376d972"}`)
},
)
*DefaultClient.Config = *newConfig(Configuration{APIKey: "badgers", Endpoint: ts.URL})
}
func teardown() {
*DefaultClient.Config = defaultConfig
}
func TestDefaultConfig(t *testing.T) {
if Config.APIKey != "" {
t.Errorf("Expected Config.APIKey to be empty by default. expected=%#v result=%#v", "", Config.APIKey)
}
}
func TestConfigure(t *testing.T) {
Configure(Configuration{APIKey: "badgers"})
if Config.APIKey != "badgers" {
t.Errorf("Expected Configure to override config.APIKey. expected=%#v actual=%#v", "badgers", Config.APIKey)
}
}
func TestNotify(t *testing.T) {
setup(t)
defer teardown()
res, _ := Notify(errors.New("Cobras!"))
if uuid.Parse(res) == nil {
t.Errorf("Expected Notify() to return a UUID. actual=%#v", res)
}
Flush()
if !testRequestCount(t, 1) {
return
}
testNoticePayload(t, requests[0].decodeJSON())
}
func TestNotifyWithContext(t *testing.T) {
setup(t)
defer teardown()
context := Context{"foo": "bar"}
Notify("Cobras!", context)
Flush()
if !testRequestCount(t, 1) {
return
}
payload := requests[0].decodeJSON()
if !testNoticePayload(t, payload) {
return
}
assertContext(t, payload, context)
}
func TestNotifyWithErrorClass(t *testing.T) {
setup(t)
defer teardown()
Notify("Cobras!", ErrorClass{"Badgers"})
Flush()
if !testRequestCount(t, 1) {
return
}
payload := requests[0].decodeJSON()
error_payload, _ := payload["error"].(map[string]interface{})
sent_klass, _ := error_payload["class"].(string)
if !testNoticePayload(t, payload) {
return
}
if sent_klass != "Badgers" {
t.Errorf("Custom error class should override default. expected=%v actual=%#v.", "Badgers", sent_klass)
return
}
}
func TestNotifyWithTags(t *testing.T) {
setup(t)
defer teardown()
Notify("Cobras!", Tags{"timeout", "http"})
Flush()
if !testRequestCount(t, 1) {
return
}
payload := requests[0].decodeJSON()
error_payload, _ := payload["error"].(map[string]interface{})
sent_tags, _ := error_payload["tags"].([]interface{})
if !testNoticePayload(t, payload) {
return
}
if got, want := sent_tags, []interface{}{"timeout", "http"}; !reflect.DeepEqual(got, want) {
t.Errorf("Custom error class should override default. expected=%#v actual=%#v.", want, got)
return
}
}
func TestNotifyWithFingerprint(t *testing.T) {
setup(t)
defer teardown()
Notify("Cobras!", Fingerprint{"Badgers"})
Flush()
if !testRequestCount(t, 1) {
return
}
payload := requests[0].decodeJSON()
error_payload, _ := payload["error"].(map[string]interface{})
sent_fingerprint, _ := error_payload["fingerprint"].(string)
if !testNoticePayload(t, payload) {
return
}
if sent_fingerprint != "Badgers" {
t.Errorf("Custom fingerprint should override default. expected=%v actual=%#v.", "Badgers", sent_fingerprint)
return
}
}
func TestMonitor(t *testing.T) {
setup(t)
defer teardown()
defer func() {
_ = recover()
if !testRequestCount(t, 1) {
return
}
testNoticePayload(t, requests[0].decodeJSON())
}()
defer Monitor()
panic("Cobras!")
}
func TestNotifyWithHandler(t *testing.T) {
setup(t)
defer teardown()
BeforeNotify(func(n *Notice) error {
n.Fingerprint = "foo bar baz"
return nil
})
Notify(errors.New("Cobras!"))
Flush()
payload := requests[0].decodeJSON()
error_payload, _ := payload["error"].(map[string]interface{})
sent_fingerprint, _ := error_payload["fingerprint"].(string)
if !testRequestCount(t, 1) {
return
}
if sent_fingerprint != "foo bar baz" {
t.Errorf("Handler fingerprint should override default. expected=%v actual=%#v.", "foo bar baz", sent_fingerprint)
return
}
}
func TestNotifyWithHandlerError(t *testing.T) {
setup(t)
defer teardown()
err := fmt.Errorf("Skipping this notification")
BeforeNotify(func(n *Notice) error {
return err
})
_, notifyErr := Notify(errors.New("Cobras!"))
Flush()
if !testRequestCount(t, 0) {
return
}
if notifyErr != err {
t.Errorf("Notify should return error from handler. expected=%v actual=%#v.", err, notifyErr)
return
}
}
// Helper functions.
func assertContext(t *testing.T, payload hash, expected Context) {
var request, context hash
var ok bool
request, ok = payload["request"].(map[string]interface{})
if !ok {
t.Errorf("Missing request in payload actual=%#v.", payload)
return
}
context, ok = request["context"].(map[string]interface{})
if !ok {
t.Errorf("Missing context in request payload actual=%#v.", request)
return
}
for k, v := range expected {
if context[k] != v {
t.Errorf("Expected context to include hash. expected=%#v actual=%#v", expected, context)
return
}
}
}
func testRequestCount(t *testing.T, num int) bool {
if len(requests) != num {
t.Errorf("Expected %v request to have been made. expected=%#v actual=%#v", num, num, len(requests))
return false
}
return true
}
func testNoticePayload(t *testing.T, payload hash) bool {
for _, key := range []string{"notifier", "error", "request", "server"} {
switch payload[key].(type) {
case map[string]interface{}:
// OK
default:
t.Errorf("Expected payload to include %v hash. expected=%#v actual=%#v", key, key, payload)
return false
}
}
return true
}
func TestHandlerCallsHandler(t *testing.T) {
mockHandler := &MockedHandler{}
mockHandler.On("ServeHTTP").Return()
handler := Handler(mockHandler)
req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
mockHandler.AssertCalled(t, "ServeHTTP")
}
func assertMethod(t *testing.T, r *http.Request, method string) {
if r.Method != method {
t.Errorf("Unexpected request method. actual=%#v expected=%#v", r.Method, method)
}
}