Search in sources :

Example 1 with GreeterImplBase

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase in project grpc-java by grpc.

the class HeaderClientInterceptorTest method clientHeaderDeliveredToServer.

@Test
public void clientHeaderDeliveredToServer() throws Exception {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    // Create a server, add service, start, and register for automatic graceful shutdown.
    grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor().addService(ServerInterceptors.intercept(new GreeterImplBase() {
    }, mockServerInterceptor)).build().start());
    // Create a client channel and register for automatic graceful shutdown.
    ManagedChannel channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
    GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(ClientInterceptors.intercept(channel, new HeaderClientInterceptor()));
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    try {
        blockingStub.sayHello(HelloRequest.getDefaultInstance());
        fail();
    } catch (StatusRuntimeException expected) {
    // expected because the method is not implemented at server side
    }
    verify(mockServerInterceptor).interceptCall(ArgumentMatchers.<ServerCall<HelloRequest, HelloReply>>any(), metadataCaptor.capture(), ArgumentMatchers.<ServerCallHandler<HelloRequest, HelloReply>>any());
    assertEquals("customRequestValue", metadataCaptor.getValue().get(HeaderClientInterceptor.CUSTOM_HEADER_KEY));
}
Also used : GreeterImplBase(io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase) GreeterBlockingStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ManagedChannel(io.grpc.ManagedChannel) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 2 with GreeterImplBase

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase in project grpc-java by grpc.

the class HeaderServerInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    GreeterImplBase greeterImplBase = new GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            responseObserver.onNext(HelloReply.getDefaultInstance());
            responseObserver.onCompleted();
        }
    };
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    // Create a server, add service, start, and register for automatic graceful shutdown.
    grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor().addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor())).build().start());
    // Create a client channel and register for automatic graceful shutdown.
    channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
}
Also used : GreeterImplBase(io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase) StreamObserver(io.grpc.stub.StreamObserver) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Before(org.junit.Before)

Aggregations

GreeterImplBase (io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase)2 HelloRequest (io.grpc.examples.helloworld.HelloRequest)2 ManagedChannel (io.grpc.ManagedChannel)1 Metadata (io.grpc.Metadata)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 GreeterBlockingStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub)1 HelloReply (io.grpc.examples.helloworld.HelloReply)1 StreamObserver (io.grpc.stub.StreamObserver)1 Before (org.junit.Before)1 Test (org.junit.Test)1