public async Task SaveStateAsync_CanSaveState()
        {
            var httpClient = new TestHttpClient();
            var client     = new StateHttpClient(httpClient, new JsonSerializerOptions());

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var task = client.SaveStateAsync("test", widget);

            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            entry.Request.RequestUri.ToString().Should().Be(SaveStateUrl(3500));

            using (var stream = await entry.Request.Content.ReadAsStreamAsync())
            {
                var json = await JsonSerializer.DeserializeAsync <JsonElement>(stream);

                json.ValueKind.Should().Be(JsonValueKind.Array);
                json.GetArrayLength().Should().Be(1);
                json[0].GetProperty("key").GetString().Should().Be("test");

                var value = JsonSerializer.Deserialize <Widget>(json[0].GetProperty("value").GetRawText());
                value.Size.Should().Be("small");
                value.Color.Should().Be("yellow");
            }

            entry.Respond(new HttpResponseMessage(HttpStatusCode.NoContent));
            await task;
        }
        public async Task SetStateAsync_ThrowsForNonSuccess()
        {
            var httpClient = new TestHttpClient();
            var client     = new StateHttpClient(httpClient, new JsonSerializerOptions());

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var task = client.SaveStateAsync("test", widget);

            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            entry.Request.RequestUri.ToString().Should().Be(SaveStateUrl(3500));

            entry.Respond(new HttpResponseMessage(HttpStatusCode.NotAcceptable));

            await FluentActions.Awaiting(async() => await task).Should().ThrowAsync <HttpRequestException>();
        }
        public async Task SetStateAsync_WithBaseAddress_GeneratesCorrectUrl()
        {
            var httpClient = new TestHttpClient()
            {
                BaseAddress = new Uri($"http://{DaprDefaultEndpoint}:5000/"),
            };
            var client = new StateHttpClient(httpClient, new JsonSerializerOptions());

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var task = client.SaveStateAsync("testStore", "test", widget);

            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            entry.Request.RequestUri.ToString().Should().Be(SaveStateUrl(5000, "testStore"));

            entry.Respond(new HttpResponseMessage(HttpStatusCode.OK));

            await task;
        }
        public async Task SaveStateAsync_CanClearState()
        {
            var httpClient = new TestHttpClient();
            var client     = new StateHttpClient(httpClient, new JsonSerializerOptions());

            var task = client.SaveStateAsync <object>("test", null);

            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            entry.Request.RequestUri.ToString().Should().Be(SaveStateUrl(3500));

            using (var stream = await entry.Request.Content.ReadAsStreamAsync())
            {
                var json = await JsonSerializer.DeserializeAsync <JsonElement>(stream);

                json.ValueKind.Should().Be(JsonValueKind.Array);
                json.GetArrayLength().Should().Be(1);
                json[0].GetProperty("key").GetString().Should().Be("test");

                json[0].GetProperty("value").ValueKind.Should().Be(JsonValueKind.Null);
            }

            entry.Respond(new HttpResponseMessage(HttpStatusCode.NoContent));
            await task;
        }