Skip to content

Commit

Permalink
Validate Null Requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpaulovich committed Oct 21, 2019
1 parent b5ac398 commit 560a406
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/FluentMediator/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public sealed class Mediator : IMediator

public void Publish(object request, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetPipeline(pipelineName);
Expand All @@ -33,6 +38,11 @@ public void Publish(object request, string? pipelineName = null)

public TResult Send<TResult>(object request, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetPipeline(pipelineName);
Expand All @@ -47,6 +57,11 @@ public TResult Send<TResult>(object request, string? pipelineName = null)

public async Task PublishAsync(object request, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetAsyncPipeline(pipelineName);
Expand All @@ -61,6 +76,11 @@ public async Task PublishAsync(object request, string? pipelineName = null)

public async Task<TResult> SendAsync<TResult>(object request, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetAsyncPipeline(pipelineName);
Expand All @@ -75,6 +95,11 @@ public async Task<TResult> SendAsync<TResult>(object request, string? pipelineNa

public async Task PublishAsync(object request, CancellationToken ct, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetCancellablePipeline(pipelineName);
Expand All @@ -89,6 +114,11 @@ public async Task PublishAsync(object request, CancellationToken ct, string? pip

public async Task<TResult> SendAsync<TResult>(object request, CancellationToken ct, string? pipelineName = null)
{
if (request is null)
{
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
{
var pipeline = _pipelines.GetCancellablePipeline(pipelineName);
Expand Down
9 changes: 9 additions & 0 deletions src/FluentMediator/NullRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FluentMediator
{
public class NullRequestException: MediatorException
{
public NullRequestException() { }
public NullRequestException(string message) : base(message) { }
public NullRequestException(string message, System.Exception inner) : base(message, inner) { }
}
}
17 changes: 17 additions & 0 deletions test/UnitTests/SendingRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,22 @@ public void Send_Not_Configured_Throws_PipelineNotFoundException()
Assert.NotNull(actualEx);
Assert.IsType<PipelineNotFoundException>(actualEx);
}

[Fact]
public void Send_Throws_Exception_Null_Requests()
{
var services = new ServiceCollection();
services.AddFluentMediator(m =>
{ });

services.AddScoped<IPingHandler, PingHandler>();
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();

var actualEx = Record.Exception(() => mediator.Send<PingResponse>(null!));

Assert.NotNull(actualEx);
Assert.IsType<NullRequestException>(actualEx);
}
}
}

0 comments on commit 560a406

Please sign in to comment.