Chromium Code Reviews| Index: sdk/lib/_internal/pub/test/serve/utils.dart |
| diff --git a/sdk/lib/_internal/pub/test/serve/utils.dart b/sdk/lib/_internal/pub/test/serve/utils.dart |
| index 85e551cffd442dcf2376d0bd21f77c096f080330..0b1219df9060990e7a1379eba5df4b131debe479 100644 |
| --- a/sdk/lib/_internal/pub/test/serve/utils.dart |
| +++ b/sdk/lib/_internal/pub/test/serve/utils.dart |
| @@ -294,44 +294,72 @@ Future _ensureWebSocket() { |
| }); |
| } |
| -/// Sends [request] (an arbitrary JSON object) to the running pub serve's web |
| -/// socket connection, waits for a reply, then verifies that the reply is |
| -/// either equal to [replyEquals] or matches [replyMatches]. |
| +var _webSocketId = 0; |
| + |
| +/// Sends a JSON RPC 2.0 request to the running pub serve's web socket |
| +/// connection, waits for a reply, then verifies the reply. |
| /// |
| -/// Only one of [replyEquals] or [replyMatches] may be provided. |
| +/// This calls a method named [method] with the given [params]. [params] may |
| +/// contain Future, in which case this will wait until they've completed before |
|
Bob Nystrom
2014/03/21 17:42:25
"Future" -> "Futures".
nweiz
2014/03/21 20:27:38
Done.
|
| +/// sending the request. |
| /// |
| -/// [request], [replyEquals], and [replyMatches] may contain futures, in which |
| -/// case this will wait until they've completed before matching. |
| +/// The result of a successful response is validating using [result], which may |
|
Bob Nystrom
2014/03/21 17:42:25
"validating" -> "validated".
nweiz
2014/03/21 20:27:38
Done.
|
| +/// be a matcher or a map containing matchers and futures. This will wait until |
|
Bob Nystrom
2014/03/21 17:42:25
Link to matcher and map.
nweiz
2014/03/21 20:27:38
Done.
|
| +/// any futures are completed before sending the request. |
| /// |
| -/// If [encodeRequest] is `false`, then [request] will be sent as-is over the |
| -/// socket. It omitted, request is JSON encoded to a string first. |
| +/// The result of an error response is validated using [errorCode] and |
| +/// [errorMessage]. Both of these must be provided. The error code is checked |
| +/// against [errorCode] and the error message is checked against [errorMessage]. |
| +/// Either of these may be matchers. |
| /// |
| -/// Returns a [Future] that completes to the call's response. |
| -Future<Map> expectWebSocketCall(request, {Map replyEquals, replyMatches, |
| - bool encodeRequest: true}) { |
| - assert((replyEquals == null) != (replyMatches == null)); |
| +/// Returns a [Future] that completes to the call's result. |
| +Future<Map> expectWebSocketCall(String method, Map params, {result, errorCode, |
| + errorMessage}) { |
| + assert((errorCode == null && errorMessage == null) != (result == null)); |
|
Bob Nystrom
2014/03/21 17:42:25
Given the interdependencies between the disjoint p
nweiz
2014/03/21 20:27:38
Done.
|
| + assert(errorCode == null || errorMessage != null); |
| + |
| + var description = "send $method with $params to web socket and expect "; |
| + if (result != null) { |
| + description += result.toString(); |
| + } else { |
| + description += "error $errorCode"; |
| + } |
| return schedule(() => _ensureWebSocket().then((_) { |
| - var matcherFuture; |
| - if (replyMatches != null) { |
| - matcherFuture = awaitObject(replyMatches); |
| - } else { |
| - matcherFuture = awaitObject(replyEquals).then((reply) => equals(reply)); |
| - } |
| - |
| - return matcherFuture.then((matcher) { |
| - return awaitObject(request).then((completeRequest) { |
| - if (encodeRequest) completeRequest = JSON.encode(completeRequest); |
| - _webSocket.add(completeRequest); |
| - |
| - return _webSocketBroadcastStream.first.then((value) { |
| - value = JSON.decode(value); |
| - expect(value, matcher); |
| - return value; |
| - }); |
| + return Future.wait([ |
| + awaitObject(params), |
| + syncFuture(() => result == null ? null : awaitObject(result)) |
|
Bob Nystrom
2014/03/21 17:42:25
I think you can just do awaitObject(result)
If re
nweiz
2014/03/21 20:27:38
Done.
|
| + ]).then((results) { |
| + var resolvedParams = results[0]; |
| + var resolvedResult = results[1]; |
| + |
| + var id = _webSocketId++; |
| + _webSocket.add(JSON.encode({ |
| + "jsonrpc": "2.0", |
| + "method": method, |
| + "params": resolvedParams, |
| + "id": id |
| + })); |
| + |
| + return _webSocketBroadcastStream.first.then((value) { |
| + value = JSON.decode(value); |
| + currentSchedule.addDebugInfo( |
| + "Web Socket request $method with params $resolvedParams\n" |
| + "Result: $value"); |
|
Bob Nystrom
2014/03/21 17:42:25
Neat.
|
| + |
| + expect(value["id"], equals(id)); |
| + |
| + if (resolvedResult != null) { |
| + expect(value["result"], resolvedResult); |
| + } else { |
| + expect(value["error"]["code"], errorCode); |
| + expect(value["error"]["message"], errorMessage); |
| + } |
| + |
| + return value["result"]; |
| }); |
| }); |
| - }), "send $request to web socket and expect reply $replyEquals"); |
| + }), description); |
| } |
| /// Returns a [Future] that completes to a URL string for the server serving |