From 7ff13c5f26d11545e5ae2e096c1e5c844e6821c9 Mon Sep 17 00:00:00 2001 From: Sven Jacobs Date: Sun, 8 Dec 2024 15:06:11 +0100 Subject: [PATCH] Enable setting OkHttpClient for FirebaseFunctions --- .../firebase/functions/FirebaseFunctionsTest.kt | 12 ++++++++++++ .../google/firebase/functions/FirebaseFunctions.kt | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.kt b/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.kt index 41e35d11f83..edb6d839835 100644 --- a/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.kt +++ b/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.kt @@ -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 @@ -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, diff --git a/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt b/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt index 3c0e7d6553e..5949242d40e 100644 --- a/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt +++ b/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt @@ -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() @@ -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()