Skip to content

Commit

Permalink
🐛 Parse incoming direcotry correctly (#50)
Browse files Browse the repository at this point in the history
Fixes #48.
  • Loading branch information
AlexV525 authored Jul 18, 2023
1 parent 5585d24 commit 168da43
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 13 additions & 2 deletions lib/src/file_storage.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<int> Function(String value)? writePreHandler;

@override
Future<void> 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('/');
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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

environment:
sdk: '>=2.15.0 <3.0.0'

dependencies:
meta: ^1.5.0
universal_io: ^2.2.0

dev_dependencies:
Expand Down
41 changes: 31 additions & 10 deletions test/cookie_jar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@ void main() async {
final cookies = <Cookie>[
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);
});

Expand All @@ -167,16 +167,16 @@ void main() async {
final cookies = <Cookie>[
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);
});

Expand Down Expand Up @@ -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/');
});
});
}

0 comments on commit 168da43

Please sign in to comment.