|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package com.google.firebase.functions |
| 15 | + |
| 16 | +import androidx.test.platform.app.InstrumentationRegistry |
| 17 | +import androidx.test.runner.AndroidJUnit4 |
| 18 | +import com.google.firebase.FirebaseApp |
| 19 | +import com.google.firebase.FirebaseOptions |
| 20 | +import com.google.firebase.functions.FirebaseFunctions.Companion.getInstance |
| 21 | +import junit.framework.TestCase.assertEquals |
| 22 | +import org.junit.Assert |
| 23 | +import org.junit.Test |
| 24 | +import org.junit.runner.RunWith |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +class FirebaseFunctionsTest { |
| 28 | + @Test |
| 29 | + fun testGetUrl() { |
| 30 | + val app = getApp("testGetUrl") |
| 31 | + var functions = getInstance(app, "my-region") |
| 32 | + var url = functions.getURL("my-endpoint") |
| 33 | + assertEquals("https://my-region-my-project.cloudfunctions.net/my-endpoint", url.toString()) |
| 34 | + |
| 35 | + functions = getInstance(app) |
| 36 | + url = functions.getURL("my-endpoint") |
| 37 | + assertEquals("https://us-central1-my-project.cloudfunctions.net/my-endpoint", url.toString()) |
| 38 | + |
| 39 | + functions = getInstance(app, "https://mydomain.com") |
| 40 | + url = functions.getURL("my-endpoint") |
| 41 | + Assert.assertEquals("https://mydomain.com/my-endpoint", url.toString()) |
| 42 | + |
| 43 | + functions = getInstance(app, "https://mydomain.com/foo") |
| 44 | + url = functions.getURL("my-endpoint") |
| 45 | + assertEquals("https://mydomain.com/foo/my-endpoint", url.toString()) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testGetUrl_withEmulator() { |
| 50 | + val app = getApp("testGetUrl_withEmulator") |
| 51 | + val functions = getInstance(app) |
| 52 | + functions.useEmulator("10.0.2.2", 5001) |
| 53 | + val functionsWithoutRegion = getInstance(app) |
| 54 | + val withoutRegion = functionsWithoutRegion.getURL("my-endpoint") |
| 55 | + assertEquals( |
| 56 | + "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", |
| 57 | + withoutRegion.toString() |
| 58 | + ) |
| 59 | + |
| 60 | + val functionsWithRegion = getInstance(app, "my-region") |
| 61 | + functionsWithRegion.useEmulator("10.0.2.2", 5001) |
| 62 | + val withRegion = functionsWithRegion.getURL("my-endpoint") |
| 63 | + assertEquals("http://10.0.2.2:5001/my-project/my-region/my-endpoint", withRegion.toString()) |
| 64 | + |
| 65 | + val functionsWithCustomDomain = getInstance(app, "https://mydomain.com") |
| 66 | + functionsWithCustomDomain.useEmulator("10.0.2.2", 5001) |
| 67 | + val withCustomDOmain = functionsWithCustomDomain.getURL("my-endpoint") |
| 68 | + assertEquals( |
| 69 | + "http://10.0.2.2:5001/my-project/us-central1/my-endpoint", |
| 70 | + withCustomDOmain.toString() |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun testGetUrl_withEmulator_matchesOldImpl() { |
| 76 | + val app = getApp("testGetUrl_withEmulator_matchesOldImpl") |
| 77 | + val functions = getInstance(app) |
| 78 | + functions.useEmulator("10.0.2.2", 5001) |
| 79 | + val newImplUrl = functions.getURL("my-endpoint") |
| 80 | + functions.useFunctionsEmulator("http://10.0.2.2:5001") |
| 81 | + val oldImplUrl = functions.getURL("my-endpoint") |
| 82 | + assertEquals(newImplUrl.toString(), oldImplUrl.toString()) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun testEmulatorSettings() { |
| 87 | + val app = getApp("testEmulatorSettings") |
| 88 | + val functions1 = getInstance(app) |
| 89 | + functions1.useEmulator("10.0.2.2", 5001) |
| 90 | + val functions2 = getInstance(app) |
| 91 | + assertEquals(functions1.getURL("foo").toString(), functions2.getURL("foo").toString()) |
| 92 | + } |
| 93 | + |
| 94 | + private fun getApp(name: String): FirebaseApp { |
| 95 | + return FirebaseApp.initializeApp( |
| 96 | + InstrumentationRegistry.getInstrumentation().targetContext, |
| 97 | + FirebaseOptions.Builder() |
| 98 | + .setProjectId("my-project") |
| 99 | + .setApplicationId("appid") |
| 100 | + .setApiKey("apikey") |
| 101 | + .build(), |
| 102 | + name |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
0 commit comments