Unverified Commit e870f5d1 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] use Url path.Context for joining URI (#79566)

parent 66af44c9
......@@ -7,9 +7,13 @@ import 'dart:math' as math;
import 'package:intl/intl.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as path; // flutter_ignore: package_path_import
import '../convert.dart';
/// A path jointer for URL paths.
final path.Context urlContext = path.url;
/// Convert `foo_bar` to `fooBar`.
String camelCase(String str) {
int index = str.indexOf('_');
......
......@@ -7,13 +7,14 @@
import 'dart:async';
import 'package:file/file.dart';
import 'package:meta/meta.dart' show required;
import 'package:meta/meta.dart' show required, visibleForTesting;
import 'package:vm_service/vm_service.dart' as vm_service;
import 'base/common.dart';
import 'base/context.dart';
import 'base/io.dart' as io;
import 'base/logger.dart';
import 'base/utils.dart';
import 'build_info.dart';
import 'convert.dart';
import 'device.dart';
......@@ -39,6 +40,14 @@ typedef PrintStructuredErrorLogMethod = void Function(vm_service.Event);
WebSocketConnector _openChannel = _defaultOpenChannel;
/// A testing only override of the WebSocket connector.
///
/// Provide a `null` value to restore the original connector.
@visibleForTesting
set openChannelForTesting(WebSocketConnector connector) {
_openChannel = connector ?? _defaultOpenChannel;
}
/// The error codes for the JSON-RPC standard, including VM service specific
/// error codes.
///
......@@ -322,7 +331,7 @@ Future<FlutterVmService> _connect(
io.CompressionOptions compression = io.CompressionOptions.compressionDefault,
Device device,
}) async {
final Uri wsUri = httpUri.replace(scheme: 'ws', path: globals.fs.path.join(httpUri.path, 'ws'));
final Uri wsUri = httpUri.replace(scheme: 'ws', path: urlContext.join(httpUri.path, 'ws'));
final io.WebSocket channel = await _openChannel(wsUri.toString(), compression: compression);
final vm_service.VmService delegateService = vm_service.VmService(
channel,
......
......@@ -643,6 +643,19 @@ void main() {
expect(processVmServiceMessage(event), 'Hello There');
});
testUsingContext('WebSocket URL construction uses correct URI join primitives', () async {
final Completer<String> completer = Completer<String>();
openChannelForTesting = (String url, {io.CompressionOptions compression}) async {
completer.complete(url);
throw Exception('');
};
// Construct a URL that does not end in a `/`.
await expectLater(() => connectToVmService(Uri.parse('http://localhost:8181/foo')), throwsException);
expect(await completer.future, 'ws://localhost:8181/foo/ws');
openChannelForTesting = null;
});
}
class MockVMService extends Fake implements vm_service.VmService {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment