diff --git a/firebase-functions/api.txt b/firebase-functions/api.txt index 689d792a49a..aaa96f9fbec 100644 --- a/firebase-functions/api.txt +++ b/firebase-functions/api.txt @@ -2,8 +2,6 @@ package com.google.firebase.functions { public final class FirebaseFunctions { - method @NonNull public com.google.android.gms.tasks.Task<com.google.firebase.functions.HttpsCallableResult> call(@NonNull String name, @Nullable Object data, @NonNull com.google.firebase.functions.HttpsCallOptions options); - method @NonNull public com.google.android.gms.tasks.Task<com.google.firebase.functions.HttpsCallableResult> call(@NonNull java.net.URL url, @Nullable Object data, @NonNull com.google.firebase.functions.HttpsCallOptions options); method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallable(@NonNull String name); method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallable(@NonNull String name, @NonNull com.google.firebase.functions.HttpsCallableOptions options); method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallableFromUrl(@NonNull java.net.URL url); @@ -12,7 +10,6 @@ package com.google.firebase.functions { method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull com.google.firebase.FirebaseApp app); method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull String regionOrCustomDomain); method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(); - method @NonNull @VisibleForTesting public java.net.URL getURL(@NonNull String function); method public void useEmulator(@NonNull String host, int port); method @Deprecated public void useFunctionsEmulator(@NonNull String origin); field @NonNull public static final com.google.firebase.functions.FirebaseFunctions.Companion Companion; diff --git a/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.java b/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.java deleted file mode 100644 index 45a3402ac5e..00000000000 --- a/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.java +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2018 Google LLC -// -// 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. - -package com.google.firebase.functions; - -import static org.junit.Assert.assertEquals; - -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.runner.AndroidJUnit4; -import com.google.firebase.FirebaseApp; -import com.google.firebase.FirebaseOptions; -import java.net.URL; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class FirebaseFunctionsTest { - - @Test - public void testGetUrl() { - FirebaseApp app = getApp("testGetUrl"); - - FirebaseFunctions functions = FirebaseFunctions.getInstance(app, "my-region"); - URL url = functions.getURL("my-endpoint"); - assertEquals("https://my-region-my-project.cloudfunctions.net/my-endpoint", url.toString()); - - functions = FirebaseFunctions.getInstance(app); - url = functions.getURL("my-endpoint"); - assertEquals("https://us-central1-my-project.cloudfunctions.net/my-endpoint", url.toString()); - - functions = FirebaseFunctions.getInstance(app, "https://mydomain.com"); - url = functions.getURL("my-endpoint"); - assertEquals("https://mydomain.com/my-endpoint", url.toString()); - - functions = FirebaseFunctions.getInstance(app, "https://mydomain.com/foo"); - url = functions.getURL("my-endpoint"); - assertEquals("https://mydomain.com/foo/my-endpoint", url.toString()); - } - - @Test - public void testGetUrl_withEmulator() { - FirebaseApp app = getApp("testGetUrl_withEmulator"); - - FirebaseFunctions functions = FirebaseFunctions.getInstance(app); - functions.useEmulator("10.0.2.2", 5001); - - FirebaseFunctions functionsWithoutRegion = FirebaseFunctions.getInstance(app); - URL withoutRegion = functionsWithoutRegion.getURL("my-endpoint"); - assertEquals( - "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", withoutRegion.toString()); - - FirebaseFunctions functionsWithRegion = FirebaseFunctions.getInstance(app, "my-region"); - functionsWithRegion.useEmulator("10.0.2.2", 5001); - - URL withRegion = functionsWithRegion.getURL("my-endpoint"); - assertEquals("http://10.0.2.2:5001/my-project/my-region/my-endpoint", withRegion.toString()); - - FirebaseFunctions functionsWithCustomDomain = - FirebaseFunctions.getInstance(app, "https://mydomain.com"); - functionsWithCustomDomain.useEmulator("10.0.2.2", 5001); - - URL withCustomDOmain = functionsWithCustomDomain.getURL("my-endpoint"); - assertEquals( - "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", withCustomDOmain.toString()); - } - - @Test - public void testGetUrl_withEmulator_matchesOldImpl() { - FirebaseApp app = getApp("testGetUrl_withEmulator_matchesOldImpl"); - - FirebaseFunctions functions = FirebaseFunctions.getInstance(app); - functions.useEmulator("10.0.2.2", 5001); - URL newImplUrl = functions.getURL("my-endpoint"); - - functions.useFunctionsEmulator("http://10.0.2.2:5001"); - URL oldImplUrl = functions.getURL("my-endpoint"); - - assertEquals(newImplUrl.toString(), oldImplUrl.toString()); - } - - @Test - public void testEmulatorSettings() { - FirebaseApp app = getApp("testEmulatorSettings"); - - FirebaseFunctions functions1 = FirebaseFunctions.getInstance(app); - functions1.useEmulator("10.0.2.2", 5001); - - FirebaseFunctions functions2 = FirebaseFunctions.getInstance(app); - - assertEquals(functions1.getURL("foo").toString(), functions2.getURL("foo").toString()); - } - - private FirebaseApp getApp(String name) { - return FirebaseApp.initializeApp( - InstrumentationRegistry.getInstrumentation().getTargetContext(), - new FirebaseOptions.Builder() - .setProjectId("my-project") - .setApplicationId("appid") - .setApiKey("apikey") - .build(), - name); - } -} 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 new file mode 100644 index 00000000000..41e35d11f83 --- /dev/null +++ b/firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.kt @@ -0,0 +1,105 @@ +// Copyright 2018 Google LLC +// +// 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. +package com.google.firebase.functions + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.runner.AndroidJUnit4 +import com.google.firebase.FirebaseApp +import com.google.firebase.FirebaseOptions +import com.google.firebase.functions.FirebaseFunctions.Companion.getInstance +import junit.framework.TestCase.assertEquals +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class FirebaseFunctionsTest { + @Test + fun testGetUrl() { + val app = getApp("testGetUrl") + var functions = getInstance(app, "my-region") + var url = functions.getURL("my-endpoint") + assertEquals("https://my-region-my-project.cloudfunctions.net/my-endpoint", url.toString()) + + functions = getInstance(app) + url = functions.getURL("my-endpoint") + assertEquals("https://us-central1-my-project.cloudfunctions.net/my-endpoint", url.toString()) + + functions = getInstance(app, "https://mydomain.com") + url = functions.getURL("my-endpoint") + Assert.assertEquals("https://mydomain.com/my-endpoint", url.toString()) + + functions = getInstance(app, "https://mydomain.com/foo") + url = functions.getURL("my-endpoint") + assertEquals("https://mydomain.com/foo/my-endpoint", url.toString()) + } + + @Test + fun testGetUrl_withEmulator() { + val app = getApp("testGetUrl_withEmulator") + val functions = getInstance(app) + functions.useEmulator("10.0.2.2", 5001) + val functionsWithoutRegion = getInstance(app) + val withoutRegion = functionsWithoutRegion.getURL("my-endpoint") + assertEquals( + "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", + withoutRegion.toString() + ) + + val functionsWithRegion = getInstance(app, "my-region") + functionsWithRegion.useEmulator("10.0.2.2", 5001) + val withRegion = functionsWithRegion.getURL("my-endpoint") + assertEquals("http://10.0.2.2:5001/my-project/my-region/my-endpoint", withRegion.toString()) + + val functionsWithCustomDomain = getInstance(app, "https://mydomain.com") + functionsWithCustomDomain.useEmulator("10.0.2.2", 5001) + val withCustomDOmain = functionsWithCustomDomain.getURL("my-endpoint") + assertEquals( + "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", + withCustomDOmain.toString() + ) + } + + @Test + fun testGetUrl_withEmulator_matchesOldImpl() { + val app = getApp("testGetUrl_withEmulator_matchesOldImpl") + val functions = getInstance(app) + functions.useEmulator("10.0.2.2", 5001) + val newImplUrl = functions.getURL("my-endpoint") + functions.useFunctionsEmulator("http://10.0.2.2:5001") + val oldImplUrl = functions.getURL("my-endpoint") + assertEquals(newImplUrl.toString(), oldImplUrl.toString()) + } + + @Test + fun testEmulatorSettings() { + val app = getApp("testEmulatorSettings") + val functions1 = getInstance(app) + functions1.useEmulator("10.0.2.2", 5001) + val functions2 = getInstance(app) + assertEquals(functions1.getURL("foo").toString(), functions2.getURL("foo").toString()) + } + + private fun getApp(name: String): FirebaseApp { + return FirebaseApp.initializeApp( + InstrumentationRegistry.getInstrumentation().targetContext, + FirebaseOptions.Builder() + .setProjectId("my-project") + .setApplicationId("appid") + .setApiKey("apikey") + .build(), + name + ) + } +} 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 043d35d6e00..cd87de4baaa 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 @@ -131,7 +131,7 @@ internal constructor( * @return The URL. */ @VisibleForTesting - fun getURL(function: String): URL { + internal fun getURL(function: String): URL { val emulatorSettings = emulatorSettings if (emulatorSettings != null) { urlFormat = @@ -173,7 +173,11 @@ internal constructor( * @param data Parameters to pass to the function. Can be anything encodable as JSON. * @return A Task that will be completed when the request is complete. */ - fun call(name: String, data: Any?, options: HttpsCallOptions): Task<HttpsCallableResult> { + internal fun call( + name: String, + data: Any?, + options: HttpsCallOptions + ): Task<HttpsCallableResult> { return providerInstalled.task .continueWithTask(executor) { task: Task<Void>? -> contextProvider.getContext(options.limitedUseAppCheckTokens) @@ -195,7 +199,7 @@ internal constructor( * @param data Parameters to pass to the function. Can be anything encodable as JSON. * @return A Task that will be completed when the request is complete. */ - fun call(url: URL, data: Any?, options: HttpsCallOptions): Task<HttpsCallableResult> { + internal fun call(url: URL, data: Any?, options: HttpsCallOptions): Task<HttpsCallableResult> { return providerInstalled.task .continueWithTask(executor) { task: Task<Void>? -> contextProvider.getContext(options.limitedUseAppCheckTokens)