Skip to content

Commit

Permalink
Fix IMiddleware not able to take options
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed committed Aug 24, 2022
1 parent 721ad1f commit 73c7dc9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
20 changes: 14 additions & 6 deletions Source/Boxed.AspNetCore/Middleware/HttpExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,34 @@ namespace Boxed.AspNetCore.Middleware;
/// The <see cref="HttpException"/> handling middleware.
/// </summary>
/// <seealso cref="IMiddleware" />
public class HttpExceptionMiddleware : IMiddleware
public class HttpExceptionMiddleware
{
private readonly RequestDelegate next;
private readonly HttpExceptionMiddlewareOptions options;

/// <summary>
/// Initializes a new instance of the <see cref="HttpExceptionMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware.</param>
/// <param name="options">The options.</param>
public HttpExceptionMiddleware(HttpExceptionMiddlewareOptions options) =>
public HttpExceptionMiddleware(RequestDelegate next, HttpExceptionMiddlewareOptions options)
{
this.next = next;
this.options = options;
}

/// <inheritdoc/>
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
/// <summary>
/// Request handling method.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>
public async Task InvokeAsync(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(next);

try
{
await next.Invoke(context).ConfigureAwait(false);
await this.next.Invoke(context).ConfigureAwait(false);
}
catch (HttpException httpException)
{
Expand Down
17 changes: 12 additions & 5 deletions Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,40 @@ namespace Boxed.AspNetCore.Middleware;
/// shortcuts and returns an error status code.
/// </summary>
/// <seealso cref="IMiddleware" />
public class RequestCanceledMiddleware : IMiddleware
public class RequestCanceledMiddleware
{
private readonly ILogger<RequestCanceledMiddleware> logger;
private readonly RequestDelegate next;
private readonly RequestCanceledMiddlewareOptions options;

/// <summary>
/// Initializes a new instance of the <see cref="RequestCanceledMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware.</param>
/// <param name="options">The middleware options.</param>
/// <param name="logger">A logger.</param>
public RequestCanceledMiddleware(
RequestDelegate next,
RequestCanceledMiddlewareOptions options,
ILogger<RequestCanceledMiddleware> logger)
{
this.next = next;
this.options = options;
this.logger = logger;
}

/// <inheritdoc/>
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
/// <summary>
/// Request handling method.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>
public async Task InvokeAsync(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(next);

try
{
await next(context).ConfigureAwait(false);
await this.next(context).ConfigureAwait(false);
}
catch (OperationCanceledException operationCanceledException)
when (operationCanceledException.CancellationToken == context.RequestAborted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public void InvokeAsync_NullNext_ThrowsArgumentNullException() =>
public async Task InvokeAsync_RequestNotCanceled_RunsNextMiddlewareAsync()
{
await new RequestCanceledMiddleware(
this.next,
new RequestCanceledMiddlewareOptions(),
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
.InvokeAsync(this.context, this.next)
.InvokeAsync(this.context)
.ConfigureAwait(false);

Assert.Equal(200, this.context.Response.StatusCode);
Expand All @@ -52,9 +53,10 @@ public async Task InvokeAsync_OperationCanceledExceptionThrownNotCanceled_RunsNe
await Assert
.ThrowsAsync<OperationCanceledException>(() =>
new RequestCanceledMiddleware(
this.next,
new RequestCanceledMiddlewareOptions(),
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
.InvokeAsync(this.context, this.next))
.InvokeAsync(this.context))
.ConfigureAwait(false);
}

Expand All @@ -67,9 +69,10 @@ public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync
this.next = x => Task.FromCanceled(cancellationTokenSource.Token);

await new RequestCanceledMiddleware(
this.next,
new RequestCanceledMiddlewareOptions(),
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
.InvokeAsync(this.context, this.next)
.InvokeAsync(this.context)
.ConfigureAwait(false);

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

0 comments on commit 73c7dc9

Please sign in to comment.