This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
rollbar_test.go
201 lines (160 loc) · 3.99 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
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) {
Error("error", errors.New(s))
}
func testErrorStackWithSkip(s string) {
testErrorStackWithSkip2(s)
}
func testErrorStackWithSkip2(s string) {
ErrorWithStackSkip("error", errors.New(s), 2)
}
func TestErrorClass(t *testing.T) {
errors := map[string]error{
// generic error
"errors.errorString": fmt.Errorf("Something is broken!"),
// custom error
"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) {
Token = os.Getenv("TOKEN")
Environment = "test"
Error("critical", errors.New("Normal critical error"))
Error("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")
// If you don't see the message sent on line 65 in Rollbar, that means this
// is broken:
Wait()
}
func TestErrorRequest(t *testing.T) {
r, _ := http.NewRequest("GET", "http://foo.com/somethere?param1=true", nil)
r.RemoteAddr = "1.1.1.1:123"
object := errorRequest(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": []string{"one"},
"ok": []string{"one"},
"access_token": []string{"one"},
}
clean := filterParams(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": []string{"one"},
"b": []string{"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")
}
}
func TestBuildError(t *testing.T) {
buildError(ERR, nil, BuildStack(0))
// this should not panic
}
func TestCustomField(t *testing.T) {
body := buildError(ERR, errors.New("test-custom"), BuildStack(0), &Field{
Name: "custom",
Data: map[string]string{
"NAME1": "VALUE1",
},
})
dataField, ok := body["data"]
if !ok {
t.Error("should have field 'data'")
}
data, ok := dataField.(map[string]interface{})
if !ok {
t.Error("should be of type map[string]interface{}")
}
custom, ok := data["custom"]
if !ok {
t.Error("should have field 'custom'")
}
customMap, ok := custom.(map[string]string)
if !ok {
t.Error("should be a map[string]string")
}
val, ok := customMap["NAME1"]
if !ok {
t.Error("should have a key 'NAME1'")
}
if val != "VALUE1" {
t.Error("should be VALUE1")
}
}
func TestErrorRead(t *testing.T) {
Token = os.Getenv("TOKEN")
Environment = "test"
bckBuffer, bckEP := Buffer, Endpoint
defer func() {
Buffer, Endpoint = bckBuffer, bckEP
}()
Buffer = 2
Endpoint = "https://does.not.exsist/foo/bar"
go func() {
errCount := 0
for err := range PostErrors() {
t.Log(err)
errCount++
}
if errCount != 2 {
t.Fatal("didn't receive the right number of errors", errCount)
}
}()
post(nil)
post(nil)
Wait()
}