Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
99a09be2
Unverified
Commit
99a09be2
authored
Jan 21, 2022
by
Yegor
Committed by
GitHub
Jan 21, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[web] validate WebDriver responses (#96884)
Validate WebDriver responses
parent
5775100d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
183 additions
and
9 deletions
+183
-9
web_driver.dart
packages/flutter_driver/lib/src/driver/web_driver.dart
+42
-7
flutter_driver_test.dart
...utter_driver/test/src/real_tests/flutter_driver_test.dart
+2
-2
web_driver_test.dart
...es/flutter_driver/test/src/web_tests/web_driver_test.dart
+139
-0
No files found.
packages/flutter_driver/lib/src/driver/web_driver.dart
View file @
99a09be2
...
...
@@ -105,25 +105,60 @@ class WebFlutterDriver extends FlutterDriver {
);
}
static
DriverError
_createMalformedExtensionResponseError
(
Object
?
data
)
{
throw
DriverError
(
'Received malformed response from the FlutterDriver extension.
\n
'
'Expected a JSON map containing a "response" field and, optionally, an '
'"isError" field, but got
${data.runtimeType}
:
$data
'
);
}
@override
Future
<
Map
<
String
,
dynamic
>>
sendCommand
(
Command
command
)
async
{
Map
<
String
,
dynamic
>
response
;
final
Map
<
String
,
dynamic
>
response
;
final
Object
?
data
;
final
Map
<
String
,
String
>
serialized
=
command
.
serialize
();
_logCommunication
(
'>>>
$serialized
'
);
try
{
final
dynamic
data
=
await
_connection
.
sendCommand
(
"window.
\
$flutterDriver
('
${jsonEncode(serialized)}
')"
,
command
.
timeout
);
response
=
data
!=
null
?
(
json
.
decode
(
data
as
String
)
as
Map
<
String
,
dynamic
>?)!
:
<
String
,
dynamic
>{};
data
=
await
_connection
.
sendCommand
(
"window.
\
$flutterDriver
('
${jsonEncode(serialized)}
')"
,
command
.
timeout
);
// The returned data is expected to be a string. If it's null or anything
// other than a string, something's wrong.
if
(
data
is
!
String
)
{
throw
_createMalformedExtensionResponseError
(
data
);
}
final
Object
?
decoded
=
json
.
decode
(
data
);
if
(
decoded
is
!
Map
<
String
,
dynamic
>)
{
throw
_createMalformedExtensionResponseError
(
data
);
}
else
{
response
=
decoded
;
}
_logCommunication
(
'<<<
$response
'
);
}
on
DriverError
catch
(
_
)
{
rethrow
;
}
catch
(
error
,
stackTrace
)
{
throw
DriverError
(
"Failed to respond to
$command
due to remote error
\n
:
\
$flutterDriver
('
${jsonEncode(serialized)}
')"
,
'FlutterDriver command
${command.runtimeType}
failed due to a remote error.
\n
'
'Command sent:
${jsonEncode(serialized)}
'
,
error
,
stackTrace
);
}
if
(
response
[
'isError'
]
==
true
)
throw
DriverError
(
'Error in Flutter application:
${response['response']}
'
);
return
response
[
'response'
]
as
Map
<
String
,
dynamic
>;
final
Object
?
isError
=
response
[
'isError'
];
final
Object
?
responseData
=
response
[
'response'
];
if
(
isError
is
!
bool
?)
{
throw
_createMalformedExtensionResponseError
(
data
);
}
else
if
(
isError
==
true
)
{
throw
DriverError
(
'Error in Flutter application:
$responseData
'
);
}
if
(
responseData
is
!
Map
<
String
,
dynamic
>)
{
throw
_createMalformedExtensionResponseError
(
data
);
}
return
responseData
;
}
@override
...
...
packages/flutter_driver/test/src/real_tests/flutter_driver_test.dart
View file @
99a09be2
...
...
@@ -745,8 +745,8 @@ void main() {
const
String
waitForCommandLog
=
'>>> {command: waitFor, timeout: 1234, finderType: ByTooltipMessage, text: logCommunicationToFile test}'
;
const
String
responseLog
=
'<<< {isError: false, response: {status: ok}, type: Response}'
;
expect
(
commandLog
.
contains
(
waitForCommandLog
),
true
,
reason:
'
$commandLog
not contains
$waitForCommandLog
'
);
expect
(
commandLog
.
contains
(
responseLog
),
true
,
reason:
'
$commandLog
not contains
$responseLog
'
);
expect
(
commandLog
,
contains
(
waitForCommandLog
)
,
reason:
'
$commandLog
not contains
$waitForCommandLog
'
);
expect
(
commandLog
,
contains
(
responseLog
)
,
reason:
'
$commandLog
not contains
$responseLog
'
);
});
test
(
'logCommunicationToFile = false'
,
()
async
{
...
...
packages/flutter_driver/test/src/web_tests/web_driver_test.dart
0 → 100644
View file @
99a09be2
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter_driver/src/common/error.dart'
;
import
'package:flutter_driver/src/common/health.dart'
;
import
'package:flutter_driver/src/driver/web_driver.dart'
;
import
'package:webdriver/src/common/log.dart'
;
import
'../../common.dart'
;
void
main
(
)
{
group
(
'WebDriver'
,
()
{
late
FakeFlutterWebConnection
fakeConnection
;
late
WebFlutterDriver
driver
;
setUp
(()
{
fakeConnection
=
FakeFlutterWebConnection
();
driver
=
WebFlutterDriver
.
connectedTo
(
fakeConnection
);
});
test
(
'sendCommand succeeds'
,
()
async
{
fakeConnection
.
fakeResponse
=
'''
{
"isError": false,
"response": {
"test": "hello"
}
}
'''
;
final
Map
<
String
,
Object
?>
response
=
await
driver
.
sendCommand
(
const
GetHealth
());
expect
(
response
[
'test'
],
'hello'
);
});
test
(
'sendCommand fails on communication error'
,
()
async
{
fakeConnection
.
communicationError
=
Error
();
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithMessage
(
'FlutterDriver command GetHealth failed due to a remote error.
\n
'
'Command sent: {"command":"get_health"}'
),
);
});
test
(
'sendCommand fails on null'
,
()
async
{
fakeConnection
.
fakeResponse
=
null
;
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithDataString
(
'Null'
,
'null'
),
);
});
test
(
'sendCommand fails when response data is not a string'
,
()
async
{
fakeConnection
.
fakeResponse
=
1234
;
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithDataString
(
'int'
,
'1234'
),
);
});
test
(
'sendCommand fails when isError is true'
,
()
async
{
fakeConnection
.
fakeResponse
=
'''
{
"isError": true,
"response": "test error message"
}
'''
;
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithMessage
(
'Error in Flutter application: test error message'
),
);
});
test
(
'sendCommand fails when isError is not bool'
,
()
async
{
fakeConnection
.
fakeResponse
=
'{ "isError": 5 }'
;
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithDataString
(
'String'
,
'{ "isError": 5 }'
),
);
});
test
(
'sendCommand fails when "response" field is not a JSON map'
,
()
async
{
fakeConnection
.
fakeResponse
=
'{ "response": 5 }'
;
expect
(
()
=>
driver
.
sendCommand
(
const
GetHealth
()),
_throwsDriverErrorWithDataString
(
'String'
,
'{ "response": 5 }'
),
);
});
});
}
Matcher
_throwsDriverErrorWithMessage
(
String
expectedMessage
)
{
return
throwsA
(
allOf
(
isA
<
DriverError
>(),
predicate
<
DriverError
>((
DriverError
error
)
{
final
String
actualMessage
=
error
.
message
;
return
actualMessage
==
expectedMessage
;
},
'contains message:
$expectedMessage
'
),
));
}
Matcher
_throwsDriverErrorWithDataString
(
String
dataType
,
String
dataString
)
{
return
_throwsDriverErrorWithMessage
(
'Received malformed response from the FlutterDriver extension.
\n
'
'Expected a JSON map containing a "response" field and, optionally, an '
'"isError" field, but got
$dataType
:
$dataString
'
);
}
class
FakeFlutterWebConnection
implements
FlutterWebConnection
{
@override
bool
supportsTimelineAction
=
false
;
@override
Future
<
void
>
close
()
async
{}
@override
Stream
<
LogEntry
>
get
logs
=>
throw
UnimplementedError
();
@override
Future
<
List
<
int
>>
screenshot
()
{
throw
UnimplementedError
();
}
Object
?
fakeResponse
;
Error
?
communicationError
;
@override
Future
<
Object
?>
sendCommand
(
String
script
,
Duration
?
duration
)
async
{
if
(
communicationError
!=
null
)
{
throw
communicationError
!;
}
return
fakeResponse
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment