forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadcrumbs_test.go
273 lines (228 loc) · 7.77 KB
/
breadcrumbs_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
package sentry
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func ExampleDefaultBreadcrumbs() {
// We can change the maximum number of breadcrumbs to be stored
DefaultBreadcrumbs().WithSize(5)
DefaultBreadcrumbs().NewDefault(nil).WithMessage("This is an example")
DefaultBreadcrumbs().NewDefault(map[string]interface{}{
"example": true,
}).WithMessage("It should give you an idea of how you can use breadcrumbs in your app")
DefaultBreadcrumbs().
NewNavigation("introduction", "navigation").
WithCategory("navigation").
WithMessage("You can use them to represent navigations from one page to another")
DefaultBreadcrumbs().
NewNavigation("navigation", "http").
WithCategory("navigation").
WithMessage("Or to represent changes in the state of your application's workflows")
DefaultBreadcrumbs().
NewHTTPRequest("GET", "https://example.com/api/v1/awesome", 200, "OK").
WithLevel(Debug).
WithMessage("I think we can agree that they're pretty awesome")
NewClient().Capture(Message("Finally, we send the event with all our breadcrumbs included"))
}
func ExampleBreadcrumbs() {
rootClient := NewClient()
DefaultBreadcrumbs().NewDefault(nil).WithMessage("Breadcrumb in the default context")
breadcrumbs := NewBreadcrumbsList(10)
contextClient := rootClient.With(Breadcrumbs(breadcrumbs))
breadcrumbs.NewDefault(nil).WithMessage("Breadcrumb in the private context")
// Will include only the first breadcrumb
rootClient.Capture(
Message("Event in default context"),
Logger("default"),
)
// Will include only the second breadcrumb
contextClient.Capture(
Message("Event in private context"),
Logger("private"),
)
}
func TestBreadcrumbs(t *testing.T) {
Convey("Breadcrumbs", t, func() {
Convey("Should be registered as a default options provider", func() {
provider := testGetOptionsProvider(&breadcrumbsList{})
So(provider, ShouldNotBeNil)
})
Convey("Should expose a DefaultBreadcrumbs() collection", func() {
So(DefaultBreadcrumbs(), ShouldNotBeNil)
So(DefaultBreadcrumbs(), ShouldImplement, (*BreadcrumbsList)(nil))
})
Convey("Breadcrumbs()", func() {
Convey("Should use the correct Class()", func() {
l := NewBreadcrumbsList(3)
So(l, ShouldNotBeNil)
So(Breadcrumbs(l).Class(), ShouldEqual, "breadcrumbs")
})
Convey("Should return nil if the list is nil", func() {
So(Breadcrumbs(nil), ShouldBeNil)
})
})
Convey("NewBreadcrumbsList()", func() {
l := NewBreadcrumbsList(3)
So(l, ShouldNotBeNil)
ll, ok := l.(*breadcrumbsList)
So(ok, ShouldBeTrue)
Convey("Should implement Option interface", func() {
So(l, ShouldImplement, (*Option)(nil))
})
Convey("Should initialize correctly", func() {
So(ll.MaxLength, ShouldEqual, 3)
So(ll.Length, ShouldEqual, 0)
So(ll.Head, ShouldBeNil)
So(ll.Tail, ShouldBeNil)
})
Convey("NewDefault()", func() {
data := map[string]interface{}{
"test": true,
}
Convey("Should return a Breadcrumb", func() {
b := l.NewDefault(data)
So(b, ShouldImplement, (*Breadcrumb)(nil))
})
Convey("Should use a default breadcrumb type", func() {
b := l.NewDefault(data)
bb, ok := b.(*breadcrumb)
So(ok, ShouldBeTrue)
So(bb.Type, ShouldEqual, "")
So(bb.Data, ShouldEqual, data)
})
Convey("Should allow you to specify nil for default data", func() {
b := l.NewDefault(nil)
bb, ok := b.(*breadcrumb)
So(ok, ShouldBeTrue)
So(bb.Data, ShouldResemble, map[string]interface{}{})
})
Convey("Should insert the breadcrumb into the list at its tail", func() {
b := l.NewDefault(data)
So(ll.Length, ShouldEqual, 1)
So(ll.Tail, ShouldNotBeNil)
So(ll.Tail.Value, ShouldEqual, b)
})
})
Convey("NewNavigation()", func() {
Convey("Should return a Breadcrumb", func() {
b := l.NewNavigation("/from", "/to")
So(b, ShouldImplement, (*Breadcrumb)(nil))
})
Convey("Should use a navigation breadcrumb type", func() {
b := l.NewNavigation("/from", "/to")
bb, ok := b.(*breadcrumb)
So(ok, ShouldBeTrue)
So(bb.Type, ShouldEqual, "navigation")
So(bb.Data, ShouldResemble, map[string]interface{}{
"from": "/from",
"to": "/to",
})
})
Convey("Should insert the breadcrumb into the list at its tail", func() {
b := l.NewNavigation("/from", "/to")
So(ll.Length, ShouldEqual, 1)
So(ll.Tail, ShouldNotBeNil)
So(ll.Tail.Value, ShouldEqual, b)
})
})
Convey("NewHTTPRequest()", func() {
Convey("Should return a Breadcrumb", func() {
b := l.NewHTTPRequest("GET", "/test", 200, "OK")
So(b, ShouldImplement, (*Breadcrumb)(nil))
})
Convey("Should use a navigation breadcrumb type", func() {
b := l.NewHTTPRequest("GET", "/test", 200, "OK")
bb, ok := b.(*breadcrumb)
So(ok, ShouldBeTrue)
So(bb.Type, ShouldEqual, "http")
So(bb.Data, ShouldResemble, map[string]interface{}{
"method": "GET",
"url": "/test",
"status_code": 200,
"reason": "OK",
})
})
Convey("Should insert the breadcrumb into the list at its tail", func() {
b := l.NewHTTPRequest("GET", "/test", 200, "OK")
So(ll.Length, ShouldEqual, 1)
So(ll.Tail, ShouldNotBeNil)
So(ll.Tail.Value, ShouldEqual, b)
})
})
Convey("WithSize()", func() {
Convey("Should be chainable", func() {
So(l.WithSize(5), ShouldEqual, l)
})
Convey("Should update the max length field", func() {
So(ll.MaxLength, ShouldEqual, 3)
l.WithSize(5)
So(ll.MaxLength, ShouldEqual, 5)
})
Convey("Should remove elements which push the length over the limit", func() {
var b Breadcrumb
for i := 0; i < 3; i++ {
b = l.NewDefault(map[string]interface{}{
"index": i,
})
So(b, ShouldNotBeNil)
}
So(ll.Length, ShouldEqual, 3)
l.WithSize(1)
So(ll.Length, ShouldEqual, 1)
So(ll.Head, ShouldNotBeNil)
So(ll.Head.Next, ShouldBeNil)
So(ll.Head.Value, ShouldEqual, b)
So(b, ShouldHaveSameTypeAs, &breadcrumb{})
So(b.(*breadcrumb).Data, ShouldResemble, map[string]interface{}{
"index": 2,
})
})
})
Convey("append()", func() {
Convey("Should evict older entries when we run over the max length", func() {
var b Breadcrumb
for i := 0; i < 10; i++ {
b = l.NewDefault(map[string]interface{}{
"index": i,
})
So(b, ShouldNotBeNil)
}
So(ll.Length, ShouldEqual, 3)
So(b, ShouldHaveSameTypeAs, &breadcrumb{})
So(b.(*breadcrumb).Data, ShouldResemble, map[string]interface{}{
"index": 9,
})
})
})
Convey("list()", func() {
Convey("Should handle an empty list correctly", func() {
ol := ll.list()
So(ol, ShouldNotBeNil)
So(ol, ShouldHaveLength, 0)
})
Convey("Should expose a non-empty list correctly", func() {
for i := 0; i < 3; i++ {
So(l.NewDefault(map[string]interface{}{
"index": i,
}), ShouldNotBeNil)
}
ol := ll.list()
So(ol, ShouldNotBeNil)
So(ol, ShouldHaveLength, 3)
for i, item := range ol {
So(item, ShouldHaveSameTypeAs, &breadcrumb{})
So(item.(*breadcrumb).Data, ShouldContainKey, "index")
So(item.(*breadcrumb).Data["index"], ShouldEqual, i)
}
})
})
})
Convey("MarshalJSON", func() {
l := NewBreadcrumbsList(5)
l.NewDefault(map[string]interface{}{"test": true})
data := testOptionsSerialize(Breadcrumbs(l))
So(data, ShouldNotBeNil)
So(data, ShouldHaveSameTypeAs, []interface{}{})
})
})
}