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 response status code test #1009

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'src/request_headers_tests.dart';
import 'src/response_body_streamed_test.dart';
import 'src/response_body_tests.dart';
import 'src/response_headers_tests.dart';
import 'src/response_status_line_tests.dart';
import 'src/server_errors_test.dart';

export 'src/close_tests.dart' show testClose;
Expand All @@ -29,6 +30,7 @@ export 'src/request_headers_tests.dart' show testRequestHeaders;
export 'src/response_body_streamed_test.dart' show testResponseBodyStreamed;
export 'src/response_body_tests.dart' show testResponseBody;
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;

/// Runs the entire test suite against the given [Client].
Expand Down Expand Up @@ -64,6 +66,7 @@ void testAll(Client Function() clientFactory,
canStreamResponseBody: canStreamResponseBody);
testRequestHeaders(clientFactory());
testResponseHeaders(clientFactory());
testResponseStatusLine(clientFactory());
testRedirect(clientFactory(), redirectAlwaysAllowed: redirectAlwaysAllowed);
testServerErrors(clientFactory());
testCompressedResponseBody(clientFactory());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
alex-james-dev marked this conversation as resolved.
Show resolved Hide resolved
// 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:stream_channel/stream_channel.dart';

/// Starts an HTTP server that returns custom headers.
alex-james-dev marked this conversation as resolved.
Show resolved Hide resolved
///
/// Channel protocol:
/// On Startup:
/// - send port
/// On Request Received:
/// - load response header map from channel
alex-james-dev marked this conversation as resolved.
Show resolved Hide resolved
/// - exit
void hybridMain(StreamChannel<Object?> channel) async {
late HttpServer server;

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

socket.writeAll([
'HTTP/1.1 OK',
'Content-Length: 0',
'', // Add \r\n at the end of this header section.
], '\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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
alex-james-dev marked this conversation as resolved.
Show resolved Hide resolved
// 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 'response_status_line_server_vm.dart'
if (dart.library.html) 'response_status_line_server_web.dart';

/// Tests that the [Client] correctly processes response headers.
void testResponseStatusLine(Client client) async {
group('response status line', () {
late String host;
late StreamChannel<Object?> httpServerChannel;
late StreamQueue<Object?> httpServerQueue;

setUp(() async {
httpServerChannel = await startServer();
httpServerQueue = StreamQueue(httpServerChannel.stream);
host = 'localhost:${await httpServerQueue.next}';
});

test('without status code', () async {
await expectLater(
client.get(Uri.http(host, '')),
throwsA(isA<ClientException>()),
);
});
});
}
1 change: 1 addition & 0 deletions pkgs/java_http/test/java_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void main() {
testIsolate(JavaClient.new);
testResponseBody(JavaClient(), canStreamResponseBody: false);
testResponseHeaders(JavaClient());
testResponseStatusLine(JavaClient());
testRequestBody(JavaClient());
testRequestHeaders(JavaClient());
testMultipleClients(JavaClient.new);
Expand Down
Loading