Skip to content

Fix visibility on Firebase Functions #6269

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

Merged
merged 3 commits into from
Sep 19, 2024
Merged
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
3 changes: 0 additions & 3 deletions firebase-functions/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading