-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca5c3d9
commit 721ad1f
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |