|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using Microsoft.AspNetCore.OpenApi; |
5 | | -using Microsoft.AspNetCore.Routing; |
6 | 5 | using Microsoft.Extensions.ApiDescriptions; |
7 | 6 | using Microsoft.Extensions.DependencyInjection; |
8 | | -using Microsoft.Extensions.DependencyInjection.Extensions; |
9 | 7 | using Microsoft.Extensions.Hosting; |
10 | 8 | using Microsoft.Extensions.Hosting.Internal; |
11 | 9 | using Microsoft.Extensions.Options; |
@@ -275,4 +273,31 @@ public async Task GetOpenApiDocumentAsync_ReturnsDocument() |
275 | 273 | Assert.Equal($"Test Application | {documentName.ToLowerInvariant()}", document.Info.Title); |
276 | 274 | Assert.Equal("1.0.0", document.Info.Version); |
277 | 275 | } |
| 276 | + |
| 277 | + [Fact] |
| 278 | + public async Task GetOpenApiDocumentAsync_HandlesCancellation() |
| 279 | + { |
| 280 | + // Arrange |
| 281 | + var services = new ServiceCollection(); |
| 282 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 283 | + { |
| 284 | + EnvironmentName = Environments.Development, |
| 285 | + ApplicationName = "Test Application" |
| 286 | + }); |
| 287 | + services.AddLogging(); |
| 288 | + services.AddRouting(); |
| 289 | + var documentName = "v1"; |
| 290 | + services.AddOpenApi(documentName); |
| 291 | + var serviceProvider = services.BuildServiceProvider(); |
| 292 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant()); |
| 293 | + |
| 294 | + using var cts = new CancellationTokenSource(); |
| 295 | + cts.Cancel(); |
| 296 | + |
| 297 | + // Act & Assert |
| 298 | + await Assert.ThrowsAsync<OperationCanceledException>(async () => |
| 299 | + { |
| 300 | + await documentProvider.GetOpenApiDocumentAsync(cts.Token); |
| 301 | + }); |
| 302 | + } |
278 | 303 | } |
0 commit comments