Skip to content

Commit 12a00d1

Browse files
authored
🐛 Fix empty cookie header parsing and header value set (#1733)
Fixes #1730. ### 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
1 parent 11cf1fd commit 12a00d1

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

plugins/cookie_manager/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Fix empty cookie parsing and header value set.
56
- Improve code formats according to linter rules.
67

78
## 2.1.1
@@ -27,4 +28,4 @@
2728

2829
## 0.0.1 - 2019.9.17
2930

30-
* A cookie manager for Dio.
31+
* A cookie manager for Dio.

plugins/cookie_manager/lib/src/cookie_mgr.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ class CookieManager extends Interceptor {
3535
@override
3636
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
3737
cookieJar.loadForRequest(options.uri).then((cookies) {
38-
options.headers[HttpHeaders.cookieHeader] = getCookies([
38+
final previousCookies =
39+
options.headers[HttpHeaders.cookieHeader] as String?;
40+
final newCookies = getCookies([
3941
...cookies,
40-
...?options.headers[HttpHeaders.cookieHeader]
42+
...?previousCookies
4143
?.split(';')
44+
.where((e) => e.isNotEmpty)
4245
.map((c) => Cookie.fromSetCookieValue(c)),
4346
]);
47+
options.headers[HttpHeaders.cookieHeader] =
48+
newCookies.isNotEmpty ? newCookies : null;
4449
handler.next(options);
4550
}).catchError((dynamic e, StackTrace s) {
4651
final err = DioError(requestOptions: options, error: e, stackTrace: s);

plugins/cookie_manager/test/cookies_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,30 @@ void main() {
8585

8686
cookieManager.onRequest(options, mockRequestInterceptorHandler);
8787
});
88+
89+
group('Empty cookies', () {
90+
test('not produced by default', () async {
91+
final dio = Dio();
92+
dio.interceptors.add(CookieManager(CookieJar()));
93+
final response = await dio.get('http://www.gstatic.com/generate_204');
94+
expect(response.requestOptions.headers[HttpHeaders.cookieHeader], null);
95+
});
96+
97+
test('can be parsed', () async {
98+
final dio = Dio();
99+
dio.interceptors
100+
..add(
101+
InterceptorsWrapper(
102+
onRequest: (options, handler) {
103+
// Write an empty cookie header.
104+
options.headers[HttpHeaders.cookieHeader] = '';
105+
handler.next(options);
106+
},
107+
),
108+
)
109+
..add(CookieManager(CookieJar()));
110+
final response = await dio.get('http://www.gstatic.com/generate_204');
111+
expect(response.requestOptions.headers[HttpHeaders.cookieHeader], null);
112+
});
113+
});
88114
}

0 commit comments

Comments
 (0)