diff --git a/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart b/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart index 5913eda..9238723 100644 --- a/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart +++ b/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart @@ -24,8 +24,17 @@ class FirebaseAdminApp { /// Use the Firebase Emulator Suite to run the app locally. void useEmulator() { _isUsingEmulator = true; - authApiHost = Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/'); - firestoreApiHost = Uri.http('127.0.0.1:8080', '/'); + final env = + Zone.current[envSymbol] as Map? ?? Platform.environment; + + authApiHost = Uri.http( + env['FIREBASE_AUTH_EMULATOR_HOST'] ?? '127.0.0.1:9099', + 'identitytoolkit.googleapis.com/', + ); + firestoreApiHost = Uri.http( + env['FIRESTORE_EMULATOR_HOST'] ?? '127.0.0.1:8080', + '/', + ); } @internal diff --git a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart index 132cfc2..6830cc1 100644 --- a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart +++ b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:dart_firebase_admin/src/app.dart'; import 'package:test/test.dart'; @@ -17,21 +19,36 @@ void main() { ); }); - test('useEmulator() sets the apiHost to the emulator', () { - final app = FirebaseAdminApp.initializeApp( - 'dart-firebase-admin', - Credential.fromApplicationDefaultCredentials(), - ); + test( + 'useEmulator() uses environment variables to set apiHost to the emulator', + () async { + const firebaseAuthEmulatorHost = '127.0.0.1:9000'; + const firestoreEmulatorHost = '127.0.0.1:8000'; + final testEnv = { + 'FIREBASE_AUTH_EMULATOR_HOST': firebaseAuthEmulatorHost, + 'FIRESTORE_EMULATOR_HOST': firestoreEmulatorHost, + }; - app.useEmulator(); + await runZoned( + zoneValues: {envSymbol: testEnv}, + () async { + final app = FirebaseAdminApp.initializeApp( + 'dart-firebase-admin', + Credential.fromApplicationDefaultCredentials(), + ); - expect( - app.authApiHost, - Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/'), - ); - expect( - app.firestoreApiHost, - Uri.http('127.0.0.1:8080', '/'), + app.useEmulator(); + + expect( + app.authApiHost, + Uri.http( + firebaseAuthEmulatorHost, 'identitytoolkit.googleapis.com/'), + ); + expect( + app.firestoreApiHost, + Uri.http(firestoreEmulatorHost, '/'), + ); + }, ); }); });