Skip to content

Commit 557d9a3

Browse files
authored
Support executing tests when run as a package (#714)
1 parent 2b4f988 commit 557d9a3

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:http/http.dart';
77
import 'package:stream_channel/stream_channel.dart';
88
import 'package:test/test.dart';
99

10+
import 'utils.dart';
11+
1012
/// Tests that the [Client] correctly implements HTTP redirect logic.
1113
///
1214
/// If [redirectAlwaysAllowed] is `true` then tests that require the [Client]
@@ -18,7 +20,7 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
1820
late final StreamQueue<Object?> httpServerQueue;
1921

2022
setUpAll(() async {
21-
httpServerChannel = spawnHybridUri('../lib/src/redirect_server.dart');
23+
httpServerChannel = await startServer('redirect_server.dart');
2224
httpServerQueue = StreamQueue(httpServerChannel.stream);
2325
host = 'localhost:${await httpServerQueue.next}';
2426
});

pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'package:http/http.dart';
1010
import 'package:stream_channel/stream_channel.dart';
1111
import 'package:test/test.dart';
1212

13+
import 'utils.dart';
14+
1315
/// Tests that the [Client] correctly implements streamed request body
1416
/// uploading.
1517
///
@@ -25,7 +27,7 @@ void testRequestBodyStreamed(Client client,
2527

2628
setUp(() async {
2729
httpServerChannel =
28-
spawnHybridUri('../lib/src/request_body_streamed_server.dart');
30+
await startServer('request_body_streamed_server.dart');
2931
httpServerQueue = StreamQueue(httpServerChannel.stream);
3032
host = 'localhost:${await httpServerQueue.next}';
3133
});

pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'package:http/http.dart';
99
import 'package:stream_channel/stream_channel.dart';
1010
import 'package:test/test.dart';
1111

12+
import 'utils.dart';
13+
1214
class _Plus2Decoder extends Converter<List<int>, String> {
1315
@override
1416
String convert(List<int> input) =>
@@ -42,7 +44,7 @@ void testRequestBody(Client client) {
4244
late final StreamQueue<Object?> httpServerQueue;
4345

4446
setUpAll(() async {
45-
httpServerChannel = spawnHybridUri('../lib/src/request_body_server.dart');
47+
httpServerChannel = await startServer('request_body_server.dart');
4648
httpServerQueue = StreamQueue(httpServerChannel.stream);
4749
host = 'localhost:${await httpServerQueue.next}';
4850
});

pkgs/http_client_conformance_tests/lib/src/response_body_streamed_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'package:http/http.dart';
99
import 'package:stream_channel/stream_channel.dart';
1010
import 'package:test/test.dart';
1111

12+
import 'utils.dart';
13+
1214
/// Tests that the [Client] correctly implements HTTP responses with bodies of
1315
/// unbounded size.
1416
///
@@ -24,7 +26,7 @@ void testResponseBodyStreamed(Client client,
2426

2527
setUpAll(() async {
2628
httpServerChannel =
27-
spawnHybridUri('../lib/src/response_body_streamed_server.dart');
29+
await startServer('response_body_streamed_server.dart');
2830
httpServerQueue = StreamQueue(httpServerChannel.stream);
2931
host = 'localhost:${await httpServerQueue.next}';
3032
});

pkgs/http_client_conformance_tests/lib/src/response_body_tests.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:http/http.dart';
77
import 'package:stream_channel/stream_channel.dart';
88
import 'package:test/test.dart';
99

10+
import 'utils.dart';
11+
1012
/// Tests that the [Client] correctly implements HTTP responses with bodies.
1113
///
1214
/// If [canStreamResponseBody] is `false` then tests that assume that the
@@ -21,8 +23,7 @@ void testResponseBody(Client client,
2123
const message = 'Hello World!';
2224

2325
setUpAll(() async {
24-
httpServerChannel =
25-
spawnHybridUri('../lib/src/response_body_server.dart');
26+
httpServerChannel = await startServer('response_body_server.dart');
2627
httpServerQueue = StreamQueue(httpServerChannel.stream);
2728
host = 'localhost:${await httpServerQueue.next}';
2829
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'dart:isolate';
2+
3+
import 'package:stream_channel/stream_channel.dart';
4+
import 'package:test/test.dart';
5+
6+
/// Starts a test server using a relative path name e.g.
7+
/// 'redirect_server.dart'.
8+
///
9+
/// See [spawnHybridUri].
10+
Future<StreamChannel<Object?>> startServer(String fileName) async {
11+
try {
12+
final fileUri = await Isolate.resolvePackageUri(Uri(
13+
scheme: 'package',
14+
path: 'http_client_conformance_tests/src/$fileName'));
15+
if (fileUri == null) {
16+
throw StateError('The package could not be resolved');
17+
}
18+
return spawnHybridUri(fileUri);
19+
// ignore: avoid_catching_errors
20+
} on UnsupportedError {
21+
// The current runtime environment (probably browser) does not support
22+
// `Isolate.resolvePackageUri` so try to use a relative path. This will
23+
// *not* work if `http_client_conformance_tests` is used as a package.
24+
return spawnHybridUri('../lib/src/$fileName');
25+
}
26+
}

0 commit comments

Comments
 (0)