diff --git a/CHANGELOG.md b/CHANGELOG.md index e8cbdae..61586e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # CHANGELOG -## 4.0.6 +## 4.0.7 +- Fix directory parsing for the file storage. - Add `WebCookieJar` to convenient Web usages instead of throwing exceptions. ## 4.0.5 diff --git a/lib/src/file_storage.dart b/lib/src/file_storage.dart index 66e2362..3e72b26 100644 --- a/lib/src/file_storage.dart +++ b/lib/src/file_storage.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'package:meta/meta.dart'; import 'package:universal_io/io.dart' show Directory, File; import 'storage.dart'; @@ -17,13 +18,23 @@ class FileStorage implements Storage { /// A storage can be used across different jars, so this cannot be final. late String _currentDirectory; + /// {@nodoc} + @visibleForTesting + String get currentDirectory => _currentDirectory; + String? Function(Uint8List list)? readPreHandler; List Function(String value)? writePreHandler; @override Future init(bool persistSession, bool ignoreExpires) async { - // 4 indicates v4 starts to use a new path. - final StringBuffer sb = StringBuffer(dir ?? '.cookies/4/') + final String baseDir; + if (dir != null) { + baseDir = Uri.directory(dir!).toString().replaceFirst('file://', ''); + } else { + // 4 indicates v4 starts to use a new path. + baseDir = '.cookies/4/'; + } + final StringBuffer sb = StringBuffer(baseDir) ..write('ie${ignoreExpires ? 1 : 0}') ..write('_ps${persistSession ? 1 : 0}') ..write('/'); diff --git a/pubspec.yaml b/pubspec.yaml index bdca496..3647ad9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: cookie_jar description: A cookie manager for http requests in Dart, help you to deal with the cookie policies and persistence. -version: 4.0.6 +version: 4.0.7 repository: https://github.com/flutterchina/cookie_jar issue_tracker: https://github.com/flutterchina/cookie_jar/issues @@ -8,6 +8,7 @@ environment: sdk: '>=2.15.0 <3.0.0' dependencies: + meta: ^1.5.0 universal_io: ^2.2.0 dev_dependencies: diff --git a/test/cookie_jar_test.dart b/test/cookie_jar_test.dart index ff21613..dfecece 100644 --- a/test/cookie_jar_test.dart +++ b/test/cookie_jar_test.dart @@ -143,22 +143,22 @@ void main() async { final cookies = [ Cookie('JSESSIONID', 'wendux')..domain = '.mozilla.org', ]; - await cj.saveFromResponse(Uri.parse('http://www.mozilla.org/'), cookies); + await cj.saveFromResponse(Uri.parse('https://www.mozilla.org/'), cookies); final results1 = - await cj.loadForRequest(Uri.parse('http://mozilla.org/')); + await cj.loadForRequest(Uri.parse('https://mozilla.org/')); expect(results1.length, 1); final results2 = - await cj.loadForRequest(Uri.parse('http://developer.mozilla.org/')); + await cj.loadForRequest(Uri.parse('https://developer.mozilla.org/')); expect(results2.length, 1); final results3 = - await cj.loadForRequest(Uri.parse('http://fakemozilla.org/')); + await cj.loadForRequest(Uri.parse('https://fakemozilla.org/')); expect(results3.length, 0); final results4 = - await cj.loadForRequest(Uri.parse('http://mozilla.org.com/')); + await cj.loadForRequest(Uri.parse('https://mozilla.org.com/')); expect(results4.length, 0); }); @@ -167,16 +167,16 @@ void main() async { final cookies = [ Cookie('JSESSIONID', 'wendux')..domain = '.mozilla.org', ]; - await cj.saveFromResponse(Uri.parse('http://www.mozilla.org/'), cookies); + await cj.saveFromResponse(Uri.parse('https://www.mozilla.org/'), cookies); - await cj.delete(Uri.parse('http://www.fakemozilla.org/'), true); + await cj.delete(Uri.parse('https://www.fakemozilla.org/'), true); final results1 = - await cj.loadForRequest(Uri.parse('http://www.mozilla.org/')); + await cj.loadForRequest(Uri.parse('https://www.mozilla.org/')); expect(results1.length, 1); - await cj.delete(Uri.parse('http://developer.mozilla.org/'), true); + await cj.delete(Uri.parse('https://developer.mozilla.org/'), true); final results2 = - await cj.loadForRequest(Uri.parse('http://www.mozilla.org/')); + await cj.loadForRequest(Uri.parse('https://www.mozilla.org/')); expect(results2.length, 0); }); @@ -272,4 +272,25 @@ void main() async { final otherResults = await otherCj.loadForRequest(uri); expect(otherResults, isEmpty); }); + + group('FileStorage', () { + test('Parsed directory correctly', () async { + final s1 = FileStorage('./test/cookies'); + final s2 = FileStorage('./test/cookies/'); + final s3 = FileStorage('/test/cookies'); + final s4 = FileStorage('/test/cookies/'); + // Silence s3 and s4 because those directories + // won't have permission to create. + await Future.wait([ + s1.init(true, false), + s2.init(true, false), + s3.init(true, false).catchError((_) {}), + s4.init(true, false).catchError((_) {}), + ]); + expect(s1.currentDirectory, 'test/cookies/ie0_ps1/'); + expect(s2.currentDirectory, 'test/cookies/ie0_ps1/'); + expect(s3.currentDirectory, '/test/cookies/ie0_ps1/'); + expect(s4.currentDirectory, '/test/cookies/ie0_ps1/'); + }); + }); }