Skip to content

Commit

Permalink
♻️ Simply replace \ to / rather than using Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Jul 22, 2023
1 parent f1770b6 commit 45156c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
22 changes: 15 additions & 7 deletions lib/src/file_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import 'storage.dart';

/// Persist [Cookies] in the host file storage.
class FileStorage implements Storage {
FileStorage([this.dir]);
FileStorage([this.dir]) : shouldCreateDirectory = true;

@visibleForTesting
FileStorage.test(this.dir, {this.shouldCreateDirectory = false});

/// Where the cookie files should be saved.
///
Expand All @@ -18,6 +21,10 @@ class FileStorage implements Storage {
/// A storage can be used across different jars, so this cannot be final.
late String _currentDirectory;

/// {@nodoc}
@visibleForTesting
final bool shouldCreateDirectory;

/// {@nodoc}
@visibleForTesting
String get currentDirectory => _currentDirectory;
Expand All @@ -27,12 +34,10 @@ class FileStorage implements Storage {

@override
Future<void> init(bool persistSession, bool ignoreExpires) async {
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/';
// 4 indicates v4 starts to use a new path.
String baseDir = dir?.replaceAll('\\', '/') ?? '.cookies/4/';
if (!baseDir.endsWith('/')) {
baseDir += '/';
}
final StringBuffer sb = StringBuffer(baseDir)
..write('ie${ignoreExpires ? 1 : 0}')
Expand Down Expand Up @@ -84,6 +89,9 @@ class FileStorage implements Storage {
}

Future<void> _makeCookieDir() async {
if (!shouldCreateDirectory) {
return;
}
final directory = Directory(_currentDirectory);
if (!directory.existsSync()) {
await directory.create(recursive: true);
Expand Down
21 changes: 11 additions & 10 deletions test/cookie_jar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,23 @@ void main() async {

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.
final s1 = FileStorage.test('./test/cookies');
final s2 = FileStorage.test('./test/cookies/');
final s3 = FileStorage.test('/test/cookies');
final s4 = FileStorage.test('/test/cookies/');
final s5 = FileStorage.test('C:\\.cookies');
await Future.wait([
s1.init(true, false),
s2.init(true, false),
s3.init(true, false).catchError((_) {}),
s4.init(true, false).catchError((_) {}),
s3.init(true, false),
s4.init(true, false),
s5.init(true, false),
]);
expect(s1.currentDirectory, 'test/cookies/ie0_ps1/');
expect(s2.currentDirectory, 'test/cookies/ie0_ps1/');
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/');
expect(s5.currentDirectory, 'C:/.cookies/ie0_ps1/');
});
});
}

0 comments on commit 45156c2

Please sign in to comment.