|
| 1 | +namespace Boxed.AspNetCore.Test.Middleware; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Boxed.AspNetCore.Middleware; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Moq; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +public class RequestCanceledMiddlewareTest |
| 13 | +{ |
| 14 | + private readonly DefaultHttpContext context; |
| 15 | + private RequestDelegate next; |
| 16 | + |
| 17 | + public RequestCanceledMiddlewareTest() |
| 18 | + { |
| 19 | + this.context = new DefaultHttpContext(); |
| 20 | + this.next = x => Task.CompletedTask; |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void InvokeAsync_NullContext_ThrowsArgumentNullException() => |
| 25 | + Assert.ThrowsAsync<ArgumentNullException>(() => new ServerTimingMiddleware().InvokeAsync(null!, this.next)); |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void InvokeAsync_NullNext_ThrowsArgumentNullException() => |
| 29 | + Assert.ThrowsAsync<ArgumentNullException>(() => new ServerTimingMiddleware().InvokeAsync(this.context, null!)); |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task InvokeAsync_RequestNotCanceled_RunsNextMiddlewareAsync() |
| 33 | + { |
| 34 | + await new RequestCanceledMiddleware( |
| 35 | + new RequestCanceledMiddlewareOptions(), |
| 36 | + new Mock<ILogger<RequestCanceledMiddleware>>().Object) |
| 37 | + .InvokeAsync(this.context, this.next) |
| 38 | + .ConfigureAwait(false); |
| 39 | + |
| 40 | + Assert.Equal(200, this.context.Response.StatusCode); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task InvokeAsync_OperationCanceledExceptionThrownNotCanceled_RunsNextMiddlewareAsync() |
| 45 | + { |
| 46 | + using var cancellationTokenSource1 = new CancellationTokenSource(); |
| 47 | + using var cancellationTokenSource2 = new CancellationTokenSource(); |
| 48 | + cancellationTokenSource2.Cancel(); |
| 49 | + this.context.RequestAborted = cancellationTokenSource1.Token; |
| 50 | + this.next = x => Task.FromException(new OperationCanceledException(cancellationTokenSource2.Token)); |
| 51 | + |
| 52 | + await Assert |
| 53 | + .ThrowsAsync<OperationCanceledException>(() => |
| 54 | + new RequestCanceledMiddleware( |
| 55 | + new RequestCanceledMiddlewareOptions(), |
| 56 | + new Mock<ILogger<RequestCanceledMiddleware>>().Object) |
| 57 | + .InvokeAsync(this.context, this.next)) |
| 58 | + .ConfigureAwait(false); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync() |
| 63 | + { |
| 64 | + using var cancellationTokenSource = new CancellationTokenSource(); |
| 65 | + cancellationTokenSource.Cancel(); |
| 66 | + this.context.RequestAborted = cancellationTokenSource.Token; |
| 67 | + this.next = x => Task.FromCanceled(cancellationTokenSource.Token); |
| 68 | + |
| 69 | + await new RequestCanceledMiddleware( |
| 70 | + new RequestCanceledMiddlewareOptions(), |
| 71 | + new Mock<ILogger<RequestCanceledMiddleware>>().Object) |
| 72 | + .InvokeAsync(this.context, this.next) |
| 73 | + .ConfigureAwait(false); |
| 74 | + |
| 75 | + Assert.Equal(RequestCanceledMiddlewareOptions.ClientClosedRequest, this.context.Response.StatusCode); |
| 76 | + } |
| 77 | +} |
0 commit comments