Skip to content

Commit 35ca7c9

Browse files
authored
Add a method for telling Functions to talk to a local emulator. (#61)
1 parent ff53efc commit 35ca7c9

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ public static void setUp() {
3636
app = FirebaseApp.getInstance();
3737
}
3838

39+
private void useTestURL(FirebaseFunctions functions) {
40+
functions.useFunctionsEmulator("http://10.0.2.2:5005");
41+
}
42+
3943
@Test
4044
public void testData() throws InterruptedException, ExecutionException {
4145
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
42-
functions.setUseTestURL(true);
46+
useTestURL(functions);
4347

4448
Map<String, Object> params = new HashMap<>();
4549
params.put("bool", true);
@@ -63,7 +67,7 @@ public void testData() throws InterruptedException, ExecutionException {
6367
@Test
6468
public void testScalars() throws InterruptedException, ExecutionException {
6569
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
66-
functions.setUseTestURL(true);
70+
useTestURL(functions);
6771

6872
HttpsCallableReference function = functions.getHttpsCallable("scalarTest");
6973
Task<HttpsCallableResult> result = function.call(17);
@@ -87,7 +91,7 @@ public Task<HttpsCallableContext> getContext() {
8791
return Tasks.forResult(context);
8892
}
8993
});
90-
functions.setUseTestURL(true);
94+
useTestURL(functions);
9195

9296
HttpsCallableReference function = functions.getHttpsCallable("tokenTest");
9397
Task<HttpsCallableResult> result = function.call(new HashMap<>());
@@ -111,7 +115,7 @@ public Task<HttpsCallableContext> getContext() {
111115
return Tasks.forResult(context);
112116
}
113117
});
114-
functions.setUseTestURL(true);
118+
useTestURL(functions);
115119

116120
HttpsCallableReference function = functions.getHttpsCallable("instanceIdTest");
117121
Task<HttpsCallableResult> result = function.call(new HashMap<>());
@@ -123,7 +127,7 @@ public Task<HttpsCallableContext> getContext() {
123127
@Test
124128
public void testNull() throws InterruptedException, ExecutionException {
125129
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
126-
functions.setUseTestURL(true);
130+
useTestURL(functions);
127131

128132
HttpsCallableReference function = functions.getHttpsCallable("nullTest");
129133
Task<HttpsCallableResult> result = function.call(null);
@@ -139,7 +143,7 @@ public void testNull() throws InterruptedException, ExecutionException {
139143
@Test
140144
public void testMissingResult() throws InterruptedException, ExecutionException {
141145
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
142-
functions.setUseTestURL(true);
146+
useTestURL(functions);
143147

144148
HttpsCallableReference function = functions.getHttpsCallable("missingResultTest");
145149
Task<HttpsCallableResult> result = function.call(null);
@@ -155,7 +159,7 @@ public void testMissingResult() throws InterruptedException, ExecutionException
155159
@Test
156160
public void testUnhandledError() throws InterruptedException, ExecutionException {
157161
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
158-
functions.setUseTestURL(true);
162+
useTestURL(functions);
159163

160164
HttpsCallableReference function = functions.getHttpsCallable("unhandledErrorTest");
161165
Task<HttpsCallableResult> result = function.call();
@@ -171,7 +175,7 @@ public void testUnhandledError() throws InterruptedException, ExecutionException
171175
@Test
172176
public void testUnknownError() throws InterruptedException, ExecutionException {
173177
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
174-
functions.setUseTestURL(true);
178+
useTestURL(functions);
175179

176180
HttpsCallableReference function = functions.getHttpsCallable("unknownErrorTest");
177181
Task<HttpsCallableResult> result = function.call();
@@ -187,7 +191,7 @@ public void testUnknownError() throws InterruptedException, ExecutionException {
187191
@Test
188192
public void testExplicitError() throws InterruptedException, ExecutionException {
189193
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
190-
functions.setUseTestURL(true);
194+
useTestURL(functions);
191195

192196
HttpsCallableReference function = functions.getHttpsCallable("explicitErrorTest");
193197
Task<HttpsCallableResult> result = function.call();
@@ -208,7 +212,7 @@ public void testExplicitError() throws InterruptedException, ExecutionException
208212
@Test
209213
public void testHttpError() throws InterruptedException, ExecutionException {
210214
FirebaseFunctions functions = FirebaseFunctions.getInstance(app);
211-
functions.setUseTestURL(true);
215+
useTestURL(functions);
212216

213217
HttpsCallableReference function = functions.getHttpsCallable("httpErrorTest");
214218
Task<HttpsCallableResult> result = function.call();

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public class FirebaseFunctions {
6565
// The region to use for all function references.
6666
private final String region;
6767

68-
// If true, use an Android emulator localhost URL instead of the normal one.
69-
private boolean useTestURL = false;
68+
// The format to use for constructing urls from region, projectId, and name.
69+
private String urlFormat = "https://%1$s-%2$s.cloudfunctions.net/%3$s";
7070

7171
@VisibleForTesting
7272
FirebaseFunctions(
@@ -172,36 +172,22 @@ public HttpsCallableReference getHttpsCallable(String name) {
172172
*/
173173
@VisibleForTesting
174174
URL getURL(String function) {
175-
StringBuilder builder = new StringBuilder();
176-
if (useTestURL) {
177-
builder
178-
.append("http://10.0.2.2:5005/")
179-
.append(projectId)
180-
.append("/")
181-
.append(region)
182-
.append("/");
183-
} else {
184-
builder
185-
.append("https://")
186-
.append(region)
187-
.append("-")
188-
.append(projectId)
189-
.append(".cloudfunctions.net/");
190-
}
191-
builder.append(function);
175+
String str = String.format(urlFormat, region, projectId, function);
192176
try {
193-
return new URL(builder.toString());
177+
return new URL(str);
194178
} catch (MalformedURLException mfe) {
195-
// This should never happen.
196179
throw new IllegalStateException(mfe);
197180
}
198181
}
199182

200-
/** Sets a flag for this instance to use a test URL instead of the normal URL. */
201-
@VisibleForTesting
202-
void setUseTestURL(boolean value) {
203-
// TODO: Change this once we hook up the test backend with hexa instead of the emulator.
204-
useTestURL = value;
183+
/**
184+
* Changes this instance to point to a Cloud Functions emulator running locally.
185+
* See https://firebase.google.com/docs/functions/local-emulator
186+
*
187+
* @param origin The origin of the local emulator, such as "http://10.0.2.2:5005".
188+
*/
189+
public void useFunctionsEmulator(String origin) {
190+
urlFormat = origin + "/%2$s/%1$s/%3$s";
205191
}
206192

207193
/**

0 commit comments

Comments
 (0)