Skip to content

Commit

Permalink
Refactor OkHttp and Apollo to Kotlin functional interfaces. (#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed Nov 12, 2021
1 parent eebe56a commit 5ba7d38
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
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.4.0

* Feat: Add `graphql-java` instrumentation (#1777)
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

0 comments on commit 5ba7d38

Please sign in to comment.