forked from rollbar/rollbar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollbar_test.go
367 lines (308 loc) · 9.67 KB
/
rollbar_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package rollbar
import (
"errors"
"fmt"
"net/http"
"os"
"testing"
)
type CustomError struct {
s string
}
func (e *CustomError) Error() string {
return e.s
}
func testErrorStack(s string) {
testErrorStack2(s)
}
func testErrorStack2(s string) {
ErrorWithLevel("error", errors.New(s))
}
func testErrorStackWithSkip(s string) {
testErrorStackWithSkip2(s)
}
func testErrorStackWithSkip2(s string) {
ErrorWithStackSkip("error", errors.New(s), 2)
}
func testErrorStackWithSkipGeneric(s string) {
testErrorStackWithSkipGeneric2(s)
}
func testErrorStackWithSkipGeneric2(s string) {
Warning(errors.New(s), 2)
}
func TestErrorClass(t *testing.T) {
errors := map[string]error{
"{4b81076c}": fmt.Errorf("something is broken"),
"rollbar.CustomError": &CustomError{"terrible mistakes were made"},
}
for expected, err := range errors {
if errorClass(err) != expected {
t.Error("Got:", errorClass(err), "Expected:", expected)
}
}
}
func TestEverything(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
if Token() != os.Getenv("TOKEN") {
t.Error("Token should be as set")
}
if Environment() != "test" {
t.Error("Token should be as set")
}
ErrorWithLevel("critical", errors.New("Normal critical error"))
ErrorWithLevel("error", &CustomError{"This is a custom error"})
testErrorStack("This error should have a nice stacktrace")
testErrorStackWithSkip("This error should have a skipped stacktrace")
done := make(chan bool)
go func() {
testErrorStack("I'm in a goroutine")
done <- true
}()
<-done
Message("error", "This is an error message")
Message("info", "And this is an info message")
SetFingerprint(true)
Errorf("error", "%s %s", "Some argument", "Another argument")
r, _ := http.NewRequest("GET", "http://foo.com/somethere?param1=true", nil)
r.RemoteAddr = "1.1.1.1:123"
RequestMessage("debug", r, "This is a debug message with a request")
SetCaptureIp(CaptureIpAnonymize)
RequestError("info", r, errors.New("Some info error with a request"))
r.RemoteAddr = "FE80::0202:B3FF:FE1E:8329"
RequestErrorWithStackSkip("info", r, errors.New("Some info error with a request"), 2)
Wait()
}
type someNonstandardTypeForLogFailing struct{}
func TestEverythingGeneric(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
SetCaptureIp(CaptureIpAnonymize)
if Token() != os.Getenv("TOKEN") {
t.Error("Token should be as set")
}
if Environment() != "test" {
t.Error("Token should be as set")
}
Critical(errors.New("Normal generic critical error"))
Error(&CustomError{"This is a generic custom error"})
testErrorStackWithSkipGeneric("This generic error should have a skipped stacktrace")
done := make(chan bool)
go func() {
testErrorStack("I'm in a generic goroutine")
done <- true
}()
<-done
Error("This is a generic error message")
Info("And this is a generic info message", map[string]interface{}{
"hello": "rollbar",
})
SetLogger(&SilentClientLogger{})
Info(someNonstandardTypeForLogFailing{}, "I am a string and I did not fail")
SetLogger(nil)
r, _ := http.NewRequest("GET", "http://foo.com/somethere?param1=true", nil)
r.RemoteAddr = "1.1.1.1:123"
Debug(r, "This is a message with a generic request")
Warning(errors.New("Some generic error with a request"), r, map[string]interface{}{
"hello": "request",
})
Wait()
}
func TestBuildBody(t *testing.T) {
// custom provided at config time
baseCustom := map[string]interface{}{
"BASE_CUSTOM_KEY": "BASE_CUSTOM_VALUE",
"OVERRIDDEN_CUSTOM_KEY": "BASE",
}
SetCustom(baseCustom)
// custom provided at call site
extraCustom := map[string]interface{}{
"EXTRA_CUSTOM_KEY": "EXTRA_CUSTOM_VALUE",
"OVERRIDDEN_CUSTOM_KEY": "EXTRA",
}
body := interface{}(std).(*Client).buildBody(ERR, "test error", extraCustom)
if body["data"] == nil {
t.Error("body should have data")
}
data := body["data"].(map[string]interface{})
if data["custom"] == nil {
t.Error("data should have custom")
}
custom := data["custom"].(map[string]interface{})
if custom["BASE_CUSTOM_KEY"] != "BASE_CUSTOM_VALUE" {
t.Error("custom should have base")
}
if custom["EXTRA_CUSTOM_KEY"] != "EXTRA_CUSTOM_VALUE" {
t.Error("custom should have extra")
}
if custom["OVERRIDDEN_CUSTOM_KEY"] != "EXTRA" {
t.Error("extra custom should overwrite base custom where keys match")
}
if Custom()["EXTRA_CUSTOM_KEY"] != nil {
t.Error("adding extra modified the client custom data config")
}
}
func TestBuildBodyNoBaseCustom(t *testing.T) {
extraCustom := map[string]interface{}{
"EXTRA_CUSTOM_KEY": "EXTRA_CUSTOM_VALUE",
"OVERRIDDEN_CUSTOM_KEY": "EXTRA",
}
body := interface{}(std).(*Client).buildBody(ERR, "test error", extraCustom)
if body["data"] == nil {
t.Error("body should have data")
}
data := body["data"].(map[string]interface{})
if data["custom"] == nil {
t.Error("data should have custom")
}
custom := data["custom"].(map[string]interface{})
if custom["EXTRA_CUSTOM_KEY"] != "EXTRA_CUSTOM_VALUE" {
t.Error("custom should have extra")
}
if custom["OVERRIDDEN_CUSTOM_KEY"] != "EXTRA" {
t.Error("extra custom should also work")
}
}
func TestErrorRequest(t *testing.T) {
r, _ := http.NewRequest("GET", "http://foo.com/somethere?param1=true", nil)
r.RemoteAddr = "1.1.1.1:123"
object := std.requestDetails(r)
if object["url"] != "http://foo.com/somethere?param1=true" {
t.Errorf("wrong url, got %v", object["url"])
}
if object["method"] != "GET" {
t.Errorf("wrong method, got %v", object["method"])
}
if object["query_string"] != "param1=true" {
t.Errorf("wrong id, got %v", object["query_string"])
}
}
func TestFilterParams(t *testing.T) {
values := map[string][]string{
"password": {"one"},
"ok": {"one"},
"access_token": {"one"},
}
clean := filterParams(std.configuration.scrubFields, values)
if clean["password"][0] != FILTERED {
t.Error("should filter password parameter")
}
if clean["ok"][0] == FILTERED {
t.Error("should keep ok parameter")
}
if clean["access_token"][0] != FILTERED {
t.Error("should filter access_token parameter")
}
}
func TestFlattenValues(t *testing.T) {
values := map[string][]string{
"a": {"one"},
"b": {"one", "two"},
}
flattened := flattenValues(values)
if flattened["a"].(string) != "one" {
t.Error("should flatten single parameter to string")
}
if len(flattened["b"].([]string)) != 2 {
t.Error("should leave multiple parametres as []string")
}
}
type cs struct {
error
cause error
stack Stack
}
func (cs cs) Cause() error {
return cs.cause
}
func (cs cs) Stack() Stack {
return cs.stack
}
func TestGetCauseOfStdErr(t *testing.T) {
if nil != getCause(fmt.Errorf("")) {
t.Error("cause should be nil for standard error")
}
}
func TestGetCauseOfCauseStacker(t *testing.T) {
cause := fmt.Errorf("cause")
effect := cs{fmt.Errorf("effect"), cause, nil}
if cause != getCause(effect) {
t.Error("effect should return cause")
}
}
func TestGetOrBuildStackOfStdErrWithoutParent(t *testing.T) {
err := cs{fmt.Errorf(""), nil, BuildStack(0)}
if nil == getOrBuildStack(err, nil, 0) {
t.Error("should build stack if parent is not a CauseStacker")
}
}
func TestGetOrBuildStackOfStdErrWithParent(t *testing.T) {
cause := fmt.Errorf("cause")
effect := cs{fmt.Errorf("effect"), cause, BuildStack(0)}
if 0 != len(getOrBuildStack(cause, effect, 0)) {
t.Error("should return empty stack of stadard error if parent is CauseStacker")
}
}
func TestGetOrBuildStackOfCauseStackerWithoutParent(t *testing.T) {
cause := fmt.Errorf("cause")
effect := cs{fmt.Errorf("effect"), cause, BuildStack(0)}
if effect.Stack()[0] != getOrBuildStack(effect, nil, 0)[0] {
t.Error("should use stack from effect")
}
}
func TestGetOrBuildStackOfCauseStackerWithParent(t *testing.T) {
cause := fmt.Errorf("cause")
effect := cs{fmt.Errorf("effect"), cause, BuildStack(0)}
effect2 := cs{fmt.Errorf("effect2"), effect, BuildStack(0)}
if effect.Stack()[0] != getOrBuildStack(effect2, effect, 0)[0] {
t.Error("should use stack from effect2")
}
}
func TestErrorBodyWithoutChain(t *testing.T) {
err := fmt.Errorf("ERR")
errorBody, fingerprint := errorBody(configuration{fingerprint: true}, err, 0)
if nil != errorBody["trace"] {
t.Error("should not have trace element")
}
if nil == errorBody["trace_chain"] {
t.Error("should have trace_chain element")
}
traces := errorBody["trace_chain"].([]map[string]interface{})
if 1 != len(traces) {
t.Error("chain should contain 1 trace")
}
if "ERR" != traces[0]["exception"].(map[string]interface{})["message"] {
t.Error("chain should contain err")
}
if "0" == fingerprint {
t.Error("fingerprint should be auto-generated and non-zero. got: ", fingerprint)
}
}
func TestErrorBodyWithChain(t *testing.T) {
cause := fmt.Errorf("cause")
effect := cs{fmt.Errorf("effect1"), cause, BuildStack(0)}
effect2 := cs{fmt.Errorf("effect2"), effect, BuildStack(0)}
errorBody, fingerprint := errorBody(configuration{fingerprint: true}, effect2, 0)
if nil != errorBody["trace"] {
t.Error("should not have trace element")
}
if nil == errorBody["trace_chain"] {
t.Error("should have trace_chain element")
}
traces := errorBody["trace_chain"].([]map[string]interface{})
if 3 != len(traces) {
t.Error("chain should contain 3 traces")
}
if "effect2" != traces[0]["exception"].(map[string]interface{})["message"] {
t.Error("chain should contain effect2 first")
}
if "effect1" != traces[1]["exception"].(map[string]interface{})["message"] {
t.Error("chain should contain effect1 second")
}
if "cause" != traces[2]["exception"].(map[string]interface{})["message"] {
t.Error("chain should contain cause third")
}
if effect2.Stack().Fingerprint()+effect.Stack().Fingerprint()+"0" != fingerprint {
t.Error("fingerprint should be the fingerprints in chain concatenated together. got: ", fingerprint)
}
}