Skip to content

Commit

Permalink
fix: preserve original source for cloud event messages (#108)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Gérald Barré <[email protected]>
Co-authored-by: Dzhem Aptula <[email protected]>
Co-authored-by: Dzhem Aptula <[email protected]>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent 94b1bee commit c62d954
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
47 changes: 40 additions & 7 deletions src/EventGridEmulator.Tests/EmulatorValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task ValidatePublishAndSubscribeRoundTripForEventGridEvent()
{
// Define the handler to intercept the message
var message = string.Empty;
HttpMessageHandler handler = new TestHandler(requestAction => message = requestAction);
using HttpMessageHandler handler = new TestHandler(requestAction => message = requestAction);

// Create the EventGrid subscriber
using var subscriber = new FactoryClientBuilder(handler)
Expand Down Expand Up @@ -47,11 +47,11 @@ public async Task ValidatePublishAndSubscribeRoundTripForEventGridEvent()
}

[Fact]
public async Task ValidatePublishAndSubscribeRoundTripForCloudEvent()
public async Task ValidatePublishAndSubscribeRoundTripForCloudEventWithEmptySource()
{
// Define the handler to intercept the message
var message = string.Empty;
HttpMessageHandler handler = new TestHandler(requestAction => message = requestAction);
using HttpMessageHandler handler = new TestHandler(requestAction => message = requestAction);

// Create the EventGrid subscriber
using var subscriber = new FactoryClientBuilder(handler)
Expand All @@ -63,8 +63,8 @@ public async Task ValidatePublishAndSubscribeRoundTripForCloudEvent()
.WithEndpoint(new Uri("https://localhost/orders/api/events"))
.Build();

// Create and send an event to EventGrid
var cloudEvent = new CloudEvent("foo", "bar", new DataModel(some: "data"));
// Create and send an event to EventGrid with an empty source
var cloudEvent = new CloudEvent("", "bar", new DataModel(some: "data"));
var response = await publisher.SendEventAsync(cloudEvent);

// Assert that the message was successfully sent
Expand All @@ -73,9 +73,42 @@ public async Task ValidatePublishAndSubscribeRoundTripForCloudEvent()
// Assert that the message was successfully received
var @event = JsonSerializer.Deserialize<JsonObject[]>(message) ?? throw new NullReferenceException("Message cannot be deserialized");
var result = @event.Single()["data"].Deserialize<DataModel>();
var receivedTopic = @event.Single()["source"].Deserialize<string>();
var receivedSource = @event.Single()["source"].Deserialize<string>();
Assert.Equal("data", result?.Some);
Assert.Equal($"{SubscriberConstants.DefaultTopicValue}{ExpectedTopic}", receivedTopic);
Assert.Equal($"{SubscriberConstants.DefaultTopicValue}{ExpectedTopic}", receivedSource);
}

[Fact]
public async Task ValidatePublishAndSubscribeRoundTripForCloudEventPreserveOriginalSource()
{
// Define the handler to intercept the message
var message = string.Empty;
var originalSource = "SOME_SOURCE_VALUE";
using HttpMessageHandler handler = new TestHandler(requestAction => message = requestAction);

// Create the EventGrid subscriber
using var subscriber = new FactoryClientBuilder(handler)
.WithTopic(ExpectedTopic, "https://localhost/orders-webhook")
.Build();

// Create the EventGrid publisher
var publisher = new PublisherBuilder(subscriber)
.WithEndpoint(new Uri("https://localhost/orders/api/events"))
.Build();

// Create and send an event to EventGrid with a source
var cloudEvent = new CloudEvent(originalSource, "bar", new DataModel(some: "data"));
var response = await publisher.SendEventAsync(cloudEvent);

// Assert that the message was successfully sent
Assert.Equal(200, response.Status);

// Assert that the message was successfully received
var @event = JsonSerializer.Deserialize<JsonObject[]>(message) ?? throw new NullReferenceException("Message cannot be deserialized");
var result = @event.Single()["data"].Deserialize<DataModel>();
var receivedSource = @event.Single()["source"].Deserialize<string>();
Assert.Equal("data", result?.Some);
Assert.Equal(originalSource, receivedSource);
}

private sealed class DataModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ protected override void EnhanceEventData(IEnumerable<CloudEvent> cloudEvents, st
{
foreach (var @event in cloudEvents)
{
@event.Source = $"{SubscriberConstants.DefaultTopicValue}{topicName}";
if (string.IsNullOrEmpty(@event.Source))
{
@event.Source = $"{SubscriberConstants.DefaultTopicValue}{topicName}";
}
}
}
}

0 comments on commit c62d954

Please sign in to comment.