|
1 | 1 | namespace Boxed.AspNetCore.Test.Middleware;
|
2 | 2 |
|
3 | 3 | using System;
|
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
4 | 6 | using System.Threading;
|
5 | 7 | using System.Threading.Tasks;
|
6 | 8 | using Boxed.AspNetCore.Middleware;
|
7 | 9 | using Microsoft.AspNetCore.Http;
|
| 10 | +using Microsoft.AspNetCore.Http.Features; |
8 | 11 | using Microsoft.Extensions.Logging;
|
9 | 12 | using Moq;
|
10 | 13 | using Xunit;
|
@@ -82,17 +85,33 @@ await Assert.ThrowsAsync<TaskCanceledException>(() =>
|
82 | 85 | public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync()
|
83 | 86 | {
|
84 | 87 | using var cancellationTokenSource = new CancellationTokenSource();
|
| 88 | + var activityFeature = new TestActivityFeature(new Activity("test")); |
| 89 | + |
| 90 | + activityFeature.Activity.Start(); |
85 | 91 | cancellationTokenSource.Cancel();
|
| 92 | + |
| 93 | + this.context.Features.Set<IHttpActivityFeature>(activityFeature); |
86 | 94 | this.context.RequestAborted = cancellationTokenSource.Token;
|
87 | 95 | this.next = x => Task.FromCanceled(cancellationTokenSource.Token);
|
88 | 96 |
|
89 | 97 | await new RequestCanceledMiddleware(
|
90 | 98 | this.next,
|
91 | 99 | new RequestCanceledMiddlewareOptions(),
|
92 | 100 | new Mock<ILogger<RequestCanceledMiddleware>>().Object)
|
93 |
| - .InvokeAsync(this.context) |
94 |
| - .ConfigureAwait(false); |
| 101 | + .InvokeAsync(this.context) |
| 102 | + .ConfigureAwait(false); |
95 | 103 |
|
| 104 | + Assert.Single(Activity.Current!.Events); |
| 105 | + Assert.Equal("Client cancelled the request.", Activity.Current!.Events.SingleOrDefault().Name); |
96 | 106 | Assert.Equal(RequestCanceledMiddlewareOptions.ClientClosedRequest, this.context.Response.StatusCode);
|
| 107 | + |
| 108 | + activityFeature.Activity.Dispose(); |
| 109 | + } |
| 110 | + |
| 111 | + private class TestActivityFeature : IHttpActivityFeature |
| 112 | + { |
| 113 | + public TestActivityFeature(Activity activity) => this.Activity = activity; |
| 114 | + |
| 115 | + public Activity Activity { get; set; } |
97 | 116 | }
|
98 | 117 | }
|
0 commit comments