Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed committed Aug 24, 2022
1 parent ca5c3d9 commit 721ad1f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Source/Boxed.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static IApplicationBuilder UseHttpException(
/// </summary>
/// <param name="application">The application builder.</param>
/// <returns>The same application builder.</returns>
public static IApplicationBuilder UseRequestCancelled(this IApplicationBuilder application) =>
UseRequestCancelled(application, null);
public static IApplicationBuilder UseRequestCanceled(this IApplicationBuilder application) =>
UseRequestCanceled(application, null);

/// <summary>
/// Handles <see cref="OperationCanceledException"/> caused by the HTTP request being aborted, then shortcuts and
Expand All @@ -52,7 +52,7 @@ public static IApplicationBuilder UseRequestCancelled(this IApplicationBuilder a
/// <param name="application">The application builder.</param>
/// <param name="configureOptions">The middleware options.</param>
/// <returns>The same application builder.</returns>
public static IApplicationBuilder UseRequestCancelled(
public static IApplicationBuilder UseRequestCanceled(
this IApplicationBuilder application,
Action<RequestCanceledMiddlewareOptions>? configureOptions)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Boxed.AspNetCore/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>(() => new ServerTimingMiddleware().InvokeAsync(null!, this.next));

[Fact]
public void InvokeAsync_NullNext_ThrowsArgumentNullException() =>
Assert.ThrowsAsync<ArgumentNullException>(() => new ServerTimingMiddleware().InvokeAsync(this.context, null!));

[Fact]
public async Task InvokeAsync_RequestNotCanceled_RunsNextMiddlewareAsync()
{
await new RequestCanceledMiddleware(
new RequestCanceledMiddlewareOptions(),
new Mock<ILogger<RequestCanceledMiddleware>>().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<OperationCanceledException>(() =>
new RequestCanceledMiddleware(
new RequestCanceledMiddlewareOptions(),
new Mock<ILogger<RequestCanceledMiddleware>>().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<ILogger<RequestCanceledMiddleware>>().Object)
.InvokeAsync(this.context, this.next)
.ConfigureAwait(false);

Assert.Equal(RequestCanceledMiddlewareOptions.ClientClosedRequest, this.context.Response.StatusCode);
}
}

0 comments on commit 721ad1f

Please sign in to comment.