Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: test/service_test.dart

Issue 1196293003: Initial support for generating client and server stubs for Dart. (Closed) Base URL: https://github.com/dart-lang/dart-protoc-plugin.git@master
Patch Set: adddressing nits Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/service_generator_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/service_test.dart
diff --git a/test/service_test.dart b/test/service_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ce83eed8c4c5e37309d79ee84d45afcb88b44c6e
--- /dev/null
+++ b/test/service_test.dart
@@ -0,0 +1,64 @@
+library service_test;
+
+import 'dart:async' show Future;
+
+import 'package:protobuf/protobuf.dart';
+import 'package:unittest/unittest.dart';
+
+import '../out/protos/service.pb.dart' as pb;
+
+class SearchService extends pb.SearchServiceBase {
+ Future<pb.SearchResponse> search(ServerContext ctx, pb.SearchRequest request) async {
Søren Gjesse 2015/06/25 13:04:49 Long line.
wibling 2015/06/25 13:10:04 Done.
+ var out = new pb.SearchResponse();
+ if (request.query == 'hello' || request.query == 'world') {
+ out.result.add('hello, world!');
+ }
+ return out;
+ }
+}
+
+class FakeJsonServer {
+ final GeneratedService searchService;
+ FakeJsonServer(this.searchService);
+
+ Future<String> messageHandler(
+ String serviceName, String methodName, String requestJson) async {
+ if (serviceName == 'SearchService') {
+ GeneratedMessage request = searchService.createRequest(methodName);
+ request.mergeFromJson(requestJson);
+ var ctx = new ServerContext();
+ var reply = await searchService.handleCall(ctx, methodName, request);
+ return reply.writeToJson();
+ } else {
+ throw 'unknown service: $serviceName';
+ }
+ }
+}
+
+class FakeJsonClient implements RpcClient {
+ final FakeJsonServer server;
+ FakeJsonClient(this.server);
+
+ Future<GeneratedMessage> invoke(
+ ClientContext ctx, String serviceName, String methodName,
+ GeneratedMessage request, GeneratedMessage response) async {
+
+ String requestJson = request.writeToJson();
+ String replyJson =
+ await server.messageHandler(serviceName, methodName, requestJson);
+ response.mergeFromJson(replyJson);
+ return response;
+ }
+}
+
+void main() {
+ var server = new FakeJsonServer(new SearchService());
+ var api = new pb.SearchServiceApi(new FakeJsonClient(server));
+
+ test('end to end RPC using JSON', () async {
+ var request = new pb.SearchRequest()
+ ..query = "hello";
+ var reply = await api.search(new ClientContext(), request);
+ expect(reply.result, ["hello, world!"]);
+ });
+}
« no previous file with comments | « test/service_generator_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698