Skip to content

Commit

Permalink
[flutter_svg] Fix SvgNetworkLoader not closing internal http client (#…
Browse files Browse the repository at this point in the history
…8126)

closes flutter/flutter#158928

Ensures that if a Client is created in `prepareMessage` it is closed after getting the resource.
  • Loading branch information
derdilla authored Nov 21, 2024
1 parent 2703b67 commit 013cc24
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
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

* Fixes `SvgNetworkLoader` not closing internally created http clients.

## 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
46 changes: 46 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,18 @@ class _TestColorMapper extends ColorMapper {
return color;
}
}

class VerifyCloseClient extends Fake implements http.Client {
bool closeCalled = false;

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

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

0 comments on commit 013cc24

Please sign in to comment.