Skip to content

Commit ef73a70

Browse files
committed
More tests
1 parent e49ce6c commit ef73a70

6 files changed

Lines changed: 35 additions & 20 deletions

File tree

samples/CleanArchitectureSample/README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,11 @@ You can create your own attributes following the same pattern — define an attr
369369
A streaming handler uses `IAsyncEnumerable<T>` to push domain events to clients in real time via Server-Sent Events. The mediator's built-in `SubscribeAsync<T>` yields events as they're published anywhere in the system:
370370

371371
```csharp
372-
[Handler]
373372
public class ClientEventStreamHandler(IMediator mediator)
374373
{
375-
[HandlerEndpoint(
376-
Route = "/events/stream",
377-
Streaming = EndpointStreaming.ServerSentEvents,
378-
Summary = "Subscribe to real-time domain events via Server-Sent Events")]
379-
[HandlerAllowAnonymous]
374+
[HandlerEndpoint(Streaming = EndpointStreaming.ServerSentEvents)]
380375
public async IAsyncEnumerable<ClientEvent> Handle(
381-
SubscribeToClientEvents message,
376+
GetEventStream message,
382377
[EnumeratorCancellation] CancellationToken cancellationToken)
383378
{
384379
await foreach (var evt in mediator.SubscribeAsync<IDispatchToClient>(cancellationToken: cancellationToken))

samples/CleanArchitectureSample/src/Api/Handlers/ClientEventStreamHandler.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ public record ClientEvent(string EventType, object Data);
1313
/// <summary>
1414
/// Message to subscribe to real-time client events via SSE.
1515
/// </summary>
16-
public record SubscribeToClientEvents;
16+
public record GetEventStream;
1717

1818
/// <summary>
1919
/// Streaming handler that subscribes to all IDispatchToClient notifications via the
2020
/// mediator's built-in subscription support and streams them as SSE events.
2121
/// </summary>
22-
[Handler]
2322
public class ClientEventStreamHandler(IMediator mediator)
2423
{
2524
[HandlerEndpoint(
26-
Route = "/events/stream",
2725
Streaming = EndpointStreaming.ServerSentEvents,
2826
Summary = "Subscribe to real-time domain events via Server-Sent Events")]
29-
[HandlerAllowAnonymous]
3027
public async IAsyncEnumerable<ClientEvent> Handle(
31-
SubscribeToClientEvents message,
28+
GetEventStream message,
3229
[EnumeratorCancellation] CancellationToken cancellationToken)
3330
{
3431
await foreach (var evt in mediator.SubscribeAsync<IDispatchToClient>(cancellationToken: cancellationToken))

src/Foundatio.Mediator.Abstractions/IMediator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ public interface IMediator
137137
/// <remarks>
138138
/// Typical usage is in streaming SSE endpoints that push domain events to connected clients:
139139
/// <code>
140-
/// public IAsyncEnumerable&lt;IDispatchToClient&gt; Handle(
141-
/// SubscribeToClientEvents message,
140+
/// public IAsyncEnumerable&lt;INotification&gt; Handle(
141+
/// GetEventStream message,
142142
/// CancellationToken ct)
143143
/// {
144-
/// return mediator.SubscribeAsync&lt;IDispatchToClient&gt;(cancellationToken: ct);
144+
/// return mediator.SubscribeAsync&lt;INotification&gt;(cancellationToken: ct);
145145
/// }
146146
/// </code>
147147
/// </remarks>

src/Foundatio.Mediator/HandlerAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ private static string RemoveVerbPrefix(string name)
936936
/// </summary>
937937
private static readonly string[] EntitySuffixes =
938938
[
939-
"Paginated", "Details", "Detail", "Summary", "ById", "Paged", "List"
939+
"Paginated", "Details", "Detail", "Summary", "ById", "Paged", "Stream", "List"
940940
];
941941

942942
/// <summary>

tests/Foundatio.Mediator.Tests/EndpointGenerationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,11 +1516,11 @@ public void StreamingEndpoint_DefaultFormat_ReturnsOkWithStream()
15161516
15171517
[assembly: MediatorConfiguration(EndpointDiscovery = EndpointDiscovery.All)]
15181518
1519-
public record SubscribeToEvents;
1519+
public record GetEventStream;
15201520
15211521
public class EventStreamHandler
15221522
{
1523-
public async IAsyncEnumerable<string> HandleAsync(SubscribeToEvents query) { yield return "event"; }
1523+
public async IAsyncEnumerable<string> HandleAsync(GetEventStream query) { yield return "event"; }
15241524
}
15251525
""";
15261526

@@ -1545,12 +1545,12 @@ public void StreamingEndpoint_SseFormat_UsesTypedResultsServerSentEvents()
15451545
15461546
[assembly: MediatorConfiguration(EndpointDiscovery = EndpointDiscovery.All)]
15471547
1548-
public record SubscribeToEvents;
1548+
public record GetEventStream;
15491549
15501550
public class EventStreamHandler
15511551
{
15521552
[HandlerEndpoint(Streaming = EndpointStreaming.ServerSentEvents, SseEventType = "event")]
1553-
public async IAsyncEnumerable<string> HandleAsync(SubscribeToEvents query) { yield return "event"; }
1553+
public async IAsyncEnumerable<string> HandleAsync(GetEventStream query) { yield return "event"; }
15541554
}
15551555
""";
15561556

tests/Foundatio.Mediator.Tests/EndpointRouteConventionTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,29 @@ public class OrderHandler
453453
AssertNoRouteContains(endpointSource, "summary");
454454
}
455455

456+
[Fact]
457+
public void EntitySuffix_Stream_StrippedFromEntityName()
458+
{
459+
var source = """
460+
using Foundatio.Mediator;
461+
462+
[assembly: MediatorConfiguration(EndpointDiscovery = EndpointDiscovery.All)]
463+
464+
public record GetEventStream();
465+
466+
public class EventHandler
467+
{
468+
public string Handle(GetEventStream query) => "stream";
469+
}
470+
""";
471+
472+
var endpointSource = GenerateEndpointSource(source);
473+
if (endpointSource is null) return;
474+
475+
AssertEndpoint(endpointSource, "GET", "/api/events");
476+
AssertNoRouteContains(endpointSource, "stream");
477+
}
478+
456479
[Fact]
457480
public void EntitySuffix_Paginated_StrippedFromEntityName()
458481
{

0 commit comments

Comments
 (0)