Support executing tests when run as a package (#714)

diff --git a/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart b/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart
index 2e2fa00..c1d5e2d 100644
--- a/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart
+++ b/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart
@@ -7,6 +7,8 @@
 import 'package:stream_channel/stream_channel.dart';
 import 'package:test/test.dart';
 
+import 'utils.dart';
+
 /// Tests that the [Client] correctly implements HTTP redirect logic.
 ///
 /// If [redirectAlwaysAllowed] is `true` then tests that require the [Client]
@@ -18,7 +20,7 @@
     late final StreamQueue<Object?> httpServerQueue;
 
     setUpAll(() async {
-      httpServerChannel = spawnHybridUri('../lib/src/redirect_server.dart');
+      httpServerChannel = await startServer('redirect_server.dart');
       httpServerQueue = StreamQueue(httpServerChannel.stream);
       host = 'localhost:${await httpServerQueue.next}';
     });
diff --git a/pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart b/pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart
index 6547a52..7f27c8b 100644
--- a/pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart
+++ b/pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart
@@ -10,6 +10,8 @@
 import 'package:stream_channel/stream_channel.dart';
 import 'package:test/test.dart';
 
+import 'utils.dart';
+
 /// Tests that the [Client] correctly implements streamed request body
 /// uploading.
 ///
@@ -25,7 +27,7 @@
 
     setUp(() async {
       httpServerChannel =
-          spawnHybridUri('../lib/src/request_body_streamed_server.dart');
+          await startServer('request_body_streamed_server.dart');
       httpServerQueue = StreamQueue(httpServerChannel.stream);
       host = 'localhost:${await httpServerQueue.next}';
     });
diff --git a/pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart b/pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart
index 38d5afd..0843955 100644
--- a/pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart
+++ b/pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart
@@ -9,6 +9,8 @@
 import 'package:stream_channel/stream_channel.dart';
 import 'package:test/test.dart';
 
+import 'utils.dart';
+
 class _Plus2Decoder extends Converter<List<int>, String> {
   @override
   String convert(List<int> input) =>
@@ -42,7 +44,7 @@
     late final StreamQueue<Object?> httpServerQueue;
 
     setUpAll(() async {
-      httpServerChannel = spawnHybridUri('../lib/src/request_body_server.dart');
+      httpServerChannel = await startServer('request_body_server.dart');
       httpServerQueue = StreamQueue(httpServerChannel.stream);
       host = 'localhost:${await httpServerQueue.next}';
     });
diff --git a/pkgs/http_client_conformance_tests/lib/src/response_body_streamed_test.dart b/pkgs/http_client_conformance_tests/lib/src/response_body_streamed_test.dart
index 31506c4..ab861f2 100644
--- a/pkgs/http_client_conformance_tests/lib/src/response_body_streamed_test.dart
+++ b/pkgs/http_client_conformance_tests/lib/src/response_body_streamed_test.dart
@@ -9,6 +9,8 @@
 import 'package:stream_channel/stream_channel.dart';
 import 'package:test/test.dart';
 
+import 'utils.dart';
+
 /// Tests that the [Client] correctly implements HTTP responses with bodies of
 /// unbounded size.
 ///
@@ -24,7 +26,7 @@
 
     setUpAll(() async {
       httpServerChannel =
-          spawnHybridUri('../lib/src/response_body_streamed_server.dart');
+          await startServer('response_body_streamed_server.dart');
       httpServerQueue = StreamQueue(httpServerChannel.stream);
       host = 'localhost:${await httpServerQueue.next}';
     });
diff --git a/pkgs/http_client_conformance_tests/lib/src/response_body_tests.dart b/pkgs/http_client_conformance_tests/lib/src/response_body_tests.dart
index 0555d7d..b5f0f75 100644
--- a/pkgs/http_client_conformance_tests/lib/src/response_body_tests.dart
+++ b/pkgs/http_client_conformance_tests/lib/src/response_body_tests.dart
@@ -7,6 +7,8 @@
 import 'package:stream_channel/stream_channel.dart';
 import 'package:test/test.dart';
 
+import 'utils.dart';
+
 /// Tests that the [Client] correctly implements HTTP responses with bodies.
 ///
 /// If [canStreamResponseBody] is `false` then tests that assume that the
@@ -21,8 +23,7 @@
     const message = 'Hello World!';
 
     setUpAll(() async {
-      httpServerChannel =
-          spawnHybridUri('../lib/src/response_body_server.dart');
+      httpServerChannel = await startServer('response_body_server.dart');
       httpServerQueue = StreamQueue(httpServerChannel.stream);
       host = 'localhost:${await httpServerQueue.next}';
     });
diff --git a/pkgs/http_client_conformance_tests/lib/src/utils.dart b/pkgs/http_client_conformance_tests/lib/src/utils.dart
new file mode 100644
index 0000000..72307e6
--- /dev/null
+++ b/pkgs/http_client_conformance_tests/lib/src/utils.dart
@@ -0,0 +1,26 @@
+import 'dart:isolate';
+
+import 'package:stream_channel/stream_channel.dart';
+import 'package:test/test.dart';
+
+/// Starts a test server using a relative path name e.g.
+/// 'redirect_server.dart'.
+///
+/// See [spawnHybridUri].
+Future<StreamChannel<Object?>> startServer(String fileName) async {
+  try {
+    final fileUri = await Isolate.resolvePackageUri(Uri(
+        scheme: 'package',
+        path: 'http_client_conformance_tests/src/$fileName'));
+    if (fileUri == null) {
+      throw StateError('The package could not be resolved');
+    }
+    return spawnHybridUri(fileUri);
+    // ignore: avoid_catching_errors
+  } on UnsupportedError {
+    // The current runtime environment (probably browser) does not support
+    // `Isolate.resolvePackageUri` so try to use a relative path. This will
+    // *not* work if `http_client_conformance_tests` is used as a package.
+    return spawnHybridUri('../lib/src/$fileName');
+  }
+}