From f9e38a8204ed5a23e7cb3e6dfbeabf3ea079d696 Mon Sep 17 00:00:00 2001 From: khoadng Date: Tue, 5 Sep 2023 22:04:14 +0700 Subject: [PATCH] [http2_adapter] Fix redirect not working (#1946) ### 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 - [ ] 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 ### Additional context and info (if any) --- plugins/http2_adapter/CHANGELOG.md | 1 + plugins/http2_adapter/lib/src/http2_adapter.dart | 4 ++-- plugins/http2_adapter/test/http2_test.dart | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/http2_adapter/CHANGELOG.md b/plugins/http2_adapter/CHANGELOG.md index 3b49c2e88..8e3ef62ce 100644 --- a/plugins/http2_adapter/CHANGELOG.md +++ b/plugins/http2_adapter/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Implement `sendTimeout` and `receiveTimeout` for the adapter. +- Fix redirect not working when requestStream is null. ## 2.3.1+1 diff --git a/plugins/http2_adapter/lib/src/http2_adapter.dart b/plugins/http2_adapter/lib/src/http2_adapter.dart index 6aa4ed822..e235d0e4a 100644 --- a/plugins/http2_adapter/lib/src/http2_adapter.dart +++ b/plugins/http2_adapter/lib/src/http2_adapter.dart @@ -129,7 +129,7 @@ class Http2Adapter implements HttpClientAdapter { if (status != null) { statusCode = int.parse(status); responseHeaders.removeAll(':status'); - needRedirect = list != null && _needRedirect(options, statusCode); + needRedirect = _needRedirect(options, statusCode); needResponse = !needRedirect && options.validateStatus(statusCode) || options.receiveDataWhenStatusError; @@ -182,7 +182,7 @@ class Http2Adapter implements HttpClientAdapter { ); return _fetch( options.copyWith(path: url, maxRedirects: --options.maxRedirects), - Stream.fromIterable(list!), + list != null ? Stream.fromIterable(list) : null, cancelFuture, redirects, ); diff --git a/plugins/http2_adapter/test/http2_test.dart b/plugins/http2_adapter/test/http2_test.dart index b3a777c58..bbe69fb0d 100644 --- a/plugins/http2_adapter/test/http2_test.dart +++ b/plugins/http2_adapter/test/http2_test.dart @@ -117,4 +117,13 @@ void main() { await dio.get('/drip?delay=1&numbytes=1'); }); + + test('request with redirect', () async { + final dio = Dio() + ..options.baseUrl = 'https://httpbun.com/' + ..httpClientAdapter = Http2Adapter(ConnectionManager()); + + final res = await dio.get('absolute-redirect/2'); + expect(res.statusCode, 200); + }); }