Skip to content

Commit 8f08bda

Browse files
committed
Register the mount point paths of the methods of a service
Motivation: - `GrpcServer#addService` only registered the canonical `/package.Service/Method` path, so the HTTP rules of a transcoded service method were ignored and the transcoded routes returned a 500. `GrpcServer#callHandler` already mounts these paths. Changes: - Mount the `MountPoint` paths of a service method in `addService`, like `callHandler` does. - Add integration tests binding a transcoded unary and server-streaming service with `addService`. Signed-off-by: Daniel Fiala <danfiala23@gmail.com>
1 parent 4b58154 commit 8f08bda

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

vertx-grpc-it/src/test/java/io/vertx/grpc/it/tests/TranscodingTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterClient;
77
import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterGrpcClient;
88
import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterGrpcService;
9+
import io.grpc.examples.streamingtranscoding.StreamingTranscodingGreeterService;
910
import io.grpc.stub.StreamObserver;
11+
import io.vertx.core.Future;
1012
import io.vertx.core.Promise;
1113
import io.vertx.core.buffer.Buffer;
1214
import io.vertx.core.http.*;
1315
import io.vertx.core.json.Json;
1416
import io.vertx.core.json.JsonArray;
1517
import io.vertx.core.json.JsonObject;
1618
import io.vertx.core.net.SocketAddress;
19+
import io.vertx.core.streams.WriteStream;
1720
import io.vertx.grpc.client.GrpcClient;
1821
import io.vertx.grpc.server.GrpcServer;
1922
import io.vertx.grpc.server.GrpcServerResponse;
@@ -357,6 +360,61 @@ public void testUnaryCollisionWithoutOption() throws TimeoutException {
357360
assertEquals("Hello Julien", reply.getMessage());
358361
}
359362

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+
360418
@Test
361419
public void testServerStreaming() throws TimeoutException {
362420
HttpClient client = vertx.createHttpClient();

vertx-grpc-server/src/main/java/io/vertx/grpc/server/impl/GrpcServerImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,14 @@ public GrpcServer addService(Service service) {
299299
}
300300
for (ServiceMethod method : service.methods()) {
301301
ServiceMethodInvoker invoker = service.invoker(method);
302-
registerMethodCallHandler(service.pathOfMethod(method.methodName()), new MethodCallHandler<Object, Object>(method, method.decoder(), method.encoder(), invoker));
302+
MethodCallHandler<Object, Object> mch = new MethodCallHandler<>(method, method.decoder(), method.encoder(), invoker);
303+
if (method instanceof MountPoint) {
304+
MountPoint<Object, Object> mountPoint = (MountPoint<Object, Object>) method;
305+
for (String path : mountPoint.paths()) {
306+
registerMethodCallHandler(path, mch);
307+
}
308+
}
309+
registerMethodCallHandler(service.pathOfMethod(method.methodName()), mch);
303310
}
304311

305312
this.services.add(service);

0 commit comments

Comments
 (0)