Skip to content

Commit

Permalink
Http2 adatper header support dynamic (#2043)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->

### 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
<!-- Provide more context and info about the PR. -->

---------

Signed-off-by: 友人A <[email protected]>
  • Loading branch information
Passer-by authored Nov 27, 2023
1 parent a7a7878 commit 1e370ab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions plugins/http2_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Support non-TLS connection requests.
- Improve the implementation of `receiveTimeout`.
- Add more header value types implicit support.

## 2.3.2

Expand Down
19 changes: 11 additions & 8 deletions plugins/http2_adapter/lib/src/http2_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ 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.entries.map(
(entry) {
final String v;
if (entry.value is Iterable) {
v = entry.value.join(', ');
} else {
v = '${entry.value}';
}
return Header.ascii(entry.key.toLowerCase(), v);
},
).toList(),
);

// Creates a new outgoing stream.
Expand Down
25 changes: 25 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,29 @@ void main() {
final res = await dio.get('absolute-redirect/2');
expect(res.statusCode, 200);
});

test('header value types implicit support', () 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,
},
),
);
final content = res.data.toString();
expect(content, contains('TEST'));
expect(content, contains('Listkey: 1, 2'));
expect(content, contains('Stringkey: 1'));
expect(content, contains('Numkey: 2'));
expect(content, contains('Booleankey: false'));
});
}

0 comments on commit 1e370ab

Please sign in to comment.