OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:mirrors'; |
| 7 |
| 8 import 'package:appengine/src/protobuf_api/rpc/rpc_service.dart'; |
| 9 |
| 10 class MockRPCService extends RPCService { |
| 11 String _service; |
| 12 Map<String, Function> _handlers = {}; |
| 13 |
| 14 MockRPCService(this._service); |
| 15 |
| 16 register(String method, Type requestType, Function handler) { |
| 17 if (handler == null) { |
| 18 _handlers.remove(method); |
| 19 } else { |
| 20 _handlers[method] = (List<int> bytes) { |
| 21 var decodedRequest = |
| 22 reflectClass(requestType).newInstance(#fromBuffer, [bytes]); |
| 23 return handler(decodedRequest.reflectee); |
| 24 }; |
| 25 } |
| 26 } |
| 27 |
| 28 Future<List<int>> call(String apiPackage, |
| 29 String method, |
| 30 List<int> requestProtocolBuffer, |
| 31 {String ticket}) { |
| 32 if (_service != apiPackage) { |
| 33 throw "This Mock only works for the $_service service."; |
| 34 } |
| 35 |
| 36 if (_handlers.containsKey(method)) { |
| 37 return _handlers[method](requestProtocolBuffer); |
| 38 } |
| 39 throw "Not mock handler for $apiPackage.$method found"; |
| 40 } |
| 41 } |
OLD | NEW |