Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions test/Dapr.Client.Test/PublishEventApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System.Collections.Immutable;
using System.Linq;
using System.Net.Http;
using System.Text.Json.Serialization;
using Grpc.Net.Client;

namespace Dapr.Client.Test
{
using System;
Expand Down Expand Up @@ -51,6 +57,44 @@ public async Task PublishEventAsync_CanPublishTopicWithData()
envelope.Metadata.Count.Should().Be(0);
}

[Fact]
public async Task PublishEvent_ShouldRespectJsonStringEnumConverter()
{
//The following mimics how the TestClient is built, but adds the JsonStringEnumConverter to the serialization options
var handler = new TestClient.CapturingHandler();
var httpClient = new HttpClient(handler);
var clientBuilder = new DaprClientBuilder()
.UseJsonSerializationOptions(new JsonSerializerOptions()
{
Converters = {new JsonStringEnumConverter(null, false)}
})
.UseHttpClientFactory(() => httpClient)
.UseGrpcChannelOptions(new GrpcChannelOptions()
{
HttpClient = httpClient, ThrowOperationCanceledOnCancellation = true
});
var client = new TestClient<DaprClient>(clientBuilder.Build(), handler);

//Ensure that the JsonStringEnumConverter is registered
client.InnerClient.JsonSerializerOptions.Converters.Count.Should().Be(1);
client.InnerClient.JsonSerializerOptions.Converters.First().GetType().Name.Should()
.Match(nameof(JsonStringEnumConverter));

var publishData = new Widget {Size = "Large", Color = WidgetColor.Red};
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
await daprClient.PublishEventAsync<Widget>(TestPubsubName, "test", publishData);
});

request.Dismiss();

var envelope = await request.GetRequestEnvelopeAsync<PublishEventRequest>();
var jsonFromRequest = envelope.Data.ToStringUtf8();
jsonFromRequest.Should()
.Be(JsonSerializer.Serialize(publishData, client.InnerClient.JsonSerializerOptions));
jsonFromRequest.Should().Match("{\"Size\":\"Large\",\"Color\":\"Red\"}");
}

[Fact]
public async Task PublishEventAsync_CanPublishTopicWithData_WithMetadata()
{
Expand Down Expand Up @@ -259,5 +303,18 @@ private class PublishData
{
public string PublishObjectParameter { get; set; }
}

private class Widget
{
public string Size { get; set; }
public WidgetColor Color { get; set; }
}

private enum WidgetColor
{
Red,
Green,
Yellow
}
}
}