Skip to content

Commit 526bebc

Browse files
ariellourencoRehanSaeed
authored andcommitted
Cover the activity event in the unit test.
1 parent 760846d commit 526bebc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
namespace Boxed.AspNetCore.Test.Middleware;
22

33
using System;
4+
using System.Diagnostics;
5+
using System.Linq;
46
using System.Threading;
57
using System.Threading.Tasks;
68
using Boxed.AspNetCore.Middleware;
79
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Http.Features;
811
using Microsoft.Extensions.Logging;
912
using Moq;
1013
using Xunit;
@@ -82,17 +85,33 @@ await Assert.ThrowsAsync<TaskCanceledException>(() =>
8285
public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync()
8386
{
8487
using var cancellationTokenSource = new CancellationTokenSource();
88+
var activityFeature = new TestActivityFeature(new Activity("test"));
89+
90+
activityFeature.Activity.Start();
8591
cancellationTokenSource.Cancel();
92+
93+
this.context.Features.Set<IHttpActivityFeature>(activityFeature);
8694
this.context.RequestAborted = cancellationTokenSource.Token;
8795
this.next = x => Task.FromCanceled(cancellationTokenSource.Token);
8896

8997
await new RequestCanceledMiddleware(
9098
this.next,
9199
new RequestCanceledMiddlewareOptions(),
92100
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
93-
.InvokeAsync(this.context)
94-
.ConfigureAwait(false);
101+
.InvokeAsync(this.context)
102+
.ConfigureAwait(false);
95103

104+
Assert.Single(Activity.Current!.Events);
105+
Assert.Equal("Client cancelled the request.", Activity.Current!.Events.SingleOrDefault().Name);
96106
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; }
97116
}
98117
}

0 commit comments

Comments
 (0)