-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add response status code test (#1009)
- Loading branch information
1 parent
5ac7cfe
commit cad7d60
Showing
6 changed files
with
107 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
pkgs/http_client_conformance_tests/lib/src/response_status_line_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,42 @@ | ||
// Copyright (c) 2023, 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 statusLine = (await clientQueue.next) as String; | ||
socket.writeAll( | ||
[ | ||
statusLine, | ||
'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(); | ||
unawaited(server.close()); | ||
}); | ||
|
||
channel.sink.add(server.port); | ||
} |
12 changes: 12 additions & 0 deletions
12
pkgs/http_client_conformance_tests/lib/src/response_status_line_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
pkgs/http_client_conformance_tests/lib/src/response_status_line_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
pkgs/http_client_conformance_tests/lib/src/response_status_line_tests.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,39 @@ | ||
// Copyright (c) 2023, 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 'response_status_line_server_vm.dart' | ||
if (dart.library.html) 'response_status_line_server_web.dart'; | ||
|
||
/// Tests that the [Client] correctly processes the response status line. | ||
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 { | ||
httpServerChannel.sink.add('HTTP/1.1 OK'); | ||
await expectLater( | ||
client.get(Uri.http(host, '')), | ||
throwsA(isA<ClientException>()), | ||
); | ||
}, | ||
skip: | ||
'Enable after https://github.com/dart-lang/http/issues/1013 is fixed', | ||
); | ||
}); | ||
} |
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