From 721ad1fa5367e19f71a34225fc8ff285445aa1d5 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Saeed Date: Wed, 24 Aug 2022 10:55:24 +0100 Subject: [PATCH] Add tests --- .../ApplicationBuilderExtensions.cs | 6 +- Source/Boxed.AspNetCore/LoggerExtensions.cs | 4 +- .../Middleware/RequestCanceledMiddleware.cs | 2 +- .../RequestCanceledMiddlewareTest.cs | 77 +++++++++++++++++++ 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs diff --git a/Source/Boxed.AspNetCore/ApplicationBuilderExtensions.cs b/Source/Boxed.AspNetCore/ApplicationBuilderExtensions.cs index 3cfa6017..3970e121 100644 --- a/Source/Boxed.AspNetCore/ApplicationBuilderExtensions.cs +++ b/Source/Boxed.AspNetCore/ApplicationBuilderExtensions.cs @@ -41,8 +41,8 @@ public static IApplicationBuilder UseHttpException( /// /// The application builder. /// The same application builder. - public static IApplicationBuilder UseRequestCancelled(this IApplicationBuilder application) => - UseRequestCancelled(application, null); + public static IApplicationBuilder UseRequestCanceled(this IApplicationBuilder application) => + UseRequestCanceled(application, null); /// /// Handles caused by the HTTP request being aborted, then shortcuts and @@ -52,7 +52,7 @@ public static IApplicationBuilder UseRequestCancelled(this IApplicationBuilder a /// The application builder. /// The middleware options. /// The same application builder. - public static IApplicationBuilder UseRequestCancelled( + public static IApplicationBuilder UseRequestCanceled( this IApplicationBuilder application, Action? configureOptions) { diff --git a/Source/Boxed.AspNetCore/LoggerExtensions.cs b/Source/Boxed.AspNetCore/LoggerExtensions.cs index cf5ac6ed..96757235 100644 --- a/Source/Boxed.AspNetCore/LoggerExtensions.cs +++ b/Source/Boxed.AspNetCore/LoggerExtensions.cs @@ -17,6 +17,6 @@ internal static partial class LoggerExtensions [LoggerMessage( EventId = 4001, Level = LogLevel.Information, - Message = "Request was cancelled.")] - public static partial void RequestCancelled(this ILogger logger); + Message = "Request was canceled.")] + public static partial void RequestCanceled(this ILogger logger); } diff --git a/Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs b/Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs index e0e16c71..02d43f9d 100644 --- a/Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs +++ b/Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs @@ -41,7 +41,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) catch (OperationCanceledException operationCanceledException) when (operationCanceledException.CancellationToken == context.RequestAborted) { - this.logger.RequestCancelled(); + this.logger.RequestCanceled(); context.Response.StatusCode = this.options.StatusCode; } } diff --git a/Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs b/Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs new file mode 100644 index 00000000..2ba44676 --- /dev/null +++ b/Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs @@ -0,0 +1,77 @@ +namespace Boxed.AspNetCore.Test.Middleware; + +using System; +using System.Threading; +using System.Threading.Tasks; +using Boxed.AspNetCore.Middleware; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Moq; +using Xunit; + +public class RequestCanceledMiddlewareTest +{ + private readonly DefaultHttpContext context; + private RequestDelegate next; + + public RequestCanceledMiddlewareTest() + { + this.context = new DefaultHttpContext(); + this.next = x => Task.CompletedTask; + } + + [Fact] + public void InvokeAsync_NullContext_ThrowsArgumentNullException() => + Assert.ThrowsAsync(() => new ServerTimingMiddleware().InvokeAsync(null!, this.next)); + + [Fact] + public void InvokeAsync_NullNext_ThrowsArgumentNullException() => + Assert.ThrowsAsync(() => new ServerTimingMiddleware().InvokeAsync(this.context, null!)); + + [Fact] + public async Task InvokeAsync_RequestNotCanceled_RunsNextMiddlewareAsync() + { + await new RequestCanceledMiddleware( + new RequestCanceledMiddlewareOptions(), + new Mock>().Object) + .InvokeAsync(this.context, this.next) + .ConfigureAwait(false); + + Assert.Equal(200, this.context.Response.StatusCode); + } + + [Fact] + public async Task InvokeAsync_OperationCanceledExceptionThrownNotCanceled_RunsNextMiddlewareAsync() + { + using var cancellationTokenSource1 = new CancellationTokenSource(); + using var cancellationTokenSource2 = new CancellationTokenSource(); + cancellationTokenSource2.Cancel(); + this.context.RequestAborted = cancellationTokenSource1.Token; + this.next = x => Task.FromException(new OperationCanceledException(cancellationTokenSource2.Token)); + + await Assert + .ThrowsAsync(() => + new RequestCanceledMiddleware( + new RequestCanceledMiddlewareOptions(), + new Mock>().Object) + .InvokeAsync(this.context, this.next)) + .ConfigureAwait(false); + } + + [Fact] + public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync() + { + using var cancellationTokenSource = new CancellationTokenSource(); + cancellationTokenSource.Cancel(); + this.context.RequestAborted = cancellationTokenSource.Token; + this.next = x => Task.FromCanceled(cancellationTokenSource.Token); + + await new RequestCanceledMiddleware( + new RequestCanceledMiddlewareOptions(), + new Mock>().Object) + .InvokeAsync(this.context, this.next) + .ConfigureAwait(false); + + Assert.Equal(RequestCanceledMiddlewareOptions.ClientClosedRequest, this.context.Response.StatusCode); + } +}