From 8b4064b7e8dc26e72a2b853b533c4d207e970a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=8B=E4=BA=BAA?= <1017480401@qq.com> Date: Tue, 14 Nov 2023 23:42:23 +0800 Subject: [PATCH] Http2Adapter header supports dynamic --- .../http2_adapter/lib/src/http2_adapter.dart | 20 +++++++++------- plugins/http2_adapter/test/http2_test.dart | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/plugins/http2_adapter/lib/src/http2_adapter.dart b/plugins/http2_adapter/lib/src/http2_adapter.dart index e235d0e4a..7fb9b47df 100644 --- a/plugins/http2_adapter/lib/src/http2_adapter.dart +++ b/plugins/http2_adapter/lib/src/http2_adapter.dart @@ -61,14 +61,18 @@ class Http2Adapter implements HttpClientAdapter { // Add custom headers headers.addAll( - options.headers.keys - .map( - (key) => Header.ascii( - key.toLowerCase(), - options.headers[key] as String? ?? '', - ), - ) - .toList(), + options.headers.keys.map( + (key) { + final String v; + final value = options.headers[key]; + if (value is Iterable) { + v = value.join(', '); + } else { + v = '$value'.trim(); + } + return Header.ascii(key.toLowerCase(), v); + }, + ).toList(), ); // Creates a new outgoing stream. diff --git a/plugins/http2_adapter/test/http2_test.dart b/plugins/http2_adapter/test/http2_test.dart index 656788912..d1bf888f1 100644 --- a/plugins/http2_adapter/test/http2_test.dart +++ b/plugins/http2_adapter/test/http2_test.dart @@ -127,4 +127,28 @@ void main() { final res = await dio.get('absolute-redirect/2'); expect(res.statusCode, 200); }); + + test('header supports dynamic', () async { + final dio = Dio() + ..options.baseUrl = 'https://httpbun.com/' + ..httpClientAdapter = Http2Adapter(ConnectionManager()); + + final res = await dio.post( + 'post', + data: 'TEST', + options: Options( + headers: { + 'ListKey': ['1', '2'], + 'StringKey': '1', + 'NumKey': 2, + 'BooleanKey': false, + }, + ), + ); + expect(res.data.toString(), contains('TEST')); + expect(res.data.toString(), contains('Listkey: 1,2')); + expect(res.data.toString(), contains('Stringkey: 1')); + expect(res.data.toString(), contains('Numkey: 2')); + expect(res.data.toString(), contains('Booleankey: false')); + }); }