feat(flutter): 增加 debug 运行时服务器地址设置
This commit is contained in:
1 parent
b76228905c
commit
907562a874
14 files changed
+590
-27
No files matched your search
@@ -0,0 +1,92 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
|
||||
import 'package:fruitshorts/core/config/app_config.dart';
|
||||
import 'package:fruitshorts/core/config/server_endpoint.dart';
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
SharedPreferencesAsyncPlatform.instance =
|
||||
InMemorySharedPreferencesAsync.empty();
|
||||
AppConfig.clearRuntimeBaseUrl();
|
||||
});
|
||||
|
||||
tearDown(AppConfig.clearRuntimeBaseUrl);
|
||||
|
||||
test('should_normalize_http_urls_and_reject_invalid_values', () {
|
||||
expect(
|
||||
ServerEndpoint.normalize(' https://192.168.1.9:8081/// '),
|
||||
'https://192.168.1.9:8081',
|
||||
);
|
||||
expect(
|
||||
ServerEndpoint.normalize('http://example.test:8081/path/'),
|
||||
'http://example.test:8081/path',
|
||||
);
|
||||
expect(ServerEndpoint.normalize('ftp://example.test'), isNull);
|
||||
expect(ServerEndpoint.normalize('example.test:8081'), isNull);
|
||||
expect(ServerEndpoint.normalize('http://'), isNull);
|
||||
});
|
||||
|
||||
test('should_prioritize_runtime_over_dart_define_over_default', () {
|
||||
expect(
|
||||
ServerEndpoint.resolve(
|
||||
stored: ' http://runtime.test:8081/ ',
|
||||
compiled: 'http://define.test:8081',
|
||||
fallback: 'http://default.test:8081',
|
||||
),
|
||||
'http://runtime.test:8081',
|
||||
);
|
||||
expect(
|
||||
ServerEndpoint.resolve(
|
||||
stored: 'ftp://invalid',
|
||||
compiled: 'http://define.test:8081/',
|
||||
fallback: 'http://default.test:8081',
|
||||
),
|
||||
'http://define.test:8081',
|
||||
);
|
||||
expect(
|
||||
ServerEndpoint.resolve(
|
||||
stored: null,
|
||||
compiled: '',
|
||||
fallback: 'http://default.test:8081/',
|
||||
),
|
||||
'http://default.test:8081',
|
||||
);
|
||||
});
|
||||
|
||||
test('should_apply_stored_url_only_when_debug_is_enabled', () async {
|
||||
SharedPreferencesAsyncPlatform.instance =
|
||||
InMemorySharedPreferencesAsync.withData({
|
||||
ServerEndpoint.preferenceKey: ' http://192.168.31.187:8081/ ',
|
||||
});
|
||||
final original = AppConfig.apiBaseUrl;
|
||||
|
||||
await ServerEndpoint.load(debug: false);
|
||||
expect(AppConfig.apiBaseUrl, original);
|
||||
|
||||
await ServerEndpoint.load(debug: true);
|
||||
expect(AppConfig.apiBaseUrl, 'http://192.168.31.187:8081');
|
||||
});
|
||||
|
||||
test('should_save_normalized_url_and_clear_override', () async {
|
||||
expect(await ServerEndpoint.save('ftp://invalid'), isFalse);
|
||||
expect(
|
||||
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
||||
isNull,
|
||||
);
|
||||
|
||||
expect(await ServerEndpoint.save(' http://192.168.31.187:8081/ '), isTrue);
|
||||
expect(
|
||||
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
||||
'http://192.168.31.187:8081',
|
||||
);
|
||||
|
||||
await ServerEndpoint.clear();
|
||||
expect(
|
||||
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user