fix(config): 指定 API_BASE_URL 时跳过地址探测
This commit is contained in:
1 parent
2c5ff3579c
commit
9ebb54ecfd
2 files changed
+61
No files matched your search
@@ -85,7 +85,15 @@ class AppConfig {
|
|||||||
static Future<void> probeAndApplyBaseUrl({
|
static Future<void> probeAndApplyBaseUrl({
|
||||||
Future<bool> Function(String baseUrl)? probe,
|
Future<bool> Function(String baseUrl)? probe,
|
||||||
String? fallbackBaseUrl,
|
String? fallbackBaseUrl,
|
||||||
|
|
||||||
|
/// 编译期地址测试注入;未传时读取 `API_BASE_URL`。
|
||||||
|
String? compiledOverride,
|
||||||
}) async {
|
}) async {
|
||||||
|
final compiled = compiledOverride ?? compiledBaseUrl;
|
||||||
|
if (compiled.isNotEmpty) {
|
||||||
|
if (_runtimeBaseUrl.isEmpty) applyRuntimeBaseUrl(compiled);
|
||||||
|
return;
|
||||||
|
}
|
||||||
final candidates = <String>[
|
final candidates = <String>[
|
||||||
if (_runtimeBaseUrl.isNotEmpty) _runtimeBaseUrl,
|
if (_runtimeBaseUrl.isNotEmpty) _runtimeBaseUrl,
|
||||||
for (final candidate in _probeCandidates)
|
for (final candidate in _probeCandidates)
|
||||||
|
|||||||
@@ -66,6 +66,59 @@ void main() {
|
|||||||
expect(AppConfig.devHostBaseUrl, 'http://192.168.31.187:8081');
|
expect(AppConfig.devHostBaseUrl, 'http://192.168.31.187:8081');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should_skip_probe_when_compiled_override_is_set', () async {
|
||||||
|
const compiled = 'http://compiled.test:8081';
|
||||||
|
var probeCalls = 0;
|
||||||
|
|
||||||
|
await AppConfig.probeAndApplyBaseUrl(
|
||||||
|
compiledOverride: compiled,
|
||||||
|
probe: (_) async {
|
||||||
|
probeCalls++;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(probeCalls, 0);
|
||||||
|
expect(AppConfig.apiBaseUrl, compiled);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_keep_runtime_endpoint_when_compiled_override_is_set', () async {
|
||||||
|
const runtime = 'http://runtime.test:8081';
|
||||||
|
const compiled = 'http://compiled.test:8081';
|
||||||
|
var probeCalls = 0;
|
||||||
|
AppConfig.applyRuntimeBaseUrl(runtime);
|
||||||
|
|
||||||
|
await AppConfig.probeAndApplyBaseUrl(
|
||||||
|
compiledOverride: compiled,
|
||||||
|
probe: (_) async {
|
||||||
|
probeCalls++;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(probeCalls, 0);
|
||||||
|
expect(AppConfig.apiBaseUrl, runtime);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_keep_probing_when_compiled_override_is_empty', () async {
|
||||||
|
final attempts = <String>[];
|
||||||
|
|
||||||
|
await AppConfig.probeAndApplyBaseUrl(
|
||||||
|
compiledOverride: '',
|
||||||
|
probe: (baseUrl) async {
|
||||||
|
attempts.add(baseUrl);
|
||||||
|
return baseUrl == AppConfig.publicBaseUrl;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(attempts, [
|
||||||
|
AppConfig.lanBaseUrl,
|
||||||
|
AppConfig.devHostBaseUrl,
|
||||||
|
AppConfig.publicBaseUrl,
|
||||||
|
]);
|
||||||
|
expect(AppConfig.apiBaseUrl, AppConfig.publicBaseUrl);
|
||||||
|
});
|
||||||
|
|
||||||
test(
|
test(
|
||||||
'should_probe_all_candidates_in_parallel_before_applying_priority',
|
'should_probe_all_candidates_in_parallel_before_applying_priority',
|
||||||
() async {
|
() async {
|
||||||
|
|||||||
Reference in new issue
Block a user