-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentrySpanClientHttpRequestInterceptorTest.kt
More file actions
78 lines (65 loc) · 2.59 KB
/
SentrySpanClientHttpRequestInterceptorTest.kt
File metadata and controls
78 lines (65 loc) · 2.59 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
package io.sentry.spring7.tracing
import io.sentry.IScopes
import io.sentry.Scope
import io.sentry.ScopeCallback
import io.sentry.Sentry
import io.sentry.SentryOptions
import io.sentry.SentryTraceHeader
import io.sentry.W3CTraceparentHeader
import java.net.URI
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.springframework.http.HttpMethod
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpResponse
import org.springframework.mock.http.client.MockClientHttpRequest
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
class SentrySpanClientHttpRequestInterceptorTest {
class Fixture {
val request = MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com/users/123"))
val options =
SentryOptions().apply {
dsn = "https://key@sentry.io/proj"
tracesSampleRate = 1.0
}
val scope = Scope(options)
val scopes = mock<IScopes>()
val requestExecution = mock<ClientHttpRequestExecution>()
val body = "data".toByteArray()
init {
whenever(scopes.options).thenReturn(options)
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }
.whenever(scopes)
.configureScope(any())
whenever(requestExecution.execute(any(), any())).thenReturn(mock<ClientHttpResponse>())
}
fun create(
config: Sentry.OptionsConfiguration<SentryOptions>
): SentrySpanClientHttpRequestInterceptor {
config.configure(options)
return SentrySpanClientHttpRequestInterceptor(scopes)
}
}
val fixture = Fixture()
@Test
fun `attaches w3c trace parent header when enabled`() {
val sut = fixture.create { options -> options.isPropagateTraceparent = true }
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
assertNotNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
}
@Test
fun `does not attach w3c trace parent header when enabled`() {
val sut = fixture.create { options -> options.isPropagateTraceparent = false }
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
assertNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
}
}