Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for sending "cookie" and receiving "set-cookie" headers #1113

Merged
merged 8 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/cronet_http/example/integration_test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Future<void> testConformance() async {
() => testAll(
CronetClient.defaultCronetEngine,
canStreamRequestBody: false,
canReceiveSetCookieHeaders: true,
canSendCookieHeaders: true,
));

group('from cronet engine', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('defaultSessionConfiguration', () {
testAll(CupertinoClient.defaultSessionConfiguration);
testAll(
CupertinoClient.defaultSessionConfiguration,
canReceiveSetCookieHeaders: true,
canSendCookieHeaders: true,
);
});
group('fromSessionConfiguration', () {
final config = URLSessionConfiguration.ephemeralSessionConfiguration();
testAll(() => CupertinoClient.fromSessionConfiguration(config),
canWorkInIsolates: false);
testAll(
() => CupertinoClient.fromSessionConfiguration(config),
canWorkInIsolates: false,
canReceiveSetCookieHeaders: true,
canSendCookieHeaders: true,
);
});
}
7 changes: 5 additions & 2 deletions pkgs/http/test/io/client_conformance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import 'package:http_client_conformance_tests/http_client_conformance_tests.dart
import 'package:test/test.dart';

void main() {
testAll(IOClient.new, preservesMethodCase: false // https://dartbug.com/54187
);
testAll(
IOClient.new, preservesMethodCase: false, // https://dartbug.com/54187
canReceiveSetCookieHeaders: true,
canSendCookieHeaders: true,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import 'src/multiple_clients_tests.dart';
import 'src/redirect_tests.dart';
import 'src/request_body_streamed_tests.dart';
import 'src/request_body_tests.dart';
import 'src/request_cookies_test.dart';
import 'src/request_headers_tests.dart';
import 'src/request_methods_tests.dart';
import 'src/response_body_streamed_test.dart';
import 'src/response_body_tests.dart';
import 'src/response_cookies_test.dart';
import 'src/response_headers_tests.dart';
import 'src/response_status_line_tests.dart';
import 'src/server_errors_test.dart';
Expand All @@ -27,10 +29,12 @@ export 'src/multiple_clients_tests.dart' show testMultipleClients;
export 'src/redirect_tests.dart' show testRedirect;
export 'src/request_body_streamed_tests.dart' show testRequestBodyStreamed;
export 'src/request_body_tests.dart' show testRequestBody;
export 'src/request_cookies_test.dart' show testRequestCookies;
export 'src/request_headers_tests.dart' show testRequestHeaders;
export 'src/request_methods_tests.dart' show testRequestMethods;
export 'src/response_body_streamed_test.dart' show testResponseBodyStreamed;
export 'src/response_body_tests.dart' show testResponseBody;
export 'src/response_cookies_test.dart' show testResponseCookies;
export 'src/response_headers_tests.dart' show testResponseHeaders;
export 'src/response_status_line_tests.dart' show testResponseStatusLine;
export 'src/server_errors_test.dart' show testServerErrors;
Expand All @@ -54,6 +58,12 @@ export 'src/server_errors_test.dart' show testServerErrors;
/// If [preservesMethodCase] is `false` then tests that assume that the
/// [Client] preserves custom request method casing will be skipped.
///
/// If [canSendCookieHeaders] is `false` then tests that require that "cookie"
/// headers be sent by the client will not be run.
///
/// If [canReceiveSetCookieHeaders] is `false` then tests that require that
/// "set-cookie" headers be received by the client will not be run.
///
/// The tests are run against a series of HTTP servers that are started by the
/// tests. If the tests are run in the browser, then the test servers are
/// started in another process. Otherwise, the test servers are run in-process.
Expand All @@ -64,6 +74,8 @@ void testAll(
bool redirectAlwaysAllowed = false,
bool canWorkInIsolates = true,
bool preservesMethodCase = false,
bool canSendCookieHeaders = false,
bool canReceiveSetCookieHeaders = false,
}) {
testRequestBody(clientFactory());
testRequestBodyStreamed(clientFactory(),
Expand All @@ -82,4 +94,8 @@ void testAll(
testMultipleClients(clientFactory);
testClose(clientFactory);
testIsolate(clientFactory, canWorkInIsolates: canWorkInIsolates);
testRequestCookies(clientFactory(),
canSendCookieHeaders: canSendCookieHeaders);
testResponseCookies(clientFactory(),
canReceiveSetCookieHeaders: canReceiveSetCookieHeaders);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:stream_channel/stream_channel.dart';

/// Starts an HTTP server that captures "cookie" headers.
///
/// Channel protocol:
/// On Startup:
/// - send port
/// On Request Received:
/// - send a list of header lines starting with "cookie:"
/// When Receive Anything:
/// - exit
void hybridMain(StreamChannel<Object?> channel) async {
late ServerSocket server;

server = (await ServerSocket.bind('localhost', 0))
..listen((Socket socket) async {
final request = utf8.decoder.bind(socket).transform(const LineSplitter());

final cookies = <String>[];
request.listen((line) {
if (line.toLowerCase().startsWith('cookie:')) {
cookies.add(line);
}

if (line.isEmpty) {
// A blank line indicates the end of the headers.
channel.sink.add(cookies);
}
});

socket.writeAll(
[
'HTTP/1.1 200 OK',
'Access-Control-Allow-Origin: *',
'Content-Length: 0',
'\r\n', // Add \r\n at the end of this header section.
],
'\r\n', // Separate each field by \r\n.
);
await socket.close();
});

channel.sink.add(server.port);
await channel
.stream.first; // Any writes indicates that the server should exit.
unawaited(server.close());
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:async/async.dart';
import 'package:http/http.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';

import 'request_cookies_server_vm.dart'
if (dart.library.js_interop) 'request_cookies_server_web.dart';

// The an HTTP header into [name, value].
final headerSplitter = RegExp(':[ \t]+');

/// Tests that the [Client] correctly sends "cookie" headers in the request.
///
/// If [canSendCookieHeaders] is `false` then tests that require that "cookie"
/// headers be sent by the client will not be run.
void testRequestCookies(Client client,
{bool canSendCookieHeaders = false}) async {
group('request cookies', () {
late final String host;
late final StreamChannel<Object?> httpServerChannel;
late final StreamQueue<Object?> httpServerQueue;

setUpAll(() async {
httpServerChannel = await startServer();
httpServerQueue = StreamQueue(httpServerChannel.stream);
host = 'localhost:${await httpServerQueue.nextAsInt}';
});
tearDownAll(() => httpServerChannel.sink.add(null));

test('one cookie', () async {
await client
.get(Uri.http(host, ''), headers: {'cookie': 'SID=298zf09hf012fh2'});

final cookies = (await httpServerQueue.next as List).cast<String>();
expect(cookies, hasLength(1));
final [header, value] = cookies[0].split(headerSplitter);
expect(header.toLowerCase(), 'cookie');
expect(value, 'SID=298zf09hf012fh2');
}, skip: canSendCookieHeaders ? false : 'cannot send cookie headers');

test('multiple cookies semicolon separated', () async {
await client.get(Uri.http(host, ''),
headers: {'cookie': 'SID=298zf09hf012fh2; lang=en-US'});

final cookies = (await httpServerQueue.next as List).cast<String>();
expect(cookies, hasLength(1));
final [header, value] = cookies[0].split(headerSplitter);
expect(header.toLowerCase(), 'cookie');
expect(value, 'SID=298zf09hf012fh2; lang=en-US');
}, skip: canSendCookieHeaders ? false : 'cannot send cookie headers');
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:async/async.dart';
import 'package:stream_channel/stream_channel.dart';

/// Starts an HTTP server that returns a custom status line.
///
/// Channel protocol:
/// On Startup:
/// - send port
/// On Request Received:
/// - load response status line from channel
/// - exit
void hybridMain(StreamChannel<Object?> channel) async {
late HttpServer server;
final clientQueue = StreamQueue(channel.stream);

server = (await HttpServer.bind('localhost', 0))
..listen((request) async {
await request.drain<void>();
final socket = await request.response.detachSocket(writeHeaders: false);

final headers = (await clientQueue.next) as List;
socket.writeAll(
[
'HTTP/1.1 200 OK',
'Access-Control-Allow-Origin: *',
'Content-Length: 0',
...headers,
'\r\n', // Add \r\n at the end of this header section.
],
'\r\n', // Separate each field by \r\n.
);
await socket.close();
unawaited(server.close());
});

channel.sink.add(server.port);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading