Skip to content

Commit

Permalink
Merge pull request #1856 from nextcloud/fix/dynamite_runtime/cookie_h…
Browse files Browse the repository at this point in the history
…eader

fix(dynamite_runtime): cookie header format
  • Loading branch information
Leptopoda authored Apr 4, 2024
2 parents 476de35 + b6f3965 commit a67df8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
17 changes: 16 additions & 1 deletion packages/dynamite/dynamite_runtime/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ class DynamiteClient with http.BaseClient {
if (cookieJar != null) {
final cookies = await cookieJar!.loadForRequest(request.url);
if (cookies.isNotEmpty) {
request.headers['cookie'] = cookies.join('; ');
final buffer = StringBuffer();

for (final entry in cookies.indexed) {
final cookie = entry.$2;

buffer
..write(cookie.name)
..write('=')
..write(cookie.value);

if (entry.$1 < cookies.length - 1) {
buffer.write('; ');
}
}

request.headers['cookie'] = buffer.toString();
}
}

Expand Down
14 changes: 9 additions & 5 deletions packages/dynamite/dynamite_runtime/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {

test('Cookies', () async {
final mockedClient = MockClient((request) async {
expect(request.headers['cookie'], equals(Cookie('a', 'b').toString()));
expect(request.headers['cookie'], equals('a=b; a2=b2; a3=b3'));

return Response(
'',
Expand All @@ -32,15 +32,19 @@ void main() {
cookieJar: cookieJar,
);

await cookieJar.saveFromResponse(uri, [Cookie('a', 'b')]);
await cookieJar.saveFromResponse(uri, [
Cookie('a', 'b'),
Cookie('a2', 'b2'),
Cookie('a3', 'b3'),
]);
await client.get(uri);

final cookies = await cookieJar.loadForRequest(uri);
expect(cookies, hasLength(2));
expect(cookies, hasLength(4));
expect(cookies[0].name, 'a');
expect(cookies[0].value, 'b');
expect(cookies[1].name, 'c');
expect(cookies[1].value, 'd');
expect(cookies.last.name, 'c');
expect(cookies.last.value, 'd');
});

test('No cookies', () async {
Expand Down

0 comments on commit a67df8e

Please sign in to comment.