Skip to content

Commit d537834

Browse files
authored
Allow headers to be passed to the WebSocket connection for VMServiceFlutterDriver (flutter#54698)
1 parent 0ece276 commit d537834

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

packages/flutter_driver/lib/src/driver/driver.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ abstract class FlutterDriver {
127127
/// `isolateNumber` is set, as this is already enough information to connect
128128
/// to an isolate.
129129
///
130+
/// `headers` optionally specifies HTTP headers to be included in the
131+
/// [WebSocket] connection. This is only used for [VMServiceFlutterDriver]
132+
/// connections.
133+
///
130134
/// `browser` specifies which FlutterDriver implementation to use. If not
131135
/// speicifed or set to false, [VMServiceFlutterDriver] implementation
132136
/// will be used. Otherwise, [WebFlutterDriver] implementation will be used.
@@ -141,6 +145,7 @@ abstract class FlutterDriver {
141145
int isolateNumber,
142146
Pattern fuchsiaModuleTarget,
143147
Duration timeout,
148+
Map<String, dynamic> headers,
144149
}) async {
145150
if (Platform.environment['FLUTTER_WEB_TEST'] != null) {
146151
return WebFlutterDriver.connectWeb(hostUrl: dartVmServiceUrl, timeout: timeout);
@@ -151,6 +156,7 @@ abstract class FlutterDriver {
151156
logCommunicationToFile: logCommunicationToFile,
152157
isolateNumber: isolateNumber,
153158
fuchsiaModuleTarget: fuchsiaModuleTarget,
159+
headers: headers,
154160
);
155161
}
156162

packages/flutter_driver/lib/src/driver/vmservice_driver.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class VMServiceFlutterDriver extends FlutterDriver {
4848
bool logCommunicationToFile = true,
4949
int isolateNumber,
5050
Pattern fuchsiaModuleTarget,
51+
Map<String, dynamic> headers,
5152
}) async {
5253
// If running on a Fuchsia device, connect to the first isolate whose name
5354
// matches FUCHSIA_MODULE_TARGET.
@@ -92,7 +93,7 @@ class VMServiceFlutterDriver extends FlutterDriver {
9293
// Connect to Dart VM services
9394
_log('Connecting to Flutter application at $dartVmServiceUrl');
9495
final VMServiceClientConnection connection =
95-
await vmServiceConnectFunction(dartVmServiceUrl);
96+
await vmServiceConnectFunction(dartVmServiceUrl, headers: headers);
9697
final VMServiceClient client = connection.client;
9798
final VM vm = await client.getVM();
9899
final VMIsolateRef isolateRef = isolateNumber ==
@@ -564,15 +565,16 @@ void _checkCloseCode(WebSocket ws) {
564565

565566
/// Waits for a real Dart VM service to become available, then connects using
566567
/// the [VMServiceClient].
567-
Future<VMServiceClientConnection> _waitAndConnect(String url) async {
568+
Future<VMServiceClientConnection> _waitAndConnect(
569+
String url, {Map<String, dynamic> headers}) async {
568570
final String webSocketUrl = _getWebSocketUrl(url);
569571
int attempts = 0;
570572
while (true) {
571573
WebSocket ws1;
572574
WebSocket ws2;
573575
try {
574-
ws1 = await WebSocket.connect(webSocketUrl);
575-
ws2 = await WebSocket.connect(webSocketUrl);
576+
ws1 = await WebSocket.connect(webSocketUrl, headers: headers);
577+
ws2 = await WebSocket.connect(webSocketUrl, headers: headers);
576578

577579
ws1.done.whenComplete(() => _checkCloseCode(ws1));
578580
ws2.done.whenComplete(() => _checkCloseCode(ws2));
@@ -650,5 +652,8 @@ class VMServiceClientConnection {
650652
final rpc.Peer peer;
651653
}
652654

653-
/// A function that connects to a Dart VM service given the [url].
654-
typedef VMServiceConnectFunction = Future<VMServiceClientConnection> Function(String url);
655+
/// A function that connects to a Dart VM service
656+
/// with [headers] given the [url].
657+
typedef VMServiceConnectFunction =
658+
Future<VMServiceClientConnection> Function(
659+
String url, {Map<String, dynamic> headers});

packages/flutter_driver/test/flutter_driver_test.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void main() {
5151
when(mockIsolate.loadRunnable()).thenAnswer((_) => Future<MockIsolate>.value(mockIsolate));
5252
when(mockIsolate.invokeExtension(any, any)).thenAnswer(
5353
(Invocation invocation) => makeMockResponse(<String, dynamic>{'status': 'ok'}));
54-
vmServiceConnectFunction = (String url) {
54+
vmServiceConnectFunction = (String url, {Map<String, dynamic> headers}) {
5555
return Future<VMServiceClientConnection>.value(
5656
VMServiceClientConnection(mockClient, mockPeer)
5757
);
@@ -116,6 +116,22 @@ void main() {
116116
expect(driver, isNotNull);
117117
expectLogContains('Isolate is not paused. Assuming application is ready.');
118118
});
119+
120+
test('connects with headers', () async {
121+
Map<String, dynamic> actualHeaders;
122+
vmServiceConnectFunction = (String url, {Map<String, dynamic> headers}) {
123+
actualHeaders = headers;
124+
return Future<VMServiceClientConnection>.value(
125+
VMServiceClientConnection(mockClient, mockPeer)
126+
);
127+
};
128+
129+
final Map<String, String> expectedHeaders = <String, String>{'header-key': 'header-value'};
130+
final FlutterDriver driver = await FlutterDriver.connect(
131+
dartVmServiceUrl: '', headers: expectedHeaders);
132+
expect(driver, isNotNull);
133+
expect(actualHeaders, equals(expectedHeaders));
134+
});
119135
});
120136

121137
group('VMServiceFlutterDriver', () {

0 commit comments

Comments
 (0)