Skip to content
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
7 changes: 5 additions & 2 deletions packages/google_cloud_firestore/lib/src/firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ class Settings {
/// set this to 'localhost:8080' (or your emulator's host:port).
final String? host;

/// Whether to use SSL when connecting.
/// Whether to connect over HTTPS.
///
/// Defaults to true. Set to false when using the emulator.
/// Defaults to `true`. Set it to `false` alongside [host] to reach a plain
/// HTTP endpoint, such as a locally running emulator.
///
/// Ignored when `FIRESTORE_EMULATOR_HOST` is set, which always uses HTTP.
final bool ssl;

/// The credential to use for authentication.
Expand Down
16 changes: 12 additions & 4 deletions packages/google_cloud_firestore/lib/src/firestore_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,15 @@ class FirestoreHttpClient {
return discovered != null ? (_cachedProjectId = discovered) : null;
}

/// Gets the Firestore API host URL based on emulator configuration.
Uri get _firestoreApiHost {
/// Gets the Firestore API host URL.
///
/// `FIRESTORE_EMULATOR_HOST` takes precedence over [Settings.host], since it
/// is the documented way to redirect the SDK at an emulator. Otherwise the
/// scheme follows [Settings.ssl], which lets a custom [Settings.host] be
/// reached over plain HTTP.
@internal
@visibleForTesting
Uri get firestoreApiHost {
final emulatorHost = Environment.getFirestoreEmulatorHost(
_settings.environmentOverride,
);
Expand All @@ -182,7 +189,8 @@ class FirestoreHttpClient {
return Uri.http(emulatorHost, '/');
}

return Uri.https(_settings.host ?? 'firestore.googleapis.com', '/');
final host = _settings.host ?? 'firestore.googleapis.com';
return _settings.ssl ? Uri.https(host, '/') : Uri.http(host, '/');
}

/// Checks if the Firestore emulator is enabled via environment variable.
Expand Down Expand Up @@ -242,7 +250,7 @@ class FirestoreHttpClient {
Future<R> Function(firestore_v1.Firestore api, String projectId) fn,
) => _run(
(client, projectId) => fn(
firestore_v1.Firestore(client: client, endPoint: _firestoreApiHost),
firestore_v1.Firestore(client: client, endPoint: firestoreApiHost),
projectId,
),
);
Expand Down
88 changes: 88 additions & 0 deletions packages/google_cloud_firestore/test/firestore_api_host_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2026 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.

import 'package:google_cloud_firestore/google_cloud_firestore.dart';
import 'package:google_cloud_firestore/src/environment.dart';
import 'package:google_cloud_firestore/src/firestore_http_client.dart';
import 'package:test/test.dart';

/// Builds a client for host resolution only; no request is ever sent.
///
/// [Settings.environmentOverride] is always supplied so the ambient
/// `FIRESTORE_EMULATOR_HOST` cannot leak into these expectations.
FirestoreHttpClient _clientFor({
String? host,
bool ssl = true,
Map<String, String> environment = const {},
}) {
return FirestoreHttpClient(
credential: Credential.fromApplicationDefaultCredentials(),
settings: Settings(
projectId: 'test-project',
host: host,
ssl: ssl,
environmentOverride: environment,
),
);
}

void main() {
group('firestoreApiHost', () {
test('defaults to the production endpoint over HTTPS', () {
final uri = _clientFor().firestoreApiHost;

expect(uri.scheme, 'https');
expect(uri.host, 'firestore.googleapis.com');
});

test('uses HTTPS for a custom host when ssl is true', () {
final uri = _clientFor(host: 'example.test:8080').firestoreApiHost;

expect(uri.scheme, 'https');
expect(uri.host, 'example.test');
expect(uri.port, 8080);
});

test('uses HTTP for a custom host when ssl is false', () {
final uri = _clientFor(
host: '127.0.0.1:8080',
ssl: false,
).firestoreApiHost;

expect(uri.scheme, 'http');
expect(uri.host, '127.0.0.1');
expect(uri.port, 8080);
});

test('uses HTTP against the default host when ssl is false', () {
final uri = _clientFor(ssl: false).firestoreApiHost;

expect(uri.scheme, 'http');
expect(uri.host, 'firestore.googleapis.com');
});

test('FIRESTORE_EMULATOR_HOST wins over host and ssl', () {
final uri = _clientFor(
host: 'example.test:9090',
environment: const {
Environment.firestoreEmulatorHost: '127.0.0.1:8080',
},
).firestoreApiHost;

expect(uri.scheme, 'http');
expect(uri.host, '127.0.0.1');
expect(uri.port, 8080);
});
});
}
Loading