Пример #1
0
        public async Task SaveStateAsync_CanSaveState()
        {
            await using var client = TestClient.CreateForDaprClient();

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.SaveStateAsync("testStore", "test", widget);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.SaveStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.States.Count.Should().Be(1);
            var state = envelope.States[0];

            state.Key.Should().Be("test");

            var stateJson        = state.Value.ToStringUtf8();
            var stateFromRequest = JsonSerializer.Deserialize <Widget>(stateJson, client.InnerClient.JsonSerializerOptions);

            stateFromRequest.Size.Should().Be(widget.Size);
            stateFromRequest.Color.Should().Be(widget.Color);
        }
Пример #2
0
        public async Task ExecuteStateTransactionAsync_ThrowsForNonSuccess()
        {
            await using var client = TestClient.CreateForDaprClient();

            var widget1 = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(widget1), StateOperationType.Upsert);
            var states = new List <StateTransactionRequest>
            {
                state1
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.ExecuteStateTransactionAsync("testStore", states);
            });

            // Create Response & Respond
            var ex = await Assert.ThrowsAsync <DaprException>(async() =>
            {
                await request.CompleteAsync(new HttpResponseMessage(HttpStatusCode.NotAcceptable));
            });

            Assert.IsType <RpcException>(ex.InnerException);
        }
Пример #3
0
        public async Task DeleteBulkStateAsync_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var key      = "test";
            var etag     = "etag";
            var metadata = new Dictionary <string, string>
            {
                { "partitionKey", "mypartition" }
            };
            var deleteBulkStateItem = new BulkDeleteStateItem(key, etag, null, metadata);
            var request             = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.DeleteBulkStateAsync("testStore", new List <BulkDeleteStateItem>()
                {
                    deleteBulkStateItem
                });
            });

            request.Dismiss();

            // Create Response & Validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.DeleteBulkStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.States.Count.Should().Be(1);
            envelope.States[0].Key.Should().Be(key);
            envelope.States[0].Metadata.Should().ContainKey("partitionKey");
        }
        public async Task InvokeMethodGrpcAsync_WithNoReturnTypeAndData()
        {
            await using var client = TestClient.CreateForDaprClient(c =>
            {
                c.UseJsonSerializationOptions(this.jsonSerializerOptions);
            });

            var invokeRequest = new Request()
            {
                RequestParameter = "Hello"
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeMethodGrpcAsync <Request, Response>("test", "test", invokeRequest));
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <InvokeServiceRequest>();

            envelope.Id.Should().Be("test");
            envelope.Message.Method.Should().Be("test");
            envelope.Message.ContentType.Should().Be(Constants.ContentTypeApplicationGrpc);

            var actual = envelope.Message.Data.Unpack <Request>();

            Assert.Equal(invokeRequest.RequestParameter, actual.RequestParameter);
        }
Пример #5
0
        public async Task QueryStateAsync_ValidateResult()
        {
            await using var client = TestClient.CreateForDaprClient();

            var queryJson = "{'query':{'filter':{ 'EQ': {'value':'test'}}}}";
            var request   = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.QueryStateAsync <Widget>("testStore", queryJson, new Dictionary <string, string>()));
            });

            // Validate request.
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.QueryStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Query.Should().Be(queryJson);
            envelope.Metadata.Should().BeEmpty();

            // Validate response.
            var testData = new Widget()
            {
                Color = "Green", Size = "Small"
            };
            var wireResponse = new Autogenerated.QueryStateResponse();

            wireResponse.Results.Add(MakeQueryStateItem("test", testData, "an etag"));

            var response = await request.CompleteWithMessageAsync(wireResponse);

            response.Results.Count.Should().Be(1);
            response.Results[0].Key.Should().Be("test");
            response.Results[0].Data.Should().Be(testData);
            response.Results[0].ETag.Should().Be("an etag");
            response.Results[0].Error.Should().BeNullOrEmpty();
        }
        public async Task InvokeMethodGrpcAsync_CanInvokeMethodWithReturnTypeNoData()
        {
            await using var client = TestClient.CreateForDaprClient(c =>
            {
                c.UseJsonSerializationOptions(this.jsonSerializerOptions);
            });

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeMethodGrpcAsync <Response>("test", "test"));
            });

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <InvokeServiceRequest>();

            envelope.Id.Should().Be("test");
            envelope.Message.Method.Should().Be("test");
            envelope.Message.ContentType.Should().Be(string.Empty);

            // Create Response & Respond
            var data = new Response()
            {
                Name = "Look, I was invoked!"
            };
            var response = new Autogen.Grpc.v1.InvokeResponse()
            {
                Data = Any.Pack(data),
            };

            // Validate Response
            var invokedResponse = await request.CompleteWithMessageAsync(response);

            invokedResponse.Name.Should().Be("Look, I was invoked!");
        }
        public async Task PublishEventAsync_CanPublishTopicWithNoContent_WithMetadata()
        {
            await using var client = TestClient.CreateForDaprClient();

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.PublishEventAsync(TestPubsubName, "test", metadata);
            });

            request.Dismiss();

            var envelope = await request.GetRequestEnvelopeAsync <PublishEventRequest>();

            envelope.PubsubName.Should().Be(TestPubsubName);
            envelope.Topic.Should().Be("test");
            envelope.Data.Length.Should().Be(0);

            envelope.Metadata.Count.Should().Be(2);
            envelope.Metadata.Keys.Contains("key1").Should().BeTrue();
            envelope.Metadata.Keys.Contains("key2").Should().BeTrue();
            envelope.Metadata["key1"].Should().Be("value1");
            envelope.Metadata["key2"].Should().Be("value2");
        }
        public async Task PublishEventAsync_CanPublishTopicWithData()
        {
            await using var client = TestClient.CreateForDaprClient();

            var publishData = new PublishData()
            {
                PublishObjectParameter = "testparam"
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.PublishEventAsync <PublishData>(TestPubsubName, "test", publishData);
            });

            request.Dismiss();

            var envelope = await request.GetRequestEnvelopeAsync <PublishEventRequest>();

            var jsonFromRequest = envelope.Data.ToStringUtf8();

            envelope.DataContentType.Should().Be("application/json");
            envelope.PubsubName.Should().Be(TestPubsubName);
            envelope.Topic.Should().Be("test");
            jsonFromRequest.Should().Be(JsonSerializer.Serialize(publishData, client.InnerClient.JsonSerializerOptions));
            envelope.Metadata.Count.Should().Be(0);
        }
Пример #9
0
        public async Task GetConfigurationAsync_WithNullMetadata_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.GetConfiguration("testStore", new List <string>()
                {
                    "test_key"
                }));
            });

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.GetConfigurationRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Keys.Should().Contain("test_key");
            envelope.Metadata.Count.Should().Be(0);

            // Get response and validate
            var invokeResponse = new Autogenerated.GetConfigurationResponse();

            invokeResponse.Items.Add(new Autogenerated.ConfigurationItem()
            {
                Key     = "testKey",
                Value   = "testValue",
                Version = "v1"
            });

            var domainResponse = await request.CompleteWithMessageAsync(invokeResponse);

            domainResponse.Items.Count.Should().Be(1);
            domainResponse.Items[0].Value.Should().Be("testValue");
            domainResponse.Items[0].Version.Should().Be("v1");
        }
Пример #10
0
        public async Task InvokeBindingRequest_WithNullData_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var req = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeBindingAsync <InvokeRequest, Widget>("binding", "operation", null));
            });

            var resp = new InvokeBindingResponse
            {
                Data = ByteString.CopyFrom(JsonSerializer.SerializeToUtf8Bytes(new Widget()
                {
                    Color = "red",
                }, client.InnerClient.JsonSerializerOptions))
            };
            var response = await req.CompleteWithMessageAsync(resp);

            var envelope = await req.GetRequestEnvelopeAsync <InvokeBindingRequest>();

            envelope.Name.Should().Be("binding");
            envelope.Operation.Should().Be("operation");

            Assert.Equal("red", response.Color);
        }
        public async Task InvokeBindingAsync_ValidateRequest_WithMetadata()
        {
            await using var client = TestClient.CreateForDaprClient();

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
            var invokeRequest = new InvokeRequest()
            {
                RequestParameter = "Hello "
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.InvokeBindingAsync <InvokeRequest>("test", "create", invokeRequest, metadata);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <InvokeBindingRequest>();

            envelope.Name.Should().Be("test");
            envelope.Metadata.Count.Should().Be(2);
            envelope.Metadata.Keys.Contains("key1").Should().BeTrue();
            envelope.Metadata.Keys.Contains("key2").Should().BeTrue();
            envelope.Metadata["key1"].Should().Be("value1");
            envelope.Metadata["key2"].Should().Be("value2");
            var json            = envelope.Data.ToStringUtf8();
            var typeFromRequest = JsonSerializer.Deserialize <InvokeRequest>(json, client.InnerClient.JsonSerializerOptions);

            typeFromRequest.RequestParameter.Should().Be("Hello ");
        }
        public async Task InvokeBindingAsync_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var invokeRequest = new InvokeRequest()
            {
                RequestParameter = "Hello "
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.InvokeBindingAsync <InvokeRequest>("test", "create", invokeRequest);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <InvokeBindingRequest>();

            envelope.Name.Should().Be("test");
            envelope.Metadata.Count.Should().Be(0);
            var json            = envelope.Data.ToStringUtf8();
            var typeFromRequest = JsonSerializer.Deserialize <InvokeRequest>(json, client.InnerClient.JsonSerializerOptions);

            typeFromRequest.RequestParameter.Should().Be("Hello ");
        }
Пример #13
0
        public async Task TryDeleteStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            await using var client = TestClient.CreateForDaprClient();

            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return await daprClient.TryDeleteStateAsync("testStore", "test", "Test_Etag", stateOptions);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.DeleteStateRequest>();
            envelope.StoreName.Should().Be("testStore");
            envelope.Key.Should().Be("test");
            envelope.Etag.Value.Should().Be("Test_Etag");
            envelope.Options.Concurrency.Should().Be(expectedConcurrency);
            envelope.Options.Consistency.Should().Be(expectedConsistency);
        }
        public async Task InvokeBindingAsync_WithRequest_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var payload = new InvokeRequest()
            {
                RequestParameter = "Hello "
            };
            var bindingRequest = new BindingRequest("test", "create")
            {
                Data     = JsonSerializer.SerializeToUtf8Bytes(payload, client.InnerClient.JsonSerializerOptions),
                Metadata =
                {
                    { "key1", "value1" },
                    { "key2", "value2" }
                }
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeBindingAsync(bindingRequest));
            });

            var gRpcResponse = new Autogen.Grpc.v1.InvokeBindingResponse()
            {
                Data = ByteString.CopyFrom(JsonSerializer.SerializeToUtf8Bytes(new Widget()
                {
                    Color = "red",
                }, client.InnerClient.JsonSerializerOptions)),
                Metadata =
                {
                    { "anotherkey", "anothervalue" },
                }
            };
            var response = await request.CompleteWithMessageAsync(gRpcResponse);

            var envelope = await request.GetRequestEnvelopeAsync <InvokeBindingRequest>();

            envelope.Name.Should().Be("test");
            envelope.Metadata.Count.Should().Be(2);
            envelope.Metadata.Keys.Contains("key1").Should().BeTrue();
            envelope.Metadata.Keys.Contains("key2").Should().BeTrue();
            envelope.Metadata["key1"].Should().Be("value1");
            envelope.Metadata["key2"].Should().Be("value2");

            var json            = envelope.Data.ToStringUtf8();
            var typeFromRequest = JsonSerializer.Deserialize <InvokeRequest>(json, client.InnerClient.JsonSerializerOptions);

            typeFromRequest.RequestParameter.Should().Be("Hello ");

            Assert.Same(bindingRequest, response.Request);
            Assert.Equal("red", JsonSerializer.Deserialize <Widget>(response.Data.Span, client.InnerClient.JsonSerializerOptions).Color);
            Assert.Collection(
                response.Metadata,
                kvp =>
            {
                Assert.Equal("anotherkey", kvp.Key);
                Assert.Equal("anothervalue", kvp.Value);
            });
        }
        public async Task InvokeBindingAsync_WrapsJsonException()
        {
            await using var client = TestClient.CreateForDaprClient();

            var response = new Autogen.Grpc.v1.InvokeBindingResponse();
            var bytes    = JsonSerializer.SerializeToUtf8Bytes <Widget>(new Widget()
            {
                Color = "red",
            }, client.InnerClient.JsonSerializerOptions);

            response.Data = ByteString.CopyFrom(bytes.Take(10).ToArray()); // trim it to make invalid JSON blob

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeBindingAsync <InvokeRequest, Widget>("test", "test", new InvokeRequest()
                {
                    RequestParameter = "Hello "
                }));
            });

            var envelope = await request.GetRequestEnvelopeAsync <InvokeBindingRequest>();

            var ex = await Assert.ThrowsAsync <DaprException>(async() =>
            {
                await request.CompleteWithMessageAsync(response);
            });

            Assert.IsType <JsonException>(ex.InnerException);
        }
        public async Task InvokeMethodAsync_VoidResponseNoHttpMethod_Success()
        {
            await using var client = TestClient.CreateForDaprClient(c =>
            {
                c.UseGrpcEndpoint("http://localhost").UseHttpEndpoint("https://test-endpoint:3501").UseJsonSerializationOptions(this.jsonSerializerOptions);
            });

            var request = await client.CaptureHttpRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeMethodAsync <Widget>("app1", "mymethod"));
            });

            // Get Request and validate
            Assert.Equal(request.Request.Method, HttpMethod.Post);
            Assert.Equal(new Uri("https://test-endpoint:3501/v1.0/invoke/app1/method/mymethod").AbsoluteUri, request.Request.RequestUri.AbsoluteUri);
            Assert.Null(request.Request.Content);

            var expected = new Widget()
            {
                Color = "red",
            };

            var actual = await request.CompleteWithJsonAsync(expected, jsonSerializerOptions);

            Assert.Equal(expected.Color, actual.Color);
        }
        public async Task BindAsync_CanBindStateEntry()
        {
            await using var client = TestClient.CreateForDaprClient();

            var binder = new StateEntryModelBinder("testStore", "id", isStateEntry: true, typeof(Widget));

            // Configure Client
            var context = CreateContext(CreateServices(client.InnerClient));

            context.HttpContext.Request.RouteValues["id"] = "test";

            var request = await client.CaptureGrpcRequestAsync(async _ =>
            {
                await binder.BindModelAsync(context);
            });

            // Create Response & Respond
            var state = new Widget()
            {
                Size = "small", Color = "yellow",
            };

            await SendResponseWithState(state, request);

            // Get response and validate
            context.Result.IsModelSet.Should().BeTrue();
            context.Result.Model.As <StateEntry <Widget> >().Key.Should().Be("test");
            context.Result.Model.As <StateEntry <Widget> >().Value.Size.Should().Be("small");
            context.Result.Model.As <StateEntry <Widget> >().Value.Color.Should().Be("yellow");

            context.ValidationState.Count.Should().Be(1);
            context.ValidationState[context.Result.Model].SuppressValidation.Should().BeTrue();
        }
        public async Task InvokeMethodAsync_RequestResponseWithHttpMethod_Success()
        {
            await using var client = TestClient.CreateForDaprClient(c =>
            {
                c.UseGrpcEndpoint("http://localhost").UseHttpEndpoint("https://test-endpoint:3501").UseJsonSerializationOptions(this.jsonSerializerOptions);
            });

            var data = new Widget()
            {
                Color = "red",
            };

            var request = await client.CaptureHttpRequestAsync(async daprClient =>
            {
                return(await daprClient.InvokeMethodAsync <Widget, Widget>(HttpMethod.Put, "app1", "mymethod", data));
            });

            // Get Request and validate
            Assert.Equal(request.Request.Method, HttpMethod.Put);
            Assert.Equal(new Uri("https://test-endpoint:3501/v1.0/invoke/app1/method/mymethod").AbsoluteUri, request.Request.RequestUri.AbsoluteUri);

            var content = Assert.IsType <JsonContent>(request.Request.Content);

            Assert.Equal(data.GetType(), content.ObjectType);
            Assert.Same(data, content.Value);

            var actual = await request.CompleteWithJsonAsync(data, jsonSerializerOptions);

            Assert.Equal(data.Color, actual.Color);
        }
Пример #19
0
        public async Task GetBulkStateAsync_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var key      = "test";
            var metadata = new Dictionary <string, string>
            {
                { "partitionKey", "mypartition" }
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.GetBulkStateAsync("testStore", new List <string>()
                {
                    key
                }, null, metadata: metadata));
            });

            request.Dismiss();

            // Create Response & Validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.GetBulkStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Metadata.Should().BeEquivalentTo(metadata);
        }
Пример #20
0
        public async Task GetStateEntryAsync_CanDeleteState()
        {
            await using var client = TestClient.CreateForDaprClient();

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return await daprClient.GetStateEntryAsync<Widget>("testStore", "test");
            });

            // Create Response & Respond
            var data = new Widget() { Size = "small", Color = "yellow", };
            var state = await request.CompleteWithMessageAsync(MakeGetStateResponse(data));

            state.Key.Should().Be("test");
            state.Value.Size.Should().Be("small");
            state.Value.Color.Should().Be("yellow");

            state.Value.Color = "green";
            var request2 = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await state.DeleteAsync();
            });

            request2.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.DeleteStateRequest>();
            envelope.StoreName.Should().Be("testStore");
            envelope.Key.Should().Be("test");

        }
Пример #21
0
        public async Task GetStateAndEtagAsync_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var metadata = new Dictionary<string, string>
            {
                { "partitionKey", "mypartition" }
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return await daprClient.GetStateAsync<Widget>("testStore", "test", metadata: metadata);
            });

            // Get Request & Validate
            var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.GetStateRequest>();
            envelope.StoreName.Should().Be("testStore");
            envelope.Key.Should().Be("test");
            envelope.Metadata.Should().BeEquivalentTo(metadata);

            // Create Response & Respond
            var state = await request.CompleteWithMessageAsync(MakeGetStateResponse<Widget>(null));

            // Get response and validate
            state.Should().BeNull();
        }
Пример #22
0
        public async Task GetSecretAsync_ValidateRequest()
        {
            await using var client = TestClient.CreateForDaprClient();

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.GetSecretAsync("testStore", "test_key", metadata));
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.GetSecretRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Key.Should().Be("test_key");
            envelope.Metadata.Count.Should().Be(2);
            envelope.Metadata.Keys.Contains("key1").Should().BeTrue();
            envelope.Metadata.Keys.Contains("key2").Should().BeTrue();
            envelope.Metadata["key1"].Should().Be("value1");
            envelope.Metadata["key2"].Should().Be("value2");
        }
Пример #23
0
        public async Task GetBulkSecretAsync_ReturnSingleSecret()
        {
            await using var client = TestClient.CreateForDaprClient();

            var metadata = new Dictionary <string, string>();

            metadata.Add("key1", "value1");
            metadata.Add("key2", "value2");

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.GetBulkSecretAsync("testStore", metadata));
            });

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.GetBulkSecretRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Metadata.Count.Should().Be(2);
            envelope.Metadata.Keys.Contains("key1").Should().BeTrue();
            envelope.Metadata.Keys.Contains("key2").Should().BeTrue();
            envelope.Metadata["key1"].Should().Be("value1");
            envelope.Metadata["key2"].Should().Be("value2");

            // Create Response & Respond
            var secrets = new Dictionary <string, string>();

            secrets.Add("redis_secret", "Guess_Redis");
            var secretsResponse = await SendBulkResponseWithSecrets(secrets, request);

            // Get response and validate
            secretsResponse.Count.Should().Be(1);
            secretsResponse.ContainsKey("redis_secret").Should().BeTrue();
            secretsResponse["redis_secret"]["redis_secret"].Should().Be("Guess_Redis");
        }
        public async Task WaitForSidecarAsync_SuccessWhenSidecarHealthy()
        {
            await using var client = TestClient.CreateForDaprClient();
            var request = await client.CaptureHttpRequestAsync(async daprClient => await daprClient.WaitForSidecarAsync());

            // If we don't throw, we're good.
            await request.CompleteAsync(new HttpResponseMessage());
        }
Пример #25
0
        public async Task QueryStateAsync_EncountersError_ValidatePartialResult()
        {
            await using var client = TestClient.CreateForDaprClient();

            var queryJson = "{'query':{'filter':{ 'EQ': {'value':'test'}}}}";
            var request   = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.QueryStateAsync <Widget>("testStore", queryJson, new Dictionary <string, string>()));
            });

            // Validate request.
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.QueryStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Query.Should().Be(queryJson);
            envelope.Metadata.Should().BeEmpty();

            // Validate response, we expect to only get the first object as the 2nd will present an error.
            var testData1 = new Widget()
            {
                Color = "Green", Size = "Small"
            };
            var testData2 = new Widget()
            {
                Color = "Green", Size = "Medium"
            };
            var testData3 = new Widget()
            {
                Color = "Green", Size = "Large"
            };
            var wireResponse = new Autogenerated.QueryStateResponse();

            wireResponse.Results.Add(MakeQueryStateItem("test1", testData1));
            wireResponse.Results.Add(MakeQueryStateItem("test2", testData2, string.Empty, "An error!"));
            wireResponse.Results.Add(MakeQueryStateItem("test3", testData3));

            var ex = await Assert.ThrowsAsync <StateQueryException <Widget> >(() => request.CompleteWithMessageAsync(wireResponse));

            ex.Message.Should().Be("Encountered an error while processing state query results.");
            var response = ex.Response;

            response.Results.Count.Should().Be(2);
            response.Results[0].Key.Should().Be("test1");
            response.Results[0].Data.Should().Be(testData1);
            response.Results[0].ETag.Should().BeNullOrEmpty();
            response.Results[0].Error.Should().BeNullOrEmpty();
            response.Results[1].Key.Should().Be("test3");
            response.Results[1].Data.Should().Be(testData3);
            response.Results[1].ETag.Should().BeNullOrEmpty();
            response.Results[1].Error.Should().BeNullOrEmpty();

            var failedKeys = ex.FailedKeys;

            failedKeys.Count.Should().Be(1);
            failedKeys[0].Should().Be("test2");
        }
        public async Task CreateInvokeMethodRequest_TransformsUrlCorrectly(string method, string expected)
        {
            await using var client = TestClient.CreateForDaprClient(c =>
            {
                c.UseGrpcEndpoint("http://localhost").UseHttpEndpoint("https://test-endpoint:3501").UseJsonSerializationOptions(this.jsonSerializerOptions);
            });

            var request = client.InnerClient.CreateInvokeMethodRequest("test-app", method);

            Assert.Equal(new Uri(expected).AbsoluteUri, request.RequestUri.AbsoluteUri);
        }
Пример #27
0
        public async Task InvokeBindingRequest_WithOperationNull_CheckException()
        {
            await using var client = TestClient.CreateForDaprClient();

            var ex = await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await client.InnerClient.InvokeBindingAsync <InvokeRequest, Widget>("binding", null, null);
            });

            Assert.IsType <ArgumentNullException>(ex);
        }
Пример #28
0
        public async Task SaveStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            await using var client = TestClient.CreateForDaprClient();

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.SaveStateAsync("testStore", "test", widget, stateOptions, metadata);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.SaveStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.States.Count.Should().Be(1);
            var state = envelope.States[0];

            state.Key.Should().Be("test");
            state.Metadata.Count.Should().Be(2);
            state.Metadata.Keys.Contains("key1").Should().BeTrue();
            state.Metadata.Keys.Contains("key2").Should().BeTrue();
            state.Metadata["key1"].Should().Be("value1");
            state.Metadata["key2"].Should().Be("value2");
            state.Options.Concurrency.Should().Be(expectedConcurrency);
            state.Options.Consistency.Should().Be(expectedConsistency);

            var stateJson        = state.Value.ToStringUtf8();
            var stateFromRequest = JsonSerializer.Deserialize <Widget>(stateJson, client.InnerClient.JsonSerializerOptions);

            stateFromRequest.Size.Should().Be(widget.Size);
            stateFromRequest.Color.Should().Be(widget.Color);
        }
Пример #29
0
        public async Task DeleteStateAsync_WithCancelledToken()
        {
            await using var client = TestClient.CreateForDaprClient();

            var cts = new CancellationTokenSource();
            cts.Cancel();

            await Assert.ThrowsAsync<OperationCanceledException>(async () =>
            {
                await client.InnerClient.DeleteStateAsync("testStore", "key", cancellationToken: cts.Token);
            });
        }
Пример #30
0
        public async Task PublishEventAsync_WithCancelledToken()
        {
            await using var client = TestClient.CreateForDaprClient();

            var cts = new CancellationTokenSource();

            cts.Cancel();

            await Assert.ThrowsAsync <OperationCanceledException>(async() =>
            {
                await client.InnerClient.PublishEventAsync(TestPubsubName, "test", cancellationToken: cts.Token);
            });
        }