Search in sources :

Example 1 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method jwtTokenCreds.

/** Test JWT-based auth. */
public void jwtTokenCreds(InputStream serviceAccountJson) throws Exception {
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseType(PayloadType.COMPRESSABLE).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).setFillUsername(true).build();
    ServiceAccountCredentials credentials = (ServiceAccountCredentials) GoogleCredentials.fromStream(serviceAccountJson);
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    SimpleResponse response = stub.unaryCall(request);
    assertEquals(credentials.getClientEmail(), response.getUsername());
    assertEquals(314159, response.getPayload().getBody().size());
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 2 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method computeEngineCreds.

/** Sends a large unary rpc with compute engine credentials. */
public void computeEngineCreds(String serviceAccount, String oauthScope) throws Exception {
    ComputeEngineCredentials credentials = new ComputeEngineCredentials();
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).setResponseSize(314159).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse response = stub.unaryCall(request);
    assertEquals(serviceAccount, response.getUsername());
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), oauthScope.contains(response.getOauthScope()));
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setOauthScope(response.getOauthScope()).setUsername(response.getUsername()).setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertEquals(goldenResponse, response);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) ComputeEngineCredentials(com.google.auth.oauth2.ComputeEngineCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 3 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method oauth2AuthToken.

/** Sends a unary rpc with raw oauth2 access token credentials. */
public void oauth2AuthToken(String jsonKey, InputStream credentialsStream, String authScope) throws Exception {
    GoogleCredentials utilCredentials = GoogleCredentials.fromStream(credentialsStream);
    utilCredentials = utilCredentials.createScoped(Arrays.<String>asList(authScope));
    AccessToken accessToken = utilCredentials.refreshAccessToken();
    // TODO(madongfly): The Auth library may have something like AccessTokenCredentials in the
    // future, change to the official implementation then.
    OAuth2Credentials credentials = new OAuth2Credentials(accessToken) {

        @Override
        public AccessToken refreshAccessToken() throws IOException {
            throw new IOException("This credential is based on a certain AccessToken, " + "so you can not refresh AccessToken");
        }
    };
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).build();
    final SimpleResponse response = stub.unaryCall(request);
    assertFalse(response.getUsername().isEmpty());
    assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername()));
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.contains(response.getOauthScope()));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 4 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method customMetadata.

@Test(timeout = 10000)
public void customMetadata() throws Exception {
    final int responseSize = 314159;
    final int requestSize = 271828;
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(responseSize).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final StreamingOutputCallRequest streamingRequest = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(responseSize)).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final StreamingOutputCallResponse goldenStreamingResponse = StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final byte[] trailingBytes = { (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb };
    // Test UnaryCall
    Metadata metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel);
    blockingStub = MetadataUtils.attachHeaders(blockingStub, metadata);
    AtomicReference<Metadata> headersCapture = new AtomicReference<Metadata>();
    AtomicReference<Metadata> trailersCapture = new AtomicReference<Metadata>();
    blockingStub = MetadataUtils.captureMetadata(blockingStub, headersCapture, trailersCapture);
    SimpleResponse response = blockingStub.unaryCall(request);
    assertEquals(goldenResponse, response);
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(request), Collections.singleton(goldenResponse));
    }
    // Test FullDuplexCall
    metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
    stub = MetadataUtils.attachHeaders(stub, metadata);
    headersCapture = new AtomicReference<Metadata>();
    trailersCapture = new AtomicReference<Metadata>();
    stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);
    StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder);
    requestStream.onNext(streamingRequest);
    requestStream.onCompleted();
    recorder.awaitCompletion();
    assertSuccess(recorder);
    assertEquals(goldenStreamingResponse, recorder.firstValue().get());
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.OK, Collections.singleton(streamingRequest), Collections.singleton(goldenStreamingResponse));
    }
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 5 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method largeUnary.

@Test(timeout = 10000)
public void largeUnary() throws Exception {
    assumeEnoughMemory();
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(314159).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertEquals(goldenResponse, blockingStub.unaryCall(request));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(request), Collections.singleton(goldenResponse));
    }
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) Test(org.junit.Test)

Aggregations

SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)13 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)9 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)2 StreamObserver (io.grpc.stub.StreamObserver)2 StreamingOutputCallRequest (io.grpc.testing.integration.Messages.StreamingOutputCallRequest)2 StreamingOutputCallResponse (io.grpc.testing.integration.Messages.StreamingOutputCallResponse)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AccessToken (com.google.auth.oauth2.AccessToken)1 ComputeEngineCredentials (com.google.auth.oauth2.ComputeEngineCredentials)1 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 CallOptions (io.grpc.CallOptions)1 Deadline (io.grpc.Deadline)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1