blob: 1684b3705c9117513f5b6d3ff674a5afe51c025e [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';
import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
/// A controller used to test a [json_rpc.Client].
class ClientController {
/// The controller for the client's response stream.
final _responseController = StreamController<String>();
/// The controller for the client's request sink.
final _requestController = StreamController<String>();
/// The client.
json_rpc.Client get client => _client;
json_rpc.Client _client;
ClientController() {
_client = json_rpc.Client(
StreamChannel(, _requestController.sink));
/// Expects that the client will send a request.
/// The request is passed to [callback], which can return a response. If it
/// returns a String, that's sent as the response directly. If it returns
/// null, no response is sent. Otherwise, the return value is encoded and sent
/// as the response.
void expectRequest(Function(dynamic) callback) {
expect( {
return callback(jsonDecode(request));
}).then((response) {
if (response == null) return;
if (response is! String) response = jsonEncode(response);
/// Sends [response], a decoded response, to [client].
void sendResponse(response) {
/// Sends [response], a JSON-encoded response, to [client].
void sendJsonResponse(String request) {