Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor OkHttp and Apollo to Kotlin functional interfaces. #1797

Merged
merged 4 commits into from Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Feat: Refactor OkHttp and Apollo to Kotlin functional interfaces (#1797)

## 5.3.0

* Feat: Add datasource tracing with P6Spy (#1784)
Expand Down
Expand Up @@ -91,7 +91,7 @@ class SentryOkHttpInterceptor(
/**
* The BeforeSpan callback
*/
interface BeforeSpanCallback {
fun interface BeforeSpanCallback {
/**
* Mutates or drops span before being added
*
Expand Down
Expand Up @@ -8,7 +8,6 @@ import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.Breadcrumb
import io.sentry.IHub
import io.sentry.ISpan
import io.sentry.SentryOptions
import io.sentry.SentryTraceHeader
import io.sentry.SentryTracer
Expand All @@ -20,7 +19,6 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
Expand Down Expand Up @@ -206,11 +204,9 @@ class SentryOkHttpInterceptorTest {
@Test
fun `customizer modifies span`() {
val sut = fixture.getSut(
beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: Request, response: Response?): ISpan {
span.description = "overwritten description"
return span
}
beforeSpan = { span, _, _ ->
span.description = "overwritten description"
span
}
)
val request = getRequest()
Expand All @@ -224,15 +220,13 @@ class SentryOkHttpInterceptorTest {
@Test
fun `customizer receives request and response`() {
val sut = fixture.getSut(
beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: Request, response: Response?): ISpan {
assertEquals(request.url, request.url)
assertEquals(request.method, request.method)
assertNotNull(response) {
assertEquals(201, it.code)
}
return span
beforeSpan = { span, request, response ->
assertEquals(request.url, request.url)
assertEquals(request.method, request.method)
assertNotNull(response) {
assertEquals(201, it.code)
}
span
}
)
val request = getRequest()
Expand All @@ -242,11 +236,7 @@ class SentryOkHttpInterceptorTest {
@Test
fun `customizer can drop the span`() {
val sut = fixture.getSut(
beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: Request, response: Response?): ISpan? {
return null
}
}
beforeSpan = { _, _, _ -> null }
)
sut.newCall(getRequest()).execute()
val httpClientSpan = fixture.sentryTracer.children.first()
Expand Down
Expand Up @@ -126,7 +126,7 @@ class SentryApolloInterceptor(
/**
* The BeforeSpan callback
*/
interface BeforeSpanCallback {
fun interface BeforeSpanCallback {
/**
* Mutates span before being added.
*
Expand Down
Expand Up @@ -3,16 +3,13 @@ package io.sentry.apollo
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.coroutines.await
import com.apollographql.apollo.exception.ApolloException
import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest
import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse
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.ISpan
import io.sentry.ITransaction
import io.sentry.SentryOptions
import io.sentry.SentryTraceHeader
Expand Down Expand Up @@ -135,14 +132,10 @@ class SentryApolloInterceptorTest {
@Test
fun `customizer modifies span`() {
executeQuery(
fixture.getSut(
beforeSpan = object : SentryApolloInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan {
span.description = "overwritten description"
return span
}
}
)
fixture.getSut { span, _, _ ->
span.description = "overwritten description"
span
}
)

verify(fixture.hub).captureTransaction(
Expand All @@ -158,13 +151,7 @@ class SentryApolloInterceptorTest {
@Test
fun `when customizer throws, exception is handled`() {
executeQuery(
fixture.getSut(
beforeSpan = object : SentryApolloInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan {
throw RuntimeException()
}
}
)
fixture.getSut { _, _, _ -> throw RuntimeException() }
)

verify(fixture.hub).captureTransaction(
Expand Down