fix(network): close request-owned HTTP clients reliably

This commit is contained in:
zarzet committed 2026-10-06 22:00:46 +07:00
1 parent 10db2f272c
commit 2ce3c33c87
3 files changed
+51 -5

No files matched your search

+36
View File
@@ -11,6 +11,18 @@ import 'package:spotiflac_android/screens/settings/donate_page.dart';
import 'package:spotiflac_android/services/app_remote_config_service.dart';
import 'package:spotiflac_android/theme/mornye_theme.dart';
class _TrackedClient extends MockClient {
_TrackedClient(super.handler);
bool closed = false;
@override
void close() {
closed = true;
super.close();
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
@@ -18,6 +30,30 @@ void main() {
SharedPreferences.setMockInitialValues({});
});
test('default request closes its client on success and failure', () async {
for (final fail in [false, true]) {
final client = _TrackedClient((_) async {
if (fail) throw StateError('network unavailable');
return http.Response('{}', 200);
});
final snapshot = await http.runWithClient(
() => AppRemoteConfigService().fetchConfigSnapshot(),
() => client,
);
expect(snapshot, fail ? isNull : isNotNull);
expect(client.closed, isTrue);
}
});
test('injected client remains owned by its caller', () async {
final client = _TrackedClient((_) async => http.Response('{}', 200));
final service = AppRemoteConfigService(client: client);
expect(await service.fetchConfigSnapshot(), isNotNull);
expect(client.closed, isFalse);
expect(await service.fetchConfigSnapshot(), isNotNull);
client.close();
});
test(
'parses monthly donation progress and display settings from the API',
() {