diff --git a/plugins/cookie_manager/CHANGELOG.md b/plugins/cookie_manager/CHANGELOG.md index ec1632081..ce0ab30d2 100644 --- a/plugins/cookie_manager/CHANGELOG.md +++ b/plugins/cookie_manager/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fix empty cookie parsing and header value set. - Improve code formats according to linter rules. ## 2.1.1 @@ -27,4 +28,4 @@ ## 0.0.1 - 2019.9.17 -* A cookie manager for Dio. +* A cookie manager for Dio. diff --git a/plugins/cookie_manager/lib/src/cookie_mgr.dart b/plugins/cookie_manager/lib/src/cookie_mgr.dart index d8a96b268..b41480b58 100644 --- a/plugins/cookie_manager/lib/src/cookie_mgr.dart +++ b/plugins/cookie_manager/lib/src/cookie_mgr.dart @@ -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); diff --git a/plugins/cookie_manager/test/cookies_test.dart b/plugins/cookie_manager/test/cookies_test.dart index 0cde746da..9646ab55b 100644 --- a/plugins/cookie_manager/test/cookies_test.dart +++ b/plugins/cookie_manager/test/cookies_test.dart @@ -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); + }); + }); }