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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspend support in interceptors #7908

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions okhttp-coroutines/api/okhttp-coroutines.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ public final class okhttp3/JvmCallExtensionsKt {
public static final fun executeAsync (Lokhttp3/Call;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public abstract interface class okhttp3/SuspendingInterceptor : okhttp3/Interceptor {
public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response;
public abstract fun interceptAsync (Lokhttp3/Interceptor$Chain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package okhttp3;

import kotlinx.coroutines.runBlocking

fun interface SuspendingInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = runBlocking {
interceptAsync(chain)
}

suspend fun interceptAsync(chain: Interceptor.Chain): Response
}
286 changes: 286 additions & 0 deletions okhttp-coroutines/src/jvmTest/kotlin/okhttp3/InterceptorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/*
* Copyright (c) 2022 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

@file:OptIn(ExperimentalCoroutinesApi::class)

package okhttp3

import assertk.assertThat
import assertk.assertions.isEqualTo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mockwebserver3.MockResponse
import mockwebserver3.MockWebServer
import mockwebserver3.junit5.internal.MockWebServerExtension
import okhttp3.tls.internal.TlsUtil
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.util.concurrent.Executors

@ExtendWith(MockWebServerExtension::class)
class InterceptorTest {
@RegisterExtension
val clientTestRule = OkHttpClientTestRule()

private lateinit var server: MockWebServer

private val clientBuilder: OkHttpClient.Builder = clientTestRule.newClientBuilder()

val request by lazy { Request(server.url("/")) }

// TODO parameterize
val tls: Boolean = true

@BeforeEach
fun setup(server: MockWebServer) {
this.server = server

clientBuilder.addInterceptor { chain ->
chain.proceed(chain.request()).also {
if (tls) {
check(it.protocol == Protocol.HTTP_2)
check(it.handshake != null)
} else {
check(it.protocol == Protocol.HTTP_1_1)
check(it.handshake == null)
}
}
}

if (tls) {
enableTls()
}

server.enqueue(MockResponse(body = "failed", code = 401))
server.enqueue(MockResponse(body = "token"))
server.enqueue(MockResponse(body = "abc"))
}

@Test
fun asyncCallTest() {
runTest {
val interceptor = Interceptor {
val response = it.proceed(it.request())

if (response.code == 401 && it.request().url.encodedPath != "/token") {
check(response.body.string() == "failed")
response.close()

val tokenRequest = Request(server.url("/token"))
val call = it.callFactory.newCall(tokenRequest)
val token = runBlocking {
val tokenResponse = call.executeAsync()
withContext(Dispatchers.IO) {
tokenResponse.body.string()
}
}

check(token == "token")

val secondResponse = it.proceed(
it.request().newBuilder()
.header("Authorization", token)
.build()
)

secondResponse
} else {
response
}
}


val client = clientBuilder
.dispatcher(Dispatcher(Executors.newSingleThreadExecutor()))
.addInterceptor(interceptor)
.build()

val call = client.newCall(request)

val tokenResponse = call.executeAsync()
val body = withContext(Dispatchers.IO) {
tokenResponse.body.string()
}

assertThat(body).isEqualTo("abc")
}
}

@Test
fun syncCallTest() {
val interceptor = Interceptor {
val response = it.proceed(it.request())

if (response.code == 401 && it.request().url.encodedPath != "/token") {
check(response.body.string() == "failed")
response.close()

val tokenRequest = Request(server.url("/token"))
val call = it.callFactory.newCall(tokenRequest)
val token = if (false)
runBlocking {
val tokenResponse = call.executeAsync()
withContext(Dispatchers.IO) {
tokenResponse.body.string()
}
}
else
call.execute().body.string()

check(token == "token")

val secondResponse = it.proceed(
it.request().newBuilder()
.header("Authorization", token)
.build()
)

secondResponse
} else {
response
}
}

val client = clientBuilder
.dispatcher(Dispatcher(Executors.newSingleThreadExecutor()))
.addInterceptor(interceptor)
.build()

val call = client.newCall(request)

val body = call.execute().body.string()

assertThat(body).isEqualTo("abc")
}

@Test
fun asyncInterceptorCallTest() {
runTest {
val interceptor = SuspendingInterceptor {
val response = it.proceed(it.request())

if (response.code == 401 && it.request().url.encodedPath != "/token") {
check(response.body.string() == "failed")
response.close()

val tokenRequest = Request(server.url("/token"))
val call = it.callFactory.newCall(tokenRequest)

val tokenResponse = call.executeAsync()
val token = withContext(Dispatchers.IO) {
tokenResponse.body.string()
}

check(token == "token")

val secondResponse = it.proceed(
it.request().newBuilder()
.header("Authorization", token)
.build()
)

secondResponse
} else {
response
}
}


val client = clientBuilder
.dispatcher(Dispatcher(Executors.newSingleThreadExecutor()))
.addInterceptor(interceptor)
.build()

val call = client.newCall(request)

val tokenResponse = call.executeAsync()
val body = withContext(Dispatchers.IO) {
tokenResponse.body.string()
}

assertThat(body).isEqualTo("abc")
}
}

@Test
fun asyncInterceptorCallByThreadTest() {
lateinit var client: OkHttpClient

runTest {
val interceptor = SuspendingInterceptor {
val response = it.proceed(it.request())

if (response.code == 401 && it.request().url.encodedPath != "/token") {
check(response.body.string() == "failed")
response.close()

val tokenRequest = Request(server.url("/token"))
val call = client.newCall(tokenRequest)

val tokenResponse = call.executeAsync()
val token = withContext(Dispatchers.IO) {
tokenResponse.body.string()
}

check(token == "token")

val secondResponse = it.proceed(
it.request().newBuilder()
.header("Authorization", token)
.build()
)

secondResponse
} else {
response
}
}

client = clientBuilder
.dispatcher(Dispatcher(Executors.newSingleThreadExecutor()))
.addInterceptor(interceptor)
.build()

val call = client.newCall(request)

val tokenResponse = call.executeAsync()
val body = withContext(Dispatchers.IO) {
tokenResponse.body.string()
}

assertThat(body).isEqualTo("abc")
}
}

private fun enableTls() {
val certs = TlsUtil.localhost()

clientBuilder
.sslSocketFactory(
certs.sslSocketFactory(), certs.trustManager
)

server.useHttps(certs.sslSocketFactory())
}
}
1 change: 1 addition & 0 deletions okhttp/api/okhttp.api
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ public abstract interface class okhttp3/Interceptor$Chain {
public abstract fun call ()Lokhttp3/Call;
public abstract fun connectTimeoutMillis ()I
public abstract fun connection ()Lokhttp3/Connection;
public abstract fun getCallFactory ()Lokhttp3/Call$Factory;
public abstract fun proceed (Lokhttp3/Request;)Lokhttp3/Response;
public abstract fun readTimeoutMillis ()I
public abstract fun request ()Lokhttp3/Request;
Expand Down
2 changes: 2 additions & 0 deletions okhttp/src/jvmMain/kotlin/okhttp3/Interceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ fun interface Interceptor {

fun call(): Call

val callFactory: Call.Factory

fun connectTimeoutMillis(): Int

fun withConnectTimeout(timeout: Int, unit: TimeUnit): Chain
Expand Down