Repository navigation
Expand file tree
/
Copy pathSentryApollo4BuilderExtensionsClientErrorsTest.kt
More file actions
537 lines (454 loc) · 14.8 KB
/
Copy pathSentryApollo4BuilderExtensionsClientErrorsTest.kt
File metadata and controls
537 lines (454 loc) · 14.8 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
package io.sentry.apollo4
import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.ApolloResponse
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.http.HttpRequest
import com.apollographql.apollo.api.http.HttpResponse
import com.apollographql.apollo.exception.ApolloException
import io.sentry.Hint
import io.sentry.HttpBodyType
import io.sentry.IScopes
import io.sentry.KeyValueCollectionBehavior
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryOptions
import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS
import io.sentry.TypeCheckHint
import io.sentry.apollo4.SentryApollo4HttpInterceptor.Companion.DEFAULT_CAPTURE_FAILED_REQUESTS
import io.sentry.apollo4.generated.LaunchDetailsQuery
import io.sentry.exception.ExceptionMechanismException
import io.sentry.protocol.SdkVersion
import io.sentry.protocol.SentryId
import kotlin.reflect.KSuspendFunction1
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
import org.mockito.kotlin.any
import org.mockito.kotlin.check
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
class SentryApollo4BuilderExtensionsClientErrorsTestWithV4Implementation :
SentryApollo4BuilderExtensionsClientErrorsTest(ApolloCall<*>::execute)
class SentryApollo4BuilderExtensionsClientErrorsTestWithV3Implementation :
SentryApollo4BuilderExtensionsClientErrorsTest(ApolloCall<*>::executeV3)
abstract class SentryApollo4BuilderExtensionsClientErrorsTest(
private val executeQueryImplementation:
KSuspendFunction1<ApolloCall<*>, ApolloResponse<out Operation.Data>>
) {
class Fixture {
val server = MockWebServer()
lateinit var scopes: IScopes
private val responseBodyOk =
"""{
"data": {
"launch": {
"__typename": "Launch",
"id": "83",
"site": "CCAFS SLC 40",
"mission": {
"__typename": "Mission",
"name": "Amos-17",
"missionPatch": "https://images2.imgbox.com/a0/ab/XUoByiuR_o.png"
}
}
}
}"""
val responseBodyNotOk =
"""{
"errors": [
{
"message": "Cannot query field \"mySite\" on type \"Launch\". Did you mean \"site\"?",
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED"
}
}
]
}"""
fun getSut(
captureFailedRequests: Boolean = DEFAULT_CAPTURE_FAILED_REQUESTS,
failedRequestTargets: List<String> = listOf(DEFAULT_PROPAGATION_TARGETS),
httpStatusCode: Int = 200,
responseBody: String = responseBodyOk,
sendDefaultPii: Boolean = false,
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
configureOptions: SentryOptions.() -> Unit = {},
): ApolloClient {
SentryIntegrationPackageStorage.getInstance().clearStorage()
scopes =
mock<IScopes>().apply {
whenever(options)
.thenReturn(
SentryOptions().apply {
dsn = "https://key@sentry.io/proj"
sdkVersion = SdkVersion("test", "1.2.3")
isSendDefaultPii = sendDefaultPii
configureOptions()
}
)
}
whenever(scopes.captureEvent(any(), any<Hint>())).thenReturn(SentryId.EMPTY_ID)
val response =
MockResponse()
.setBody(responseBody)
.setSocketPolicy(socketPolicy)
.setResponseCode(httpStatusCode)
if (sendDefaultPii) {
response.addHeader("Set-Cookie", "Test")
}
server.enqueue(response)
val builder =
ApolloClient.Builder()
.serverUrl(server.url("?myQuery=query#myFragment").toString())
.sentryTracing(
scopes = scopes,
captureFailedRequests = captureFailedRequests,
failedRequestTargets = failedRequestTargets,
)
if (sendDefaultPii) {
builder.addHttpHeader("Cookie", "Test")
}
return builder.build()
}
}
private val fixture = Fixture()
// region captureFailedRequests
@Test
fun `does not capture errors if captureFailedRequests is disabled`() {
val sut =
fixture.getSut(captureFailedRequests = false, responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes, never()).captureEvent(any(), any<Hint>())
}
@Test
fun `capture errors if captureFailedRequests is enabled`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
}
// endregion
// region Apollo4ClientError
@Test
fun `does not add Apollo4ClientError integration if captureFailedRequests is disabled`() {
fixture.getSut(captureFailedRequests = false)
assertFalse(
SentryIntegrationPackageStorage.getInstance().integrations.contains("Apollo4ClientError")
)
}
@Test
fun `adds Apollo4ClientError integration if captureFailedRequests is enabled`() {
fixture.getSut()
assertTrue(
SentryIntegrationPackageStorage.getInstance().integrations.contains("Apollo4ClientError")
)
}
// endregion
// region failedRequestTargets
@Test
fun `does not capture errors if failedRequestTargets does not match`() {
val sut =
fixture.getSut(
failedRequestTargets = listOf("nope.com"),
responseBody = fixture.responseBodyNotOk,
)
executeQuery(sut)
verify(fixture.scopes, never()).captureEvent(any(), any<Hint>())
}
@Test
fun `capture errors if failedRequestTargets matches`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
}
// endregion
// region SentryEvent
@Test
fun `capture errors with SentryApollo4Interceptor mechanism`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val throwable = (it.throwableMechanism as ExceptionMechanismException)
assertEquals("SentryApollo4Interceptor", throwable.exceptionMechanism.type)
},
any<Hint>(),
)
}
@Test
fun `capture errors with title`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val throwable = (it.throwableMechanism as ExceptionMechanismException)
assertEquals(
"GraphQL Request failed, name: LaunchDetails, type: query",
throwable.throwable.message,
)
},
any<Hint>(),
)
}
@Test
fun `capture errors with snapshot flag set`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val throwable = (it.throwableMechanism as ExceptionMechanismException)
assertTrue(throwable.isSnapshot)
},
any<Hint>(),
)
}
private val escapeDolar = "\$id"
@Test
fun `capture errors with request context`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
val body =
"""
{"operationName":"LaunchDetails","variables":{"id":"83"},"query":"query LaunchDetails($escapeDolar: ID!) { launch(id: $escapeDolar) { id site mission { name missionPatch(size: LARGE) } rocket { name type } } }"}
"""
.trimIndent()
verify(fixture.scopes)
.captureEvent(
check {
val request = it.request!!
assertEquals("http://localhost:${fixture.server.port}/", request.url)
assertEquals("myQuery=query", request.queryString)
assertEquals("myFragment", request.fragment)
assertEquals("Post", request.method)
assertEquals("graphql", request.apiTarget)
assertEquals(193L, request.bodySize)
assertEquals(body, request.data)
assertNull(request.cookies)
assertNull(request.headers)
},
any<Hint>(),
)
}
@Test
fun `data collection can disable outgoing request body`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_RESPONSE)
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
assertEquals(193L, it.request!!.bodySize)
assertNull(it.request!!.data)
},
any<Hint>(),
)
}
@Test
fun `data collection can disable the GraphQL document independently`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.graphql.setDocument(false)
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val body = it.request!!.data as String
assertFalse(body.contains("\"query\""))
assertTrue(body.contains("\"variables\""))
},
any<Hint>(),
)
}
@Test
fun `data collection can disable GraphQL variables independently`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.graphql.setVariables(false)
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val body = it.request!!.data as String
assertTrue(body.contains("\"query\""))
assertFalse(body.contains("\"variables\""))
},
any<Hint>(),
)
}
@Test
fun `data collection can disable the GraphQL request body`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.graphql.setDocument(false)
dataCollection.graphql.setVariables(false)
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check { assertNull(it.request!!.data) },
any<Hint>(),
)
}
@Test
fun `data collection filters request headers`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.httpHeaders.request = KeyValueCollectionBehavior.denyList("accept")
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
assertEquals("[Filtered]", it.request!!.headers?.get("Accept"))
},
any<Hint>(),
)
}
@Test
fun `data collection can disable request headers`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.httpHeaders.request = KeyValueCollectionBehavior.off()
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check { assertTrue(it.request!!.headers!!.isEmpty()) },
any<Hint>(),
)
}
@Test
fun `capture errors with more request context if sendDefaultPii is enabled`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val request = it.request!!
assertEquals("Test", request.cookies)
assertNotNull(request.headers)
},
any<Hint>(),
)
}
@Test
fun `capture errors with response context`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val response = it.contexts.response!!
assertEquals(200, response.statusCode)
assertEquals(200, response.bodySize)
assertEquals(fixture.responseBodyNotOk, response.data)
assertNull(response.cookies)
assertNull(response.headers)
},
any<Hint>(),
)
}
@Test
fun `data collection can disable incoming response body`() {
val sut =
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
dataCollection.httpBodies = emptySet()
}
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val response = it.contexts.response!!
assertEquals(200, response.statusCode)
assertEquals(200, response.bodySize)
assertNull(response.data)
},
any<Hint>(),
)
}
@Test
fun `capture errors with more response context if sendDefaultPii is enabled`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check {
val response = it.contexts.response!!
assertEquals("Test", response.cookies)
assertNotNull(response.headers)
assertEquals(200, response.headers?.get("Content-Length")?.toInt())
},
any<Hint>(),
)
}
@Test
fun `capture errors with specific fingerprints`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
check { assertEquals(listOf("LaunchDetails", "query", "200"), it.fingerprints) },
any<Hint>(),
)
}
// endregion
// region errors
@Test
fun `capture errors if response code is equal or higher than 400`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk, httpStatusCode = 500)
executeQuery(sut)
// HttpInterceptor does not throw for >= 400
verify(fixture.scopes).captureEvent(any(), any<Hint>())
}
@Test
fun `capture errors swallow any exception during the error transformation`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
whenever(fixture.scopes.captureEvent(any(), any<Hint>())).thenThrow(RuntimeException())
executeQuery(sut)
}
// endregion
// region hints
@Test
fun `hints are set when capturing errors`() {
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk)
executeQuery(sut)
verify(fixture.scopes)
.captureEvent(
any(),
check<Hint> {
val request = it.get(TypeCheckHint.APOLLO_REQUEST)
assertNotNull(request)
assertTrue(request is HttpRequest)
val response = it.get(TypeCheckHint.APOLLO_RESPONSE)
assertNotNull(response)
assertTrue(response is HttpResponse)
},
)
}
// endregion
private fun executeQuery(sut: ApolloClient, id: String = "83") = runBlocking {
val coroutine = launch {
try {
executeQueryImplementation(sut.query(LaunchDetailsQuery(id)))
} catch (e: ApolloException) {
return@launch
}
}
coroutine.join()
}
}