Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/cookie_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

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

## 2.1.1
Expand All @@ -27,4 +28,4 @@

## 0.0.1 - 2019.9.17

* A cookie manager for Dio.
* A cookie manager for Dio.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

9 changes: 7 additions & 2 deletions plugins/cookie_manager/lib/src/cookie_mgr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ class CookieManager extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
cookieJar.loadForRequest(options.uri).then((cookies) {
options.headers[HttpHeaders.cookieHeader] = getCookies([
final previousCookies =
options.headers[HttpHeaders.cookieHeader] as String?;
final newCookies = getCookies([
...cookies,
...?options.headers[HttpHeaders.cookieHeader]
...?previousCookies
?.split(';')
.where((e) => e.isNotEmpty)
.map((c) => Cookie.fromSetCookieValue(c)),
]);
options.headers[HttpHeaders.cookieHeader] =
newCookies.isNotEmpty ? newCookies : null;
handler.next(options);
}).catchError((dynamic e, StackTrace s) {
final err = DioError(requestOptions: options, error: e, stackTrace: s);
Expand Down
26 changes: 26 additions & 0 deletions plugins/cookie_manager/test/cookies_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,30 @@ void main() {

cookieManager.onRequest(options, mockRequestInterceptorHandler);
});

group('Empty cookies', () {
test('not produced by default', () async {
final dio = Dio();
dio.interceptors.add(CookieManager(CookieJar()));
final response = await dio.get('http://www.gstatic.com/generate_204');
expect(response.requestOptions.headers[HttpHeaders.cookieHeader], null);
});

test('can be parsed', () async {
final dio = Dio();
dio.interceptors
..add(
InterceptorsWrapper(
onRequest: (options, handler) {
// Write an empty cookie header.
options.headers[HttpHeaders.cookieHeader] = '';
handler.next(options);
},
),
)
..add(CookieManager(CookieJar()));
final response = await dio.get('http://www.gstatic.com/generate_204');
expect(response.requestOptions.headers[HttpHeaders.cookieHeader], null);
});
});
}