Skip to content

Enable setting OkHttpClient for FirebaseFunctions #5430

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.functions.FirebaseFunctions.Companion.getInstance
import junit.framework.TestCase.assertEquals
import okhttp3.OkHttpClient
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -91,6 +92,17 @@ class FirebaseFunctionsTest {
assertEquals(functions1.getURL("foo").toString(), functions2.getURL("foo").toString())
}

@Test
fun testSetOkHttpClient() {
val client = OkHttpClient.Builder().build()
val app = getApp("testSetOkHttpClient")
val functions = getInstance(app)

functions.setOkHttpClient(client)

assertEquals(client, functions.getOkHttpClient())
}

private fun getApp(name: String): FirebaseApp {
return FirebaseApp.initializeApp(
InstrumentationRegistry.getInstrumentation().targetContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal constructor(
@UiThread uiExecutor: Executor
) {
// The network client to use for HTTPS requests.
private val client: OkHttpClient = OkHttpClient()
private var client: OkHttpClient = OkHttpClient()

// A serializer to encode/decode parameters and return values.
private val serializer: Serializer = Serializer()
Expand Down Expand Up @@ -311,6 +311,12 @@ internal constructor(
return tcs.task
}

public fun setOkHttpClient(client: OkHttpClient) {
this.client = client
}

public fun getOkHttpClient(): OkHttpClient = this.client

public companion object {
/** A task that will be resolved once ProviderInstaller has installed what it needs to. */
private val providerInstalled = TaskCompletionSource<Void>()
Expand Down