-
Notifications
You must be signed in to change notification settings - Fork 381
Add tests for sending "cookie" and receiving "set-cookie" headers #1113
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
Changes from 6 commits
fc076a8
7d47213
a630c9f
af93745
8976b15
eb01835
a183952
05d1a51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,53 @@ | ||
// 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'; | ||
|
||
/// 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(RegExp(':[ \t]+')); | ||
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(RegExp(':[ \t]+')); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.