public void InvokeMethodAsync_CanInvokeMethodWithNoReturnTypeAndData() { Request request = new Request() { RequestParameter = "Hello " }; var client = new MockClient(); var data = new Response() { Name = "Look, I was invoked!" }; var invokeResponse = new InvokeResponse { Data = TypeConverters.ToAny(data) }; var response = client.Call <InvokeResponse>() .SetResponse(invokeResponse) .Build(); // Setup the mock client to throw an Rpc Exception with the expected details info client.Mock .Setup(m => m.InvokeServiceAsync(It.IsAny <Autogen.Grpc.v1.InvokeServiceRequest>(), It.IsAny <CallOptions>())) .Returns(response); FluentActions.Awaiting(async() => await client.DaprClient.InvokeMethodAsync <Request>("test", "test", request)).Should().NotThrow(); }
public async Task InvokeMethodAsync_CanInvokeMethodWithReturnTypeNoData_ThrowsExceptionNonSuccess() { var client = new MockClient(); var data = new Response() { Name = "Look, I was invoked!" }; var invokeResponse = new InvokeResponse { Data = TypeConverters.ToAny(data) }; var response = client.Call <InvokeResponse>() .SetResponse(invokeResponse) .Build(); const string rpcExceptionMessage = "RPC exception"; const StatusCode rpcStatusCode = StatusCode.Unavailable; const string rpcStatusDetail = "Non success"; var rpcStatus = new Status(rpcStatusCode, rpcStatusDetail); var rpcException = new RpcException(rpcStatus, new Metadata(), rpcExceptionMessage); // Setup the mock client to throw an Rpc Exception with the expected details info client.Mock .Setup(m => m.InvokeServiceAsync(It.IsAny <Autogen.Grpc.v1.InvokeServiceRequest>(), It.IsAny <CallOptions>())) .Throws(rpcException); await FluentActions.Awaiting(async() => await client.DaprClient.InvokeMethodAsync <Response>("test", "test")).Should().ThrowAsync <RpcException>(); }
public void AnyConversion_GRPC_Pack_Unpack() { var testRun = new TestRun(); testRun.Tests.Add(new TestCase() { Name = "test1" }); testRun.Tests.Add(new TestCase() { Name = "test2" }); testRun.Tests.Add(new TestCase() { Name = "test3" }); var any = TypeConverters.ToAny(testRun); var type = TypeConverters.FromAny <TestRun>(any); type.Should().BeEquivalentTo(testRun); any.TypeUrl.Should().Be("type.googleapis.com/TestRun"); type.Tests.Count.Should().Be(3); type.Tests[0].Name.Should().Be("test1"); type.Tests[1].Name.Should().Be("test2"); type.Tests[2].Name.Should().Be("test3"); }
private async void SendResponse <T>(T data, TestHttpClient.Entry entry, JsonSerializerOptions options = null) { var dataAny = TypeConverters.ToAny(data, options); var dataResponse = new InvokeResponse(); dataResponse.Data = dataAny; var streamContent = await GrpcUtils.CreateResponseContent(dataResponse); var response = GrpcUtils.CreateResponse(HttpStatusCode.OK, streamContent); entry.Completion.SetResult(response); }
public override Task <InvokeResponse> OnInvoke(InvokeRequest request, ServerCallContext context) { return(request.Method switch { "SayHello" => SayHello(request), "TestRun" => TestRun(request), _ => Task.FromResult(new InvokeResponse() { Data = TypeConverters.ToAny(new Response() { Name = $"unexpected" }, this.jsonOptions) }) });
public void AnyConversion_JSON_Serialization_Deserialization() { var response = new Response() { Name = "test" }; var any = TypeConverters.ToAny(response); var type = TypeConverters.FromAny <Response>(any); type.Should().BeEquivalentTo(response); any.TypeUrl.Should().Be("Dapr.Client.Test.TypeConvertersTest+Response"); type.Name.Should().Be("test"); }
public async Task InvokeMethodAsync_CanInvokeMethodWithResponse_CalleeSideHttp() { var client = new MockClient(); var data = new Response() { Name = "Look, I was invoked!" }; var invokeResponse = new InvokeResponse { Data = TypeConverters.ToAny(data) }; var response = client.Call <InvokeResponse>() .SetResponse(invokeResponse) .AddHeader("dapr-http-status", "200") .Build(); client.Mock .Setup(m => m.InvokeServiceAsync(It.IsAny <Autogen.Grpc.v1.InvokeServiceRequest>(), It.IsAny <CallOptions>())) .Returns(response); var body = new Request() { RequestParameter = "Hello " }; var task = client.DaprClient.InvokeMethodWithResponseAsync <Request, Response>("test", "testMethod", body); // Validate Response var invokedResponse = await task; invokedResponse.Body.Name.Should().Be("Look, I was invoked!"); invokedResponse.Headers.ContainsKey("dapr-http-status").Should().BeTrue(); invokedResponse.ContentType.Should().Be(Constants.ContentTypeApplicationJson); invokedResponse.GrpcStatusInfo.Should().BeNull(); invokedResponse.HttpStatusCode.Should().NotBeNull(); invokedResponse.HttpStatusCode.Should().Be(HttpStatusCode.OK); }
public async Task InvokeMethodAsync_CanInvokeRawMethodWithResponse_HttpServerReturnsNonSuccessResponse() { var client = new MockClient(); var data = new Response() { Name = "Look, I was invoked!" }; var invokeResponse = new InvokeResponse { Data = TypeConverters.ToAny(data) }; var response = client.Call <InvokeResponse>() .SetResponse(invokeResponse) .AddHeader("dapr-status-header", "200") .Build(); var trailers = new Metadata(); const string grpcErrorInfoReason = "Insufficient permissions"; const int grpcErrorInfoDetailHttpCode = 500; const string grpcErrorInfoDetailHttpErrorMsg = "no permissions"; int grpcStatusCode = Convert.ToInt32(StatusCode.PermissionDenied); const string grpcStatusMessage = "Bad permissions"; var details = new Google.Rpc.Status { Code = grpcStatusCode, Message = grpcStatusMessage, }; var errorInfo = new Google.Rpc.ErrorInfo { Reason = grpcErrorInfoReason, Domain = "dapr.io", }; errorInfo.Metadata.Add("http.code", grpcErrorInfoDetailHttpCode.ToString()); errorInfo.Metadata.Add("http.error_message", grpcErrorInfoDetailHttpErrorMsg); details.Details.Add(Google.Protobuf.WellKnownTypes.Any.Pack(errorInfo)); var entry = new Metadata.Entry("grpc-status-details-bin", Google.Protobuf.MessageExtensions.ToByteArray(details)); trailers.Add(entry); const string rpcExceptionMessage = "No access to app"; const StatusCode rpcStatusCode = StatusCode.PermissionDenied; const string rpcStatusDetail = "Insufficient permissions"; var rpcException = new RpcException(new Status(rpcStatusCode, rpcStatusDetail), trailers, rpcExceptionMessage); // Setup the mock client to throw an Rpc Exception with the expected details info client.Mock .Setup(m => m.InvokeServiceAsync(It.IsAny <Autogen.Grpc.v1.InvokeServiceRequest>(), It.IsAny <CallOptions>())) .Throws(rpcException); try { var body = new Request() { RequestParameter = "Hello " }; var bytes = JsonSerializer.SerializeToUtf8Bytes(body); await client.DaprClient.InvokeMethodRawAsync("test", "testMethod", bytes); Assert.False(true); } catch (InvocationException ex) { ex.Message.Should().Be("Exception while invoking testMethod on appId:test"); ex.InnerException.Message.Should().Be(rpcExceptionMessage); ex.Response.GrpcStatusInfo.Should().BeNull(); Encoding.UTF8.GetString(ex.Response.Body).Should().Be(grpcErrorInfoDetailHttpErrorMsg); ex.Response.HttpStatusCode.Should().Be(grpcErrorInfoDetailHttpCode); } }