From a3f61a9ca458de1f281d94c76d6b77b54a922d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=8B=E4=BA=BAA?= <58846244+Passer-by@users.noreply.github.com> Date: Sat, 13 May 2023 00:29:23 +0800 Subject: [PATCH] Fix IOHttpClientAdapter.onHttpClientCreate Repeated calls (#1810) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #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 <58846244+Passer-by@users.noreply.github.com> Co-authored-by: Peter Leibiger Co-authored-by: Alex Li --- dio/CHANGELOG.md | 1 + dio/lib/src/adapters/io_adapter.dart | 21 ++++++++++----------- dio/test/adapters_test.dart | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 dio/test/adapters_test.dart diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index e04d7a1b4..90a81376e 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -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 diff --git a/dio/lib/src/adapters/io_adapter.dart b/dio/lib/src/adapters/io_adapter.dart index 648a71e8f..20bd3eda2 100644 --- a/dio/lib/src/adapters/io_adapter.dart +++ b/dio/lib/src/adapters/io_adapter.dart @@ -35,7 +35,7 @@ class IOHttpClientAdapter implements HttpClientAdapter { /// [validateCertificate] evaluates the leaf certificate. ValidateCertificate? validateCertificate; - HttpClient? _defaultHttpClient; + HttpClient? _cachedHttpClient; bool _closed = false; @@ -187,27 +187,26 @@ class IOHttpClientAdapter implements HttpClientAdapter { Future? 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); } } diff --git a/dio/test/adapters_test.dart b/dio/test/adapters_test.dart new file mode 100644 index 000000000..1c9ef4ac4 --- /dev/null +++ b/dio/test/adapters_test.dart @@ -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); + }); + }); +}