Search in sources :

Example 1 with GreeterBlockingStub

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub 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 GreeterBlockingStub

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

the class HeaderServerInterceptorTest method serverHeaderDeliveredToClient.

@Test
public void serverHeaderDeliveredToClient() {
    class SpyingClientInterceptor implements ClientInterceptor {

        ClientCall.Listener<?> spyListener;

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

                @Override
                public void start(Listener<RespT> responseListener, Metadata headers) {
                    spyListener = responseListener = mock(ClientCall.Listener.class, delegatesTo(responseListener));
                    super.start(responseListener, headers);
                }
            };
        }
    }
    SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
    GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel).withInterceptors(clientInterceptor);
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    blockingStub.sayHello(HelloRequest.getDefaultInstance());
    assertNotNull(clientInterceptor.spyListener);
    verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
    assertEquals("customRespondValue", metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
Also used : GreeterBlockingStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)2 GreeterBlockingStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub)2 Test (org.junit.Test)2 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientCall (io.grpc.ClientCall)1 ClientInterceptor (io.grpc.ClientInterceptor)1 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)1 ManagedChannel (io.grpc.ManagedChannel)1 MethodDescriptor (io.grpc.MethodDescriptor)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 GreeterImplBase (io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase)1 HelloReply (io.grpc.examples.helloworld.HelloReply)1 HelloRequest (io.grpc.examples.helloworld.HelloRequest)1