-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryApolloInterceptorTest.kt
218 lines (194 loc) · 6.68 KB
/
SentryApolloInterceptorTest.kt
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
package io.sentry.apollo
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.coroutines.await
import com.apollographql.apollo.exception.ApolloException
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.check
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.Breadcrumb
import io.sentry.IHub
import io.sentry.ITransaction
import io.sentry.SentryOptions
import io.sentry.SentryTraceHeader
import io.sentry.SentryTracer
import io.sentry.SpanStatus
import io.sentry.TraceContext
import io.sentry.TracesSamplingDecision
import io.sentry.TransactionContext
import io.sentry.protocol.SentryTransaction
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
class SentryApolloInterceptorTest {
class Fixture {
val server = MockWebServer()
val hub = mock<IHub>()
private var interceptor = SentryApolloInterceptor(hub)
@SuppressWarnings("LongParameterList")
fun getSut(
httpStatusCode: Int = 200,
responseBody: String = """{
"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"
}
}
}
}""",
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
beforeSpan: SentryApolloInterceptor.BeforeSpanCallback? = null
): ApolloClient {
whenever(hub.options).thenReturn(SentryOptions())
server.enqueue(
MockResponse()
.setBody(responseBody)
.setSocketPolicy(socketPolicy)
.setResponseCode(httpStatusCode)
)
if (beforeSpan != null) {
interceptor = SentryApolloInterceptor(hub, beforeSpan)
}
return ApolloClient.builder()
.serverUrl(server.url("/"))
.addApplicationInterceptor(interceptor)
.build()
}
}
private val fixture = Fixture()
@Test
fun `creates a span around the successful request`() {
executeQuery()
verify(fixture.hub).captureTransaction(
check {
assertTransactionDetails(it)
assertEquals(SpanStatus.OK, it.spans.first().status)
},
anyOrNull<TraceContext>(),
anyOrNull(),
anyOrNull()
)
}
@Test
fun `creates a span around the failed request`() {
executeQuery(fixture.getSut(httpStatusCode = 403))
verify(fixture.hub).captureTransaction(
check {
assertTransactionDetails(it)
assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status)
},
anyOrNull<TraceContext>(),
anyOrNull(),
anyOrNull()
)
}
@Test
fun `creates a span around the request failing with network error`() {
executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY))
verify(fixture.hub).captureTransaction(
check {
assertTransactionDetails(it)
assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status)
},
anyOrNull<TraceContext>(),
anyOrNull(),
anyOrNull()
)
}
@Test
fun `when there is no active span, does not add sentry trace header to the request`() {
executeQuery(isSpanActive = false)
val recorderRequest = fixture.server.takeRequest()
assertNull(recorderRequest.headers[SentryTraceHeader.SENTRY_TRACE_HEADER])
}
@Test
fun `when there is an active span, adds sentry trace headers to the request`() {
executeQuery()
val recorderRequest = fixture.server.takeRequest()
assertNotNull(recorderRequest.headers[SentryTraceHeader.SENTRY_TRACE_HEADER])
}
@Test
fun `customizer modifies span`() {
executeQuery(
fixture.getSut { span, _, _ ->
span.description = "overwritten description"
span
}
)
verify(fixture.hub).captureTransaction(
check {
assertEquals(1, it.spans.size)
val httpClientSpan = it.spans.first()
assertEquals("overwritten description", httpClientSpan.description)
},
anyOrNull<TraceContext>(),
anyOrNull(),
anyOrNull()
)
}
@Test
fun `when customizer throws, exception is handled`() {
executeQuery(
fixture.getSut { _, _, _ -> throw RuntimeException() }
)
verify(fixture.hub).captureTransaction(
check {
assertEquals(1, it.spans.size)
},
anyOrNull<TraceContext>(),
anyOrNull(),
anyOrNull()
)
}
@Test
fun `adds breadcrumb when http calls succeeds`() {
executeQuery()
verify(fixture.hub).addBreadcrumb(
check<Breadcrumb> {
assertEquals("http", it.type)
assertEquals(280L, it.data["response_body_size"])
assertEquals(193L, it.data["request_body_size"])
},
anyOrNull()
)
}
private fun assertTransactionDetails(it: SentryTransaction) {
assertEquals(1, it.spans.size)
val httpClientSpan = it.spans.first()
assertEquals("LaunchDetails", httpClientSpan.op)
assertEquals("query LaunchDetails", httpClientSpan.description)
assertNotNull(httpClientSpan.data) {
assertNotNull(it["operationId"])
assertEquals("{id=83}", it["variables"])
}
}
private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true) = runBlocking {
var tx: ITransaction? = null
if (isSpanActive) {
tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub)
whenever(fixture.hub.span).thenReturn(tx)
}
val coroutine = launch {
try {
sut.query(LaunchDetailsQuery.builder().id("83").build()).await()
} catch (e: ApolloException) {
return@launch
}
}
coroutine.join()
tx?.finish()
}
}