Skip to content

Commit

Permalink
Validate Null Requests #37
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpaulovich committed Oct 21, 2019
1 parent b5ac398 commit b3d03b1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 2019-10-20 - 0.4.4

* #37 Validation for Null Requests added.

## 2019-10-20 - 0.4.3

* #2 Support o Named Pipelines Added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup>
<PackageId>FluentMediator.Microsoft.Extensions.DependencyInjection</PackageId>
<Version>0.4.3</Version>
<Version>0.4.4</Version>
<Authors>Ivan Paulovich</Authors>
<Copyright>Ivan Paulovich</Copyright>
<Description>Microsoft Extensions for FluentMediator.</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/FluentMediator/FluentMediator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<PropertyGroup>
<PackageId>FluentMediator</PackageId>
<Version>0.4.3</Version>
<Version>0.4.4</Version>
<Authors>Ivan Paulovich</Authors>
<Copyright>Ivan Paulovich</Copyright>
<Description>FluentMediator is an unobtrusive library that allows developers to build custom pipelines for Commands, Queries and Events.</Description>
Expand Down
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);
}
}
}
2 changes: 1 addition & 1 deletion test/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Moq" Version="4.13.0"/>
<PackageReference Include="Moq" Version="4.13.1"/>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit b3d03b1

Please sign in to comment.