Skip to content

Commit

Permalink
Http2Adapter header supports dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Passer-by committed Nov 14, 2023
1 parent bedcc54 commit 8b4064b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
20 changes: 12 additions & 8 deletions plugins/http2_adapter/lib/src/http2_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

0 comments on commit 8b4064b

Please sign in to comment.