Skip to content

Commit 238852f

Browse files
committed
Add more tests
1 parent 190459d commit 238852f

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.sentry.spring7.tracing
2+
3+
import io.sentry.IScopes
4+
import io.sentry.Scope
5+
import io.sentry.ScopeCallback
6+
import io.sentry.Sentry
7+
import io.sentry.SentryOptions
8+
import io.sentry.SentryTraceHeader
9+
import io.sentry.W3CTraceparentHeader
10+
import java.net.URI
11+
import kotlin.test.Test
12+
import kotlin.test.assertNotNull
13+
import kotlin.test.assertNull
14+
import org.junit.runner.RunWith
15+
import org.mockito.kotlin.any
16+
import org.mockito.kotlin.doAnswer
17+
import org.mockito.kotlin.mock
18+
import org.mockito.kotlin.whenever
19+
import org.springframework.http.HttpMethod
20+
import org.springframework.http.client.ClientHttpRequestExecution
21+
import org.springframework.http.client.ClientHttpResponse
22+
import org.springframework.mock.http.client.MockClientHttpRequest
23+
import org.springframework.test.context.junit4.SpringRunner
24+
25+
@RunWith(SpringRunner::class)
26+
class SentrySpanClientHttpRequestInterceptorTest {
27+
28+
class Fixture {
29+
val request = MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com/users/123"))
30+
31+
val options =
32+
SentryOptions().apply {
33+
dsn = "https://key@sentry.io/proj"
34+
tracesSampleRate = 1.0
35+
}
36+
val scope = Scope(options)
37+
38+
val scopes = mock<IScopes>()
39+
val requestExecution = mock<ClientHttpRequestExecution>()
40+
val body = "data".toByteArray()
41+
42+
init {
43+
whenever(scopes.options).thenReturn(options)
44+
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }
45+
.whenever(scopes)
46+
.configureScope(any())
47+
48+
whenever(requestExecution.execute(any(), any())).thenReturn(mock<ClientHttpResponse>())
49+
}
50+
51+
fun create(
52+
config: Sentry.OptionsConfiguration<SentryOptions>
53+
): SentrySpanClientHttpRequestInterceptor {
54+
config.configure(options)
55+
return SentrySpanClientHttpRequestInterceptor(scopes)
56+
}
57+
}
58+
59+
val fixture = Fixture()
60+
61+
@Test
62+
fun `attaches w3c trace parent header when enabled`() {
63+
val sut = fixture.create { options -> options.isPropagateTraceparent = true }
64+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
65+
66+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
67+
assertNotNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
68+
}
69+
70+
@Test
71+
fun `does not attach w3c trace parent header when enabled`() {
72+
val sut = fixture.create { options -> options.isPropagateTraceparent = false }
73+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
74+
75+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
76+
assertNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.sentry.spring.jakarta.tracing
2+
3+
import io.sentry.IScopes
4+
import io.sentry.Scope
5+
import io.sentry.ScopeCallback
6+
import io.sentry.Sentry
7+
import io.sentry.SentryOptions
8+
import io.sentry.SentryTraceHeader
9+
import io.sentry.W3CTraceparentHeader
10+
import java.net.URI
11+
import kotlin.test.Test
12+
import kotlin.test.assertNotNull
13+
import kotlin.test.assertNull
14+
import org.junit.runner.RunWith
15+
import org.mockito.kotlin.any
16+
import org.mockito.kotlin.doAnswer
17+
import org.mockito.kotlin.mock
18+
import org.mockito.kotlin.whenever
19+
import org.springframework.http.HttpMethod
20+
import org.springframework.http.client.ClientHttpRequestExecution
21+
import org.springframework.http.client.ClientHttpResponse
22+
import org.springframework.mock.http.client.MockClientHttpRequest
23+
import org.springframework.test.context.junit4.SpringRunner
24+
25+
@RunWith(SpringRunner::class)
26+
class SentrySpanClientHttpRequestInterceptorTest {
27+
28+
class Fixture {
29+
val request = MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com/users/123"))
30+
31+
val options =
32+
SentryOptions().apply {
33+
dsn = "https://key@sentry.io/proj"
34+
tracesSampleRate = 1.0
35+
}
36+
val scope = Scope(options)
37+
38+
val scopes = mock<IScopes>()
39+
val requestExecution = mock<ClientHttpRequestExecution>()
40+
val body = "data".toByteArray()
41+
42+
init {
43+
whenever(scopes.options).thenReturn(options)
44+
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }
45+
.whenever(scopes)
46+
.configureScope(any())
47+
48+
whenever(requestExecution.execute(any(), any())).thenReturn(mock<ClientHttpResponse>())
49+
}
50+
51+
fun create(
52+
config: Sentry.OptionsConfiguration<SentryOptions>
53+
): SentrySpanClientHttpRequestInterceptor {
54+
config.configure(options)
55+
return SentrySpanClientHttpRequestInterceptor(scopes)
56+
}
57+
}
58+
59+
val fixture = Fixture()
60+
61+
@Test
62+
fun `attaches w3c trace parent header when enabled`() {
63+
val sut = fixture.create { options -> options.isPropagateTraceparent = true }
64+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
65+
66+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
67+
assertNotNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
68+
}
69+
70+
@Test
71+
fun `does not attach w3c trace parent header when enabled`() {
72+
val sut = fixture.create { options -> options.isPropagateTraceparent = false }
73+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
74+
75+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
76+
assertNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.sentry.spring.tracing
2+
3+
import io.sentry.IScopes
4+
import io.sentry.Scope
5+
import io.sentry.ScopeCallback
6+
import io.sentry.Sentry
7+
import io.sentry.SentryOptions
8+
import io.sentry.SentryTraceHeader
9+
import io.sentry.W3CTraceparentHeader
10+
import java.net.URI
11+
import kotlin.test.Test
12+
import kotlin.test.assertNotNull
13+
import kotlin.test.assertNull
14+
import org.junit.runner.RunWith
15+
import org.mockito.kotlin.any
16+
import org.mockito.kotlin.doAnswer
17+
import org.mockito.kotlin.mock
18+
import org.mockito.kotlin.whenever
19+
import org.springframework.http.HttpMethod
20+
import org.springframework.http.client.ClientHttpRequestExecution
21+
import org.springframework.http.client.ClientHttpResponse
22+
import org.springframework.mock.http.client.MockClientHttpRequest
23+
import org.springframework.test.context.junit4.SpringRunner
24+
25+
@RunWith(SpringRunner::class)
26+
class SentrySpanClientHttpRequestInterceptorTest {
27+
28+
class Fixture {
29+
val request = MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com/users/123"))
30+
31+
val options =
32+
SentryOptions().apply {
33+
dsn = "https://key@sentry.io/proj"
34+
tracesSampleRate = 1.0
35+
}
36+
val scope = Scope(options)
37+
38+
val scopes = mock<IScopes>()
39+
val requestExecution = mock<ClientHttpRequestExecution>()
40+
val body = "data".toByteArray()
41+
42+
init {
43+
whenever(scopes.options).thenReturn(options)
44+
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }
45+
.whenever(scopes)
46+
.configureScope(any())
47+
48+
whenever(requestExecution.execute(any(), any())).thenReturn(mock<ClientHttpResponse>())
49+
}
50+
51+
fun create(
52+
config: Sentry.OptionsConfiguration<SentryOptions>
53+
): SentrySpanClientHttpRequestInterceptor {
54+
config.configure(options)
55+
return SentrySpanClientHttpRequestInterceptor(scopes)
56+
}
57+
}
58+
59+
val fixture = Fixture()
60+
61+
@Test
62+
fun `attaches w3c trace parent header when enabled`() {
63+
val sut = fixture.create { options -> options.isPropagateTraceparent = true }
64+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
65+
66+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
67+
assertNotNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
68+
}
69+
70+
@Test
71+
fun `does not attach w3c trace parent header when enabled`() {
72+
val sut = fixture.create { options -> options.isPropagateTraceparent = false }
73+
sut.intercept(fixture.request, fixture.body, fixture.requestExecution)
74+
75+
assertNotNull(fixture.request.headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER))
76+
assertNull(fixture.request.headers.get(W3CTraceparentHeader.TRACEPARENT_HEADER))
77+
}
78+
}

0 commit comments

Comments
 (0)