11package io.sentry.ktorClient
22
33import io.ktor.client.HttpClient
4+ import io.ktor.client.HttpClientConfig
5+ import io.ktor.client.engine.HttpClientEngine
6+ import io.ktor.client.engine.java.Java
7+ import io.ktor.client.engine.okhttp.OkHttp
8+ import io.ktor.client.engine.okhttp.OkHttpConfig
9+ import io.ktor.client.engine.okhttp.OkHttpEngine
10+ import io.ktor.client.plugins.plugin
411import io.ktor.client.request.get
512import io.ktor.client.request.post
613import io.ktor.client.request.setBody
@@ -24,8 +31,10 @@ import io.sentry.SpanStatus
2431import io.sentry.TransactionContext
2532import io.sentry.exception.SentryHttpClientException
2633import io.sentry.mockServerRequestTimeoutMillis
34+ import io.sentry.okhttp.SentryOkHttpInterceptor
2735import java.util.concurrent.TimeUnit
2836import kotlin.test.Test
37+ import okhttp3.OkHttpClient
2938import kotlin.test.assertEquals
3039import kotlin.test.assertNotNull
3140import kotlin.test.assertNull
@@ -68,6 +77,7 @@ class SentryKtorClientPluginTest {
6877 ),
6978 sendDefaultPii : Boolean = false,
7079 optionsConfiguration : Sentry .OptionsConfiguration <SentryOptions >? = null,
80+ httpClientEngine : HttpClientEngine = Java .create(),
7181 ): HttpClient {
7282 options =
7383 SentryOptions ().also {
@@ -102,7 +112,7 @@ class SentryKtorClientPluginTest {
102112 .setResponseCode(httpStatusCode)
103113 )
104114
105- return HttpClient {
115+ return HttpClient (httpClientEngine) {
106116 install(SentryKtorClientPlugin ) {
107117 this .scopes = this @Fixture.scopes
108118 this .captureFailedRequests = captureFailedRequests
@@ -364,9 +374,12 @@ class SentryKtorClientPluginTest {
364374 @Test
365375 fun `does not add sentry-trace header when span origin is ignored` (): Unit = runBlocking {
366376 val sut =
367- fixture.getSut(isSpanActive = false ) { options ->
368- options.setIgnoredSpanOrigins(listOf (" auto.http.ktor-client" ))
369- }
377+ fixture.getSut(
378+ isSpanActive = false ,
379+ optionsConfiguration = { options ->
380+ options.setIgnoredSpanOrigins(listOf (" auto.http.ktor-client" ))
381+ }
382+ )
370383 sut.get(fixture.server.url(" /hello" ).toString())
371384
372385 val recordedRequest =
@@ -419,4 +432,23 @@ class SentryKtorClientPluginTest {
419432 assertTrue(baggageHeaderValues[0 ].contains(" sentry-transaction=name" ))
420433 assertTrue(baggageHeaderValues[0 ].contains(" sentry-trace_id" ))
421434 }
435+
436+ @Test
437+ fun `is disabled when using OkHttp client with Sentry interceptor added to builder` () {
438+ val okHttpClient = OkHttpClient .Builder ()
439+ .addInterceptor(SentryOkHttpInterceptor ())
440+ .build()
441+ val engine = OkHttpEngine (OkHttpConfig ().apply {
442+ preconfigured = okHttpClient
443+ })
444+
445+ val client = fixture.getSut(httpClientEngine = engine)
446+ val plugin = client.plugin(SentryKtorClientPlugin )
447+ }
448+
449+ @Test
450+ fun `is disabled when using preconfigured OkHttp client with Sentry interceptor` () {
451+ val engine = OkHttpEngine (OkHttpConfig ())
452+ val client = fixture.getSut(httpClientEngine = engine)
453+ }
422454}
0 commit comments