-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for sending "cookie" and receiving "set-cookie" headers (#1113
- Loading branch information
1 parent
c2a6d64
commit 5c75da6
Showing
12 changed files
with
331 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
pkgs/http_client_conformance_tests/lib/src/request_cookies_server.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
14 changes: 14 additions & 0 deletions
14
pkgs/http_client_conformance_tests/lib/src/request_cookies_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
pkgs/http_client_conformance_tests/lib/src/request_cookies_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
pkgs/http_client_conformance_tests/lib/src/request_cookies_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |
44 changes: 44 additions & 0 deletions
44
pkgs/http_client_conformance_tests/lib/src/response_cookies_server.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
14 changes: 14 additions & 0 deletions
14
pkgs/http_client_conformance_tests/lib/src/response_cookies_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
pkgs/http_client_conformance_tests/lib/src/response_cookies_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.