|
6 | 6 | import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterClient; |
7 | 7 | import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterGrpcClient; |
8 | 8 | import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterGrpcService; |
| 9 | +import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterService; |
9 | 10 | import io.grpc.stub.StreamObserver; |
| 11 | +import io.vertx.core.Future; |
10 | 12 | import io.vertx.core.Promise; |
11 | 13 | import io.vertx.core.buffer.Buffer; |
12 | 14 | import io.vertx.core.http.*; |
13 | 15 | import io.vertx.core.json.Json; |
14 | 16 | import io.vertx.core.json.JsonArray; |
15 | 17 | import io.vertx.core.json.JsonObject; |
16 | 18 | import io.vertx.core.net.SocketAddress; |
| 19 | +import io.vertx.core.streams.WriteStream; |
17 | 20 | import io.vertx.grpc.client.GrpcClient; |
18 | 21 | import io.vertx.grpc.server.GrpcServer; |
19 | 22 | import io.vertx.grpc.server.GrpcServerResponse; |
@@ -357,6 +360,61 @@ public void testUnaryCollisionWithoutOption() throws TimeoutException { |
357 | 360 | assertEquals("Hello Julien", reply.getMessage()); |
358 | 361 | } |
359 | 362 |
|
| 363 | + @Test |
| 364 | + public void testUnaryAddService() throws TimeoutException { |
| 365 | + HttpClient client = vertx.createHttpClient(); |
| 366 | + |
| 367 | + vertx.createHttpServer() |
| 368 | + .requestHandler(GrpcServer.server(vertx).addService(GreeterGrpcService.of(new GreeterService() { |
| 369 | + @Override |
| 370 | + public Future<HelloReply> sayHello(HelloRequest request) { |
| 371 | + return Future.succeededFuture(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build()); |
| 372 | + } |
| 373 | + }))).listen(8080, "localhost").await(10, TimeUnit.SECONDS); |
| 374 | + |
| 375 | + RequestOptions options = new RequestOptions().setHost("localhost").setPort(8080).setURI("/v1/hello/Julien").setMethod(HttpMethod.GET); |
| 376 | + |
| 377 | + Buffer body = client.request(options).compose(req -> { |
| 378 | + req.putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); |
| 379 | + req.putHeader(HttpHeaders.ACCEPT, "application/json"); |
| 380 | + return req.send(); |
| 381 | + }).expecting(HttpResponseExpectation.SC_OK) |
| 382 | + .expecting(HttpResponseExpectation.JSON) |
| 383 | + .compose(HttpClientResponse::body) |
| 384 | + .await(10, TimeUnit.SECONDS); |
| 385 | + assertEquals("Hello Julien", getMessage(body.toString())); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + public void testServerStreamingAddService() throws TimeoutException { |
| 390 | + HttpClient client = vertx.createHttpClient(); |
| 391 | + |
| 392 | + vertx.createHttpServer() |
| 393 | + .requestHandler(GrpcServer.server(vertx).addService(StreamingTranscodingGreeterGrpcService.of(new StreamingTranscodingGreeterService() { |
| 394 | + @Override |
| 395 | + protected void sayHelloStreaming(StreamingHelloRequest request, WriteStream<StreamingHelloReply> response) { |
| 396 | + response.write(StreamingHelloReply.newBuilder().setMessage("Hello " + request.getName() + " 1").build()); |
| 397 | + response.write(StreamingHelloReply.newBuilder().setMessage("Hello " + request.getName() + " 2").build()); |
| 398 | + response.end(); |
| 399 | + } |
| 400 | + }))).listen(8080, "localhost").await(10, TimeUnit.SECONDS); |
| 401 | + |
| 402 | + RequestOptions options = new RequestOptions().setHost("localhost").setPort(8080).setURI("/v1/hello/stream/Julien").setMethod(HttpMethod.GET); |
| 403 | + |
| 404 | + Buffer body = client.request(options).compose(req -> { |
| 405 | + req.putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); |
| 406 | + req.putHeader(HttpHeaders.ACCEPT, "application/json"); |
| 407 | + return req.send(); |
| 408 | + }).expecting(HttpResponseExpectation.SC_OK) |
| 409 | + .compose(HttpClientResponse::body) |
| 410 | + .await(10, TimeUnit.SECONDS); |
| 411 | + |
| 412 | + JsonArray array = new JsonArray(body); |
| 413 | + assertEquals(2, array.size()); |
| 414 | + assertEquals("Hello Julien 1", array.getJsonObject(0).getString("message")); |
| 415 | + assertEquals("Hello Julien 2", array.getJsonObject(1).getString("message")); |
| 416 | + } |
| 417 | + |
360 | 418 | @Test |
361 | 419 | public void testServerStreaming() throws TimeoutException { |
362 | 420 | HttpClient client = vertx.createHttpClient(); |
|
0 commit comments