Unverified Commit d537834b authored by Aubrey Anderson's avatar Aubrey Anderson Committed by GitHub

Allow headers to be passed to the WebSocket connection for VMServiceFlutterDriver (#54698)

parent 0ece276e
...@@ -127,6 +127,10 @@ abstract class FlutterDriver { ...@@ -127,6 +127,10 @@ abstract class FlutterDriver {
/// `isolateNumber` is set, as this is already enough information to connect /// `isolateNumber` is set, as this is already enough information to connect
/// to an isolate. /// to an isolate.
/// ///
/// `headers` optionally specifies HTTP headers to be included in the
/// [WebSocket] connection. This is only used for [VMServiceFlutterDriver]
/// connections.
///
/// `browser` specifies which FlutterDriver implementation to use. If not /// `browser` specifies which FlutterDriver implementation to use. If not
/// speicifed or set to false, [VMServiceFlutterDriver] implementation /// speicifed or set to false, [VMServiceFlutterDriver] implementation
/// will be used. Otherwise, [WebFlutterDriver] implementation will be used. /// will be used. Otherwise, [WebFlutterDriver] implementation will be used.
...@@ -141,6 +145,7 @@ abstract class FlutterDriver { ...@@ -141,6 +145,7 @@ abstract class FlutterDriver {
int isolateNumber, int isolateNumber,
Pattern fuchsiaModuleTarget, Pattern fuchsiaModuleTarget,
Duration timeout, Duration timeout,
Map<String, dynamic> headers,
}) async { }) async {
if (Platform.environment['FLUTTER_WEB_TEST'] != null) { if (Platform.environment['FLUTTER_WEB_TEST'] != null) {
return WebFlutterDriver.connectWeb(hostUrl: dartVmServiceUrl, timeout: timeout); return WebFlutterDriver.connectWeb(hostUrl: dartVmServiceUrl, timeout: timeout);
...@@ -151,6 +156,7 @@ abstract class FlutterDriver { ...@@ -151,6 +156,7 @@ abstract class FlutterDriver {
logCommunicationToFile: logCommunicationToFile, logCommunicationToFile: logCommunicationToFile,
isolateNumber: isolateNumber, isolateNumber: isolateNumber,
fuchsiaModuleTarget: fuchsiaModuleTarget, fuchsiaModuleTarget: fuchsiaModuleTarget,
headers: headers,
); );
} }
......
...@@ -48,6 +48,7 @@ class VMServiceFlutterDriver extends FlutterDriver { ...@@ -48,6 +48,7 @@ class VMServiceFlutterDriver extends FlutterDriver {
bool logCommunicationToFile = true, bool logCommunicationToFile = true,
int isolateNumber, int isolateNumber,
Pattern fuchsiaModuleTarget, Pattern fuchsiaModuleTarget,
Map<String, dynamic> headers,
}) async { }) async {
// If running on a Fuchsia device, connect to the first isolate whose name // If running on a Fuchsia device, connect to the first isolate whose name
// matches FUCHSIA_MODULE_TARGET. // matches FUCHSIA_MODULE_TARGET.
...@@ -92,7 +93,7 @@ class VMServiceFlutterDriver extends FlutterDriver { ...@@ -92,7 +93,7 @@ class VMServiceFlutterDriver extends FlutterDriver {
// Connect to Dart VM services // Connect to Dart VM services
_log('Connecting to Flutter application at $dartVmServiceUrl'); _log('Connecting to Flutter application at $dartVmServiceUrl');
final VMServiceClientConnection connection = final VMServiceClientConnection connection =
await vmServiceConnectFunction(dartVmServiceUrl); await vmServiceConnectFunction(dartVmServiceUrl, headers: headers);
final VMServiceClient client = connection.client; final VMServiceClient client = connection.client;
final VM vm = await client.getVM(); final VM vm = await client.getVM();
final VMIsolateRef isolateRef = isolateNumber == final VMIsolateRef isolateRef = isolateNumber ==
...@@ -564,15 +565,16 @@ void _checkCloseCode(WebSocket ws) { ...@@ -564,15 +565,16 @@ void _checkCloseCode(WebSocket ws) {
/// Waits for a real Dart VM service to become available, then connects using /// Waits for a real Dart VM service to become available, then connects using
/// the [VMServiceClient]. /// the [VMServiceClient].
Future<VMServiceClientConnection> _waitAndConnect(String url) async { Future<VMServiceClientConnection> _waitAndConnect(
String url, {Map<String, dynamic> headers}) async {
final String webSocketUrl = _getWebSocketUrl(url); final String webSocketUrl = _getWebSocketUrl(url);
int attempts = 0; int attempts = 0;
while (true) { while (true) {
WebSocket ws1; WebSocket ws1;
WebSocket ws2; WebSocket ws2;
try { try {
ws1 = await WebSocket.connect(webSocketUrl); ws1 = await WebSocket.connect(webSocketUrl, headers: headers);
ws2 = await WebSocket.connect(webSocketUrl); ws2 = await WebSocket.connect(webSocketUrl, headers: headers);
ws1.done.whenComplete(() => _checkCloseCode(ws1)); ws1.done.whenComplete(() => _checkCloseCode(ws1));
ws2.done.whenComplete(() => _checkCloseCode(ws2)); ws2.done.whenComplete(() => _checkCloseCode(ws2));
...@@ -650,5 +652,8 @@ class VMServiceClientConnection { ...@@ -650,5 +652,8 @@ class VMServiceClientConnection {
final rpc.Peer peer; final rpc.Peer peer;
} }
/// A function that connects to a Dart VM service given the [url]. /// A function that connects to a Dart VM service
typedef VMServiceConnectFunction = Future<VMServiceClientConnection> Function(String url); /// with [headers] given the [url].
typedef VMServiceConnectFunction =
Future<VMServiceClientConnection> Function(
String url, {Map<String, dynamic> headers});
...@@ -51,7 +51,7 @@ void main() { ...@@ -51,7 +51,7 @@ void main() {
when(mockIsolate.loadRunnable()).thenAnswer((_) => Future<MockIsolate>.value(mockIsolate)); when(mockIsolate.loadRunnable()).thenAnswer((_) => Future<MockIsolate>.value(mockIsolate));
when(mockIsolate.invokeExtension(any, any)).thenAnswer( when(mockIsolate.invokeExtension(any, any)).thenAnswer(
(Invocation invocation) => makeMockResponse(<String, dynamic>{'status': 'ok'})); (Invocation invocation) => makeMockResponse(<String, dynamic>{'status': 'ok'}));
vmServiceConnectFunction = (String url) { vmServiceConnectFunction = (String url, {Map<String, dynamic> headers}) {
return Future<VMServiceClientConnection>.value( return Future<VMServiceClientConnection>.value(
VMServiceClientConnection(mockClient, mockPeer) VMServiceClientConnection(mockClient, mockPeer)
); );
...@@ -116,6 +116,22 @@ void main() { ...@@ -116,6 +116,22 @@ void main() {
expect(driver, isNotNull); expect(driver, isNotNull);
expectLogContains('Isolate is not paused. Assuming application is ready.'); expectLogContains('Isolate is not paused. Assuming application is ready.');
}); });
test('connects with headers', () async {
Map<String, dynamic> actualHeaders;
vmServiceConnectFunction = (String url, {Map<String, dynamic> headers}) {
actualHeaders = headers;
return Future<VMServiceClientConnection>.value(
VMServiceClientConnection(mockClient, mockPeer)
);
};
final Map<String, String> expectedHeaders = <String, String>{'header-key': 'header-value'};
final FlutterDriver driver = await FlutterDriver.connect(
dartVmServiceUrl: '', headers: expectedHeaders);
expect(driver, isNotNull);
expect(actualHeaders, equals(expectedHeaders));
});
}); });
group('VMServiceFlutterDriver', () { group('VMServiceFlutterDriver', () {
......
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