Skip to content

Commit cad7d60

Browse files
Add response status code test (dart-lang#1009)
1 parent 5ac7cfe commit cad7d60

6 files changed

+107
-0
lines changed

pkgs/http_client_conformance_tests/lib/http_client_conformance_tests.dart

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'src/request_headers_tests.dart';
1515
import 'src/response_body_streamed_test.dart';
1616
import 'src/response_body_tests.dart';
1717
import 'src/response_headers_tests.dart';
18+
import 'src/response_status_line_tests.dart';
1819
import 'src/server_errors_test.dart';
1920

2021
export 'src/close_tests.dart' show testClose;
@@ -29,6 +30,7 @@ export 'src/request_headers_tests.dart' show testRequestHeaders;
2930
export 'src/response_body_streamed_test.dart' show testResponseBodyStreamed;
3031
export 'src/response_body_tests.dart' show testResponseBody;
3132
export 'src/response_headers_tests.dart' show testResponseHeaders;
33+
export 'src/response_status_line_tests.dart' show testResponseStatusLine;
3234
export 'src/server_errors_test.dart' show testServerErrors;
3335

3436
/// Runs the entire test suite against the given [Client].
@@ -64,6 +66,7 @@ void testAll(Client Function() clientFactory,
6466
canStreamResponseBody: canStreamResponseBody);
6567
testRequestHeaders(clientFactory());
6668
testResponseHeaders(clientFactory());
69+
testResponseStatusLine(clientFactory());
6770
testRedirect(clientFactory(), redirectAlwaysAllowed: redirectAlwaysAllowed);
6871
testServerErrors(clientFactory());
6972
testCompressedResponseBody(clientFactory());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
8+
import 'package:async/async.dart';
9+
import 'package:stream_channel/stream_channel.dart';
10+
11+
/// Starts an HTTP server that returns a custom status line.
12+
///
13+
/// Channel protocol:
14+
/// On Startup:
15+
/// - send port
16+
/// On Request Received:
17+
/// - load response status line from channel
18+
/// - exit
19+
void hybridMain(StreamChannel<Object?> channel) async {
20+
late HttpServer server;
21+
final clientQueue = StreamQueue(channel.stream);
22+
23+
server = (await HttpServer.bind('localhost', 0))
24+
..listen((request) async {
25+
await request.drain<void>();
26+
final socket = await request.response.detachSocket(writeHeaders: false);
27+
28+
final statusLine = (await clientQueue.next) as String;
29+
socket.writeAll(
30+
[
31+
statusLine,
32+
'Content-Length: 0',
33+
'\r\n', // Add \r\n at the end of this header section.
34+
],
35+
'\r\n', // Separate each field by \r\n.
36+
);
37+
await socket.close();
38+
unawaited(server.close());
39+
});
40+
41+
channel.sink.add(server.port);
42+
}

pkgs/http_client_conformance_tests/lib/src/response_status_line_server_vm.dart

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/http_client_conformance_tests/lib/src/response_status_line_server_web.dart

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:async/async.dart';
6+
import 'package:http/http.dart';
7+
import 'package:stream_channel/stream_channel.dart';
8+
import 'package:test/test.dart';
9+
10+
import 'response_status_line_server_vm.dart'
11+
if (dart.library.html) 'response_status_line_server_web.dart';
12+
13+
/// Tests that the [Client] correctly processes the response status line.
14+
void testResponseStatusLine(Client client) async {
15+
group('response status line', () {
16+
late String host;
17+
late StreamChannel<Object?> httpServerChannel;
18+
late StreamQueue<Object?> httpServerQueue;
19+
20+
setUp(() async {
21+
httpServerChannel = await startServer();
22+
httpServerQueue = StreamQueue(httpServerChannel.stream);
23+
host = 'localhost:${await httpServerQueue.next}';
24+
});
25+
26+
test(
27+
'without status code',
28+
() async {
29+
httpServerChannel.sink.add('HTTP/1.1 OK');
30+
await expectLater(
31+
client.get(Uri.http(host, '')),
32+
throwsA(isA<ClientException>()),
33+
);
34+
},
35+
skip:
36+
'Enable after https://github.com/dart-lang/http/issues/1013 is fixed',
37+
);
38+
});
39+
}

pkgs/java_http/test/java_client_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void main() {
1212
testResponseBody(JavaClient());
1313
testResponseBodyStreamed(JavaClient());
1414
testResponseHeaders(JavaClient());
15+
testResponseStatusLine(JavaClient());
1516
testRequestBody(JavaClient());
1617
testRequestHeaders(JavaClient());
1718
testMultipleClients(JavaClient.new);

0 commit comments

Comments
 (0)