Skip to content

Commit bc0e438

Browse files
committed
add useEmulator test
1 parent 02f76a2 commit bc0e438

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

firebase-functions/firebase-functions.gradle

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ android {
4848
testOptions.unitTests.includeAndroidResources = true
4949
}
5050

51-
configurations {
52-
all {
53-
exclude module: 'commons-logging'
54-
}
55-
}
56-
5751
dependencies {
5852
implementation project(':firebase-common')
5953
implementation project(':firebase-components')
@@ -69,7 +63,6 @@ dependencies {
6963
implementation 'com.google.firebase:firebase-iid-interop:17.0.0'
7064

7165
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
72-
implementation 'commons-validator:commons-validator:1.7'
7366

7467
annotationProcessor 'com.google.auto.value:auto-value:1.6.2'
7568

firebase-functions/src/androidTest/java/com/google/firebase/functions/FirebaseFunctionsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public void testGetUrl_withEmulator() {
6565

6666
URL withRegion = functionsWithRegion.getURL("my-endpoint");
6767
assertEquals("http://10.0.2.2:5001/my-project/my-region/my-endpoint", withRegion.toString());
68+
69+
FirebaseFunctions functionsWithCustomDomain =
70+
FirebaseFunctions.getInstance(app, "https://mydomain.com");
71+
functionsWithCustomDomain.useEmulator("10.0.2.2", 5001);
72+
73+
URL withCustomDOmain = functionsWithCustomDomain.getURL("my-endpoint");
74+
assertEquals(
75+
"http://10.0.2.2:5001/my-project/us-central1/my-endpoint", withCustomDOmain.toString());
6876
}
6977

7078
@Test

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import okhttp3.Request;
4343
import okhttp3.RequestBody;
4444
import okhttp3.Response;
45-
import org.apache.commons.validator.routines.UrlValidator;
4645
import org.json.JSONException;
4746
import org.json.JSONObject;
4847

@@ -74,17 +73,14 @@ public class FirebaseFunctions {
7473
private final String projectId;
7574

7675
// The region to use for all function references.
77-
@Nullable private final String region;
76+
private final String region;
7877

7978
// A custom domain for the http trigger, such as "https://mydomain.com"
8079
@Nullable private final String customDomain;
8180

8281
// The format to use for constructing urls from region, projectId, and name.
8382
private String urlFormat = "https://%1$s-%2$s.cloudfunctions.net/%3$s";
8483

85-
// Allowed custom domain protocols.
86-
private String[] customDomainSchemes = {"http", "https"};
87-
8884
// Emulator settings
8985
@Nullable private EmulatedServiceSettings emulatorSettings;
9086

@@ -100,13 +96,20 @@ public class FirebaseFunctions {
10096
this.contextProvider = Preconditions.checkNotNull(contextProvider);
10197
this.projectId = Preconditions.checkNotNull(projectId);
10298

103-
UrlValidator validator = new UrlValidator(customDomainSchemes);
104-
if (validator.isValid(regionOrCustomDomain)) {
105-
this.region = null;
106-
this.customDomain = regionOrCustomDomain;
107-
} else {
99+
boolean isRegion;
100+
try {
101+
new URL(regionOrCustomDomain);
102+
isRegion = false;
103+
} catch (MalformedURLException malformedURLException) {
104+
isRegion = true;
105+
}
106+
107+
if (isRegion) {
108108
this.region = regionOrCustomDomain;
109109
this.customDomain = null;
110+
} else {
111+
this.region = "us-central1";
112+
this.customDomain = regionOrCustomDomain;
110113
}
111114

112115
maybeInstallProviders(context);
@@ -209,21 +212,22 @@ public HttpsCallableReference getHttpsCallable(@NonNull String name) {
209212
*/
210213
@VisibleForTesting
211214
URL getURL(String function) {
212-
String str;
213-
if (customDomain != null) {
215+
EmulatedServiceSettings emulatorSettings = this.emulatorSettings;
216+
if (emulatorSettings != null) {
217+
urlFormat =
218+
"http://"
219+
+ emulatorSettings.getHost()
220+
+ ":"
221+
+ emulatorSettings.getPort()
222+
+ "/%2$s/%1$s/%3$s";
223+
}
224+
225+
String str = String.format(urlFormat, region, projectId, function);
226+
227+
if (customDomain != null && emulatorSettings == null) {
214228
str = customDomain + "/" + function;
215-
} else {
216-
EmulatedServiceSettings emulatorSettings = this.emulatorSettings;
217-
if (emulatorSettings != null) {
218-
urlFormat =
219-
"http://"
220-
+ emulatorSettings.getHost()
221-
+ ":"
222-
+ emulatorSettings.getPort()
223-
+ "/%2$s/%1$s/%3$s";
224-
}
225-
str = String.format(urlFormat, region, projectId, function);
226229
}
230+
227231
try {
228232
return new URL(str);
229233
} catch (MalformedURLException mfe) {

0 commit comments

Comments
 (0)