From b3d03b1a75b8b603088dfb43cb6480b926b6012a Mon Sep 17 00:00:00 2001 From: Ivan Paulovich Date: Mon, 21 Oct 2019 19:16:10 +0200 Subject: [PATCH] Validate Null Requests #37 --- changelog.md | 4 +++ ...soft.Extensions.DependencyInjection.csproj | 2 +- src/FluentMediator/FluentMediator.csproj | 2 +- src/FluentMediator/Mediator.cs | 30 +++++++++++++++++++ src/FluentMediator/NullRequestException.cs | 9 ++++++ test/UnitTests/SendingRequestTests.cs | 17 +++++++++++ test/UnitTests/UnitTests.csproj | 2 +- 7 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/FluentMediator/NullRequestException.cs diff --git a/changelog.md b/changelog.md index d2bc535..ed42ed5 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj index 2b1fc81..b041306 100644 --- a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj +++ b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj @@ -6,7 +6,7 @@ FluentMediator.Microsoft.Extensions.DependencyInjection - 0.4.3 + 0.4.4 Ivan Paulovich Ivan Paulovich Microsoft Extensions for FluentMediator. diff --git a/src/FluentMediator/FluentMediator.csproj b/src/FluentMediator/FluentMediator.csproj index 45b7f88..a1025f9 100644 --- a/src/FluentMediator/FluentMediator.csproj +++ b/src/FluentMediator/FluentMediator.csproj @@ -10,7 +10,7 @@ FluentMediator - 0.4.3 + 0.4.4 Ivan Paulovich Ivan Paulovich FluentMediator is an unobtrusive library that allows developers to build custom pipelines for Commands, Queries and Events. diff --git a/src/FluentMediator/Mediator.cs b/src/FluentMediator/Mediator.cs index ff5e01b..84fa3d1 100644 --- a/src/FluentMediator/Mediator.cs +++ b/src/FluentMediator/Mediator.cs @@ -18,6 +18,11 @@ public Mediator( 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); @@ -33,6 +38,11 @@ public void Publish(object request, string? pipelineName = null) public TResult Send(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); @@ -47,6 +57,11 @@ public TResult Send(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); @@ -61,6 +76,11 @@ public async Task PublishAsync(object request, string? pipelineName = null) public async Task SendAsync(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); @@ -75,6 +95,11 @@ public async Task SendAsync(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); @@ -89,6 +114,11 @@ public async Task PublishAsync(object request, CancellationToken ct, string? pip public async Task SendAsync(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); diff --git a/src/FluentMediator/NullRequestException.cs b/src/FluentMediator/NullRequestException.cs new file mode 100644 index 0000000..7120291 --- /dev/null +++ b/src/FluentMediator/NullRequestException.cs @@ -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) { } + } +} \ No newline at end of file diff --git a/test/UnitTests/SendingRequestTests.cs b/test/UnitTests/SendingRequestTests.cs index a84ecd5..506bd88 100644 --- a/test/UnitTests/SendingRequestTests.cs +++ b/test/UnitTests/SendingRequestTests.cs @@ -245,5 +245,22 @@ public void Send_Not_Configured_Throws_PipelineNotFoundException() Assert.NotNull(actualEx); Assert.IsType(actualEx); } + + [Fact] + public void Send_Throws_Exception_Null_Requests() + { + var services = new ServiceCollection(); + services.AddFluentMediator(m => + { }); + + services.AddScoped(); + var provider = services.BuildServiceProvider(); + var mediator = provider.GetRequiredService(); + + var actualEx = Record.Exception(() => mediator.Send(null!)); + + Assert.NotNull(actualEx); + Assert.IsType(actualEx); + } } } \ No newline at end of file diff --git a/test/UnitTests/UnitTests.csproj b/test/UnitTests/UnitTests.csproj index ca612fa..5ed3b83 100644 --- a/test/UnitTests/UnitTests.csproj +++ b/test/UnitTests/UnitTests.csproj @@ -15,7 +15,7 @@ - +