-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathDefaultReplayBreadcrumbConverterTest.kt
More file actions
577 lines (475 loc) · 17.6 KB
/
DefaultReplayBreadcrumbConverterTest.kt
File metadata and controls
577 lines (475 loc) · 17.6 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package io.sentry.android.replay
import io.sentry.Breadcrumb
import io.sentry.Hint
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.SpanDataConvention
import io.sentry.TypeCheckHint.SENTRY_REPLAY_NETWORK_DETAILS
import io.sentry.rrweb.RRWebBreadcrumbEvent
import io.sentry.rrweb.RRWebSpanEvent
import io.sentry.util.network.NetworkBody
import io.sentry.util.network.NetworkRequestData
import io.sentry.util.network.ReplayNetworkRequestOrResponse
import java.util.Date
import junit.framework.TestCase.assertEquals
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertSame
class DefaultReplayBreadcrumbConverterTest {
class Fixture {
fun getSut(options: SentryOptions? = null): DefaultReplayBreadcrumbConverter =
if (options != null) {
DefaultReplayBreadcrumbConverter(options)
} else {
DefaultReplayBreadcrumbConverter()
}
}
private val fixture = Fixture()
@Test
fun `returns null when no category`() {
val converter = fixture.getSut()
val breadcrumb = Breadcrumb(Date(123L)).apply { message = "message" }
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `convert RRWebSpanEvent`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "http"
data["url"] = "http://example.com"
data["status_code"] = 404
data["method"] = "GET"
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1234L
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2234L
data["http.response_content_length"] = 300
data["http.request_content_length"] = 400
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebSpanEvent)
assertEquals("resource.http", rrwebEvent.op)
assertEquals("http://example.com", rrwebEvent.description)
assertEquals(123L, rrwebEvent.timestamp)
assertEquals(1.234, rrwebEvent.startTimestamp)
assertEquals(2.234, rrwebEvent.endTimestamp)
assertEquals(404, rrwebEvent.data!!["statusCode"])
assertEquals("GET", rrwebEvent.data!!["method"])
assertEquals(300, rrwebEvent.data!!["responseBodySize"])
assertEquals(400, rrwebEvent.data!!["requestBodySize"])
}
@Test
fun `convert RRWebSpanEvent works with floating timestamps`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "http"
data["url"] = "http://example.com"
data["status_code"] = 404
data["method"] = "GET"
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1234.0
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2234.0
data["http.response_content_length"] = 300
data["http.request_content_length"] = 400
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebSpanEvent)
assertEquals(1.234, rrwebEvent.startTimestamp)
assertEquals(2.234, rrwebEvent.endTimestamp)
}
@Test
fun `returns null if not eligible for RRWebSpanEvent`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "http"
data["status_code"] = 404
data["method"] = "GET"
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1234L
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2234L
data["http.response_content_length"] = 300
data["http.request_content_length"] = 400
}
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `converts app lifecycle breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "app.lifecycle"
type = "navigation"
data["state"] = "background"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("app.background", rrwebEvent.category)
assertEquals(123L, rrwebEvent.timestamp)
assertEquals(0.123, rrwebEvent.breadcrumbTimestamp)
assertEquals("default", rrwebEvent.breadcrumbType)
}
@Test
fun `converts device orientation breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "device.orientation"
type = "navigation"
data["position"] = "landscape"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("device.orientation", rrwebEvent.category)
assertEquals("landscape", rrwebEvent.data!!["position"])
}
@Test
fun `returns null if no position for orientation breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "device.orientation"
type = "navigation"
}
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `converts navigation breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "navigation"
type = "navigation"
data["state"] = "resumed"
data["screen"] = "io.sentry.MainActivity"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("navigation", rrwebEvent.category)
assertEquals("MainActivity", rrwebEvent.data!!["to"])
}
@Test
fun `converts navigation breadcrumbs with destination`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "navigation"
type = "navigation"
data["to"] = "/github"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("navigation", rrwebEvent.category)
assertEquals("/github", rrwebEvent.data!!["to"])
}
@Test
fun `returns null when lifecycle state is not 'resumed'`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "navigation"
type = "navigation"
data["state"] = "started"
data["screen"] = "io.sentry.MainActivity"
}
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `converts ui click breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "ui.click"
type = "user"
data["view.id"] = "button_login"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("ui.tap", rrwebEvent.category)
assertEquals("button_login", rrwebEvent.message)
}
@Test
fun `returns null if no view identifier in data`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "ui.click"
type = "user"
}
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `converts network connectivity breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "network.event"
type = "system"
data["network_type"] = "cellular"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("device.connectivity", rrwebEvent.category)
assertEquals("cellular", rrwebEvent.data!!["state"])
}
@Test
fun `returns null if no network connectivity state`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "network.event"
type = "system"
}
val rrwebEvent = converter.convert(breadcrumb)
assertNull(rrwebEvent)
}
@Test
fun `converts battery status breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "device.event"
type = "system"
data["action"] = "BATTERY_CHANGED"
data["level"] = 85.0f
data["charging"] = true
data["stuff"] = "shiet"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("device.battery", rrwebEvent.category)
assertEquals(85.0f, rrwebEvent.data!!["level"])
assertEquals(true, rrwebEvent.data!!["charging"])
assertNull(rrwebEvent.data!!["stuff"])
}
@Test
fun `converts generic breadcrumbs`() {
val converter = fixture.getSut()
val breadcrumb =
Breadcrumb(Date(123L)).apply {
category = "device.event"
type = "system"
message = "message"
level = SentryLevel.ERROR
data["stuff"] = "shiet"
}
val rrwebEvent = converter.convert(breadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("device.event", rrwebEvent.category)
assertEquals("message", rrwebEvent.message)
assertEquals(SentryLevel.ERROR, rrwebEvent.level)
assertEquals("shiet", rrwebEvent.data!!["stuff"])
}
// BeforeBreadcrumbCallback delegation tests
@Test
fun `ReplayBeforeBreadcrumb does not modify breadcrumb__no user-provided BeforeBreadcrumbCallback`() {
// Create options with no beforeBreadcrumb callback
val options = SentryOptions.empty()
options.beforeBreadcrumb = null
DefaultReplayBreadcrumbConverter(options)
val breadcrumb =
Breadcrumb(Date()).apply {
message = "test message"
category = "test.category"
}
val hint = Hint()
val result = options.beforeBreadcrumb?.execute(breadcrumb, hint)
assertSame(breadcrumb, result)
}
@Test
fun `ReplayBeforeBreadcrumb delegates to user-provided BeforeBreadcrumbCallback`() {
val originalBreadcrumb =
Breadcrumb(Date()).apply {
message = "original message"
category = "original.category"
}
val userModifiedBreadcrumb =
Breadcrumb(Date()).apply {
message = "modified message"
category = "modified.category"
}
// Set up options with a user callback that returns modified breadcrumb
val userBeforeBreadcrumbCallback =
SentryOptions.BeforeBreadcrumbCallback { _, _ -> userModifiedBreadcrumb }
val options = SentryOptions.empty()
options.beforeBreadcrumb = userBeforeBreadcrumbCallback
DefaultReplayBreadcrumbConverter(options)
// user-provided SentryOptions beforeBreadcrumb is replaced.
assertNotSame(userBeforeBreadcrumbCallback, options.beforeBreadcrumb)
// SentryOptions#beforeBreadcrumb still respects user-provided beforeBreadcrumb
val result = options.beforeBreadcrumb?.execute(originalBreadcrumb, Hint())
assertSame(userModifiedBreadcrumb, result)
}
@Test
fun `ReplayBeforeBreadcrumb handles user-provided BeforeBreadcrumbCallback returning null`() {
val breadcrumb =
Breadcrumb(Date()).apply {
message = "test message"
category = "test.category"
}
val options = SentryOptions.empty()
val userCallback = SentryOptions.BeforeBreadcrumbCallback { _, _ -> null }
options.beforeBreadcrumb = userCallback
fixture.getSut(options)
// user-provided SentryOptions beforeBreadcrumb is replaced.
assertNotSame(userCallback, options.beforeBreadcrumb)
val result = options.beforeBreadcrumb?.execute(breadcrumb, Hint())
assertNull(result)
}
@Test
fun `converts network details data__with user-provided BeforeBreadcrumbCallback`() {
val options = SentryOptions.empty()
val userCallback = SentryOptions.BeforeBreadcrumbCallback { b, _ -> b }
options.beforeBreadcrumb = userCallback
val converter = fixture.getSut(options)
val httpBreadcrumb =
Breadcrumb(Date(123L)).apply {
type = "http"
category = "http"
data["url"] = "https://example.com"
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1000L
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2000L
}
val fakeOkHttpNetworkDetails = NetworkRequestData("POST")
fakeOkHttpNetworkDetails.setRequestDetails(
ReplayNetworkRequestOrResponse(
100L,
NetworkBody("request body content"),
mapOf("Content-Type" to "application/json"),
)
)
fakeOkHttpNetworkDetails.setResponseDetails(
200,
ReplayNetworkRequestOrResponse(
500L,
NetworkBody(mapOf("status" to "success", "message" to "OK")),
mapOf("Content-Type" to "text/plain"),
),
)
val hintWithFakeOKHttpNetworkDetails = Hint()
hintWithFakeOKHttpNetworkDetails.set(SENTRY_REPLAY_NETWORK_DETAILS, fakeOkHttpNetworkDetails)
options.beforeBreadcrumb?.execute(httpBreadcrumb, hintWithFakeOKHttpNetworkDetails)
// Verify NetworkDetails is properly extracted
val rrwebEvent = converter.convert(httpBreadcrumb)
check(rrwebEvent is RRWebSpanEvent)
// Meta data
assertEquals("POST", rrwebEvent.data!!["method"])
assertEquals(200, rrwebEvent.data!!["statusCode"])
assertEquals(100L, rrwebEvent.data!!["requestBodySize"])
assertEquals(500L, rrwebEvent.data!!["responseBodySize"])
// Request data
val requestData = rrwebEvent.data!!["request"] as? Map<*, *>
assertNotNull(requestData)
assertEquals(100L, requestData["size"])
assertEquals("request body content", requestData["body"])
assertEquals(mapOf("Content-Type" to "application/json"), requestData["headers"])
// Response data
val responseData = rrwebEvent.data!!["response"] as? Map<*, *>
assertNotNull(responseData)
assertEquals(500L, responseData["size"])
assertEquals(mapOf("status" to "success", "message" to "OK"), responseData["body"])
assertEquals(mapOf("Content-Type" to "text/plain"), responseData["headers"])
}
@Test
fun `converts network details data__no user-provided BeforeBreadcrumbCallback`() {
val options = SentryOptions.empty()
val userCallback = null
options.beforeBreadcrumb = userCallback
val converter = fixture.getSut(options)
val httpBreadcrumb =
Breadcrumb(Date(123L)).apply {
type = "http"
category = "http"
data["url"] = "https://example.com"
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1000L
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2000L
}
val fakeOkHttpNetworkDetails = NetworkRequestData("POST")
fakeOkHttpNetworkDetails.setRequestDetails(
ReplayNetworkRequestOrResponse(
150L,
NetworkBody(listOf("item1", "item2", "item3")),
mapOf("Content-Type" to "application/json"),
)
)
fakeOkHttpNetworkDetails.setResponseDetails(
404,
ReplayNetworkRequestOrResponse(
550L,
NetworkBody(mapOf("status" to "success", "message" to "OK")),
mapOf("Content-Type" to "text/plain"),
),
)
val hintWithFakeOKHttpNetworkDetails = Hint()
hintWithFakeOKHttpNetworkDetails.set(SENTRY_REPLAY_NETWORK_DETAILS, fakeOkHttpNetworkDetails)
options.beforeBreadcrumb?.execute(httpBreadcrumb, hintWithFakeOKHttpNetworkDetails)
// Verify NetworkDetails is properly extracted
val rrwebEvent = converter.convert(httpBreadcrumb)
check(rrwebEvent is RRWebSpanEvent)
// Meta data
assertEquals("POST", rrwebEvent.data!!["method"])
assertEquals(404, rrwebEvent.data!!["statusCode"])
assertEquals(150L, rrwebEvent.data!!["requestBodySize"])
assertEquals(550L, rrwebEvent.data!!["responseBodySize"])
// Request data
val requestData = rrwebEvent.data!!["request"] as? Map<*, *>
assertNotNull(requestData)
assertEquals(150L, requestData["size"])
assertEquals(listOf("item1", "item2", "item3"), requestData["body"])
assertEquals(mapOf("Content-Type" to "application/json"), requestData["headers"])
// Response data
val responseData = rrwebEvent.data!!["response"] as? Map<*, *>
assertNotNull(responseData)
assertEquals(550L, responseData["size"])
assertEquals(mapOf("status" to "success", "message" to "OK"), responseData["body"])
assertEquals(mapOf("Content-Type" to "text/plain"), responseData["headers"])
}
@Test
fun `does not convert network details data for non-http breadcrumbs`() {
val navigationBreadcrumb =
Breadcrumb(Date()).apply {
type = "navigation"
category = "navigation"
data["to"] = "/home"
}
val hint = Hint()
val networkRequestData = NetworkRequestData("GET")
networkRequestData.setRequestDetails(
ReplayNetworkRequestOrResponse(
100L,
NetworkBody("request body content"),
mapOf("Content-Type" to "application/json"),
)
)
networkRequestData.setResponseDetails(
200,
ReplayNetworkRequestOrResponse(
100L,
NetworkBody("response body content"),
mapOf("Content-Type" to "application/json"),
),
)
hint.set(SENTRY_REPLAY_NETWORK_DETAILS, networkRequestData)
val options = SentryOptions.empty()
options.beforeBreadcrumb = null
val converter = fixture.getSut(options)
assertSame(navigationBreadcrumb, options.beforeBreadcrumb?.execute(navigationBreadcrumb, hint))
// Verify converter also doesn't include network details for non-http breadcrumbs
val rrwebEvent = converter.convert(navigationBreadcrumb)
check(rrwebEvent is RRWebBreadcrumbEvent)
assertEquals("navigation", rrwebEvent.category)
assertEquals("/home", rrwebEvent.data!!["to"])
// Verify no network-related data is present
assertNull(rrwebEvent.data!!["method"])
assertNull(rrwebEvent.data!!["statusCode"])
assertNull(rrwebEvent.data!!["requestBodySize"])
assertNull(rrwebEvent.data!!["responseBodySize"])
assertNull(rrwebEvent.data!!["request"])
assertNull(rrwebEvent.data!!["response"])
}
}