Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flutter_svg] Fix SvgNetworkLoader not closing internal http client #8126

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions third_party/packages/flutter_svg/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.15

* Fix `SvgNetworkLoader` not closing internally created http clients.
derdilla marked this conversation as resolved.
Show resolved Hide resolved

## 2.0.14

* Makes the package WASM compatible.
Expand Down
7 changes: 6 additions & 1 deletion third_party/packages/flutter_svg/lib/src/loaders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ class SvgNetworkLoader extends SvgLoader<Uint8List> {
@override
Future<Uint8List?> prepareMessage(BuildContext? context) async {
final http.Client client = _httpClient ?? http.Client();
return (await client.get(Uri.parse(url), headers: headers)).bodyBytes;
final http.Response response =
await client.get(Uri.parse(url), headers: headers);
if (_httpClient == null) {
client.close();
}
return response.bodyBytes;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion third_party/packages/flutter_svg/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_svg
description: An SVG rendering and widget library for Flutter, which allows painting and displaying Scalable Vector Graphics 1.1 files.
repository: https://github.com/flutter/packages/tree/main/third_party/packages/flutter_svg
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_svg%22
version: 2.0.14
version: 2.0.15

environment:
sdk: ^3.4.0
Expand Down
47 changes: 47 additions & 0 deletions third_party/packages/flutter_svg/test/loaders_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;

void main() {
setUp(() {
Expand Down Expand Up @@ -57,6 +58,36 @@ void main() {
expect((await loader.prepareMessage(null))!.lengthInBytes, 0);
expect((await packageLoader.prepareMessage(null))!.lengthInBytes, 1);
});

test('SvgNetworkLoader closes internal client', () async {
final List<VerifyCloseClient> createdClients = <VerifyCloseClient>[];

await http.runWithClient(() async {
const SvgNetworkLoader loader = SvgNetworkLoader('');

expect(createdClients, isEmpty);
await loader.prepareMessage(null);

expect(createdClients, hasLength(1));
expect(createdClients[0].closeCalled, isTrue);
}, () {
final VerifyCloseClient client = VerifyCloseClient();
createdClients.add(client);
return client;
});
});

test("SvgNetworkLoader doesn't close passed client", () async {
final VerifyCloseClient client = VerifyCloseClient();
final SvgNetworkLoader loader = SvgNetworkLoader(
'',
httpClient: client as http.Client,
);

expect(client.closeCalled, isFalse);
await loader.prepareMessage(null);
expect(client.closeCalled, isFalse);
});
}

class TestBundle extends Fake implements AssetBundle {
Expand Down Expand Up @@ -96,3 +127,19 @@ class _TestColorMapper extends ColorMapper {
return color;
}
}

class VerifyCloseClient implements http.Client {
derdilla marked this conversation as resolved.
Show resolved Hide resolved
bool closeCalled = false;

Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
return http.Response('', 200);
}

void close() {
assert(!closeCalled);
closeCalled = true;
}

@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}