Skip to content

Commit

Permalink
Merge pull request #4 from sehnryr/3-merge-cookiejars
Browse files Browse the repository at this point in the history
add `+` operator
  • Loading branch information
myConsciousness committed May 18, 2022
2 parents fdde660 + 8396a5b commit a154498
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/src/sweet_cookie_jar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ abstract class SweetCookieJar {

/// Returns true if this cookie jar contains cookie linked to [name], otherwise false.
bool containsName({required String name});

/// Combines two [SweetCookieJar].
SweetCookieJar operator +(SweetCookieJar cookieJar);
}
10 changes: 10 additions & 0 deletions lib/src/sweet_cookie_jar_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ class SweetCookieJarImpl implements SweetCookieJar {
return false;
}

@override
SweetCookieJar operator +(SweetCookieJar cookieJar) {
final _response = Response.bytes(
[],
200,
headers: {'set-cookie': "$rawData,${cookieJar.rawData}"},
);
return SweetCookieJarImpl.from(response: _response);
}

String _getSetCookie({required Response response}) {
final headers = response.headers;

Expand Down
29 changes: 29 additions & 0 deletions test/sweet_cookie_jar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ final _testResponse = Response.bytes(
},
);

final _partTestResponse1 = Response.bytes(
[],
200,
headers: {
'set-cookie':
'AWSALB=CGSOoaFEi91n9xSfeeSxoUvs0A/TTQn9/Mbxe8dtkv50cBqJmHTwPw3; Expires=Tue, 14 Dec 2021 02:20:37 GMT; Path=/,AWSALBCORS=OHhxYMU0mU7WOoh+4RH5bxe8d6AytmnHaZNGUBqJmHTwPw3; Expires=Tue, 14 Dec 2021 02:20:37 GMT; Path=/; SameSite=None; Secure,jwt_token=test; Domain=.test; Max-Age=31536000; Path=/; expires=Wed, 07-Dec-2022 02:20:37 GMT; SameSite=lax; Secure'
},
);

final _partTestResponse2 = Response.bytes(
[],
200,
headers: {
'set-cookie':
'csrf_token=test==; Domain=.test; Max-Age=31536000; Path=/; expires=Wed, 07-Dec-2022 02:20:37 GMT,csrf_token=test==; Domain=.test; Max-Age=31536000; Path=/; expires=Wed, 07-Dec-2022 02:20:37 GMT,wuuid=77be8f46-4'
},
);

void main() {
group('Test constructor.', () {
test('Test constructor.', () {
Expand All @@ -36,6 +54,17 @@ void main() {
});
});

group('Test operator +.', () {
test('Test operator +.', () {
final cookieJar1 = SweetCookieJar.from(response: _partTestResponse1);
final cookieJar2 = SweetCookieJar.from(response: _partTestResponse2);
final cookieJar3 = cookieJar1 + cookieJar2;

expect(cookieJar3.nameSet,
{"AWSALB", "AWSALBCORS", "jwt_token", "csrf_token", "wuuid"});
});
});

group('Test find.', () {
test('Test find AWSALB', () {
final cookieJar = SweetCookieJar.from(response: _testResponse);
Expand Down

0 comments on commit a154498

Please sign in to comment.