Skip to content

Commit

Permalink
Fix IOHttpClientAdapter.onHttpClientCreate Repeated calls (cfug#1810)
Browse files Browse the repository at this point in the history
Fix cfug#1809

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

---------

Signed-off-by: 友人A <[email protected]>
Co-authored-by: Peter Leibiger <[email protected]>
Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent e663d8a commit a3f61a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Make `LogInterceptor` prints in DEBUG mode (when the assertion is enabled) by default.
- Fix `IOHttpClientAdapter.onHttpClientCreate` Repeated calls

## 5.1.2

Expand Down
21 changes: 10 additions & 11 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class IOHttpClientAdapter implements HttpClientAdapter {
/// [validateCertificate] evaluates the leaf certificate.
ValidateCertificate? validateCertificate;

HttpClient? _defaultHttpClient;
HttpClient? _cachedHttpClient;

bool _closed = false;

Expand Down Expand Up @@ -187,27 +187,26 @@ class IOHttpClientAdapter implements HttpClientAdapter {
Future<void>? cancelFuture,
Duration? connectionTimeout,
) {
HttpClient client = onHttpClientCreate?.call(HttpClient()) ?? HttpClient();
if (cancelFuture != null) {
final HttpClient client =
onHttpClientCreate?.call(HttpClient()) ?? HttpClient();
client.userAgent = null;
client.idleTimeout = Duration(seconds: 0);
cancelFuture.whenComplete(() => client.close(force: true));
return client..connectionTimeout = connectionTimeout;
}
if (_defaultHttpClient == null) {
client.idleTimeout = Duration(seconds: 3);
if (onHttpClientCreate?.call(client) != null) {
client = onHttpClientCreate!(client)!;
}
client.connectionTimeout = connectionTimeout;
_defaultHttpClient = client;

if (_cachedHttpClient == null) {
final HttpClient client = HttpClient()
..idleTimeout = Duration(seconds: 3);
_cachedHttpClient = onHttpClientCreate?.call(client) ?? client;
}
return _defaultHttpClient!..connectionTimeout = connectionTimeout;
return _cachedHttpClient!..connectionTimeout = connectionTimeout;
}

@override
void close({bool force = false}) {
_closed = true;
_defaultHttpClient?.close(force: force);
_cachedHttpClient?.close(force: force);
}
}
20 changes: 20 additions & 0 deletions dio/test/adapters_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:test/test.dart';

void main() {
group(HttpClientAdapter, () {
test(
'IOHttpClientAdapter.onHttpClientCreate is only executed once per request',
() async {
int onHttpClientCreateInvokeCount = 0;
final dio = Dio();
dio.httpClientAdapter = IOHttpClientAdapter(onHttpClientCreate: (client) {
onHttpClientCreateInvokeCount++;
return client;
});
await dio.get('https://pub.dev');
expect(onHttpClientCreateInvokeCount, 1);
});
});
}

0 comments on commit a3f61a9

Please sign in to comment.