|
4 | 4 | using Microsoft.AspNetCore.OpenApi;
|
5 | 5 | using Microsoft.Extensions.ApiDescriptions;
|
6 | 6 | using Microsoft.Extensions.DependencyInjection;
|
| 7 | +using Microsoft.Extensions.Hosting; |
| 8 | +using Microsoft.Extensions.Hosting.Internal; |
7 | 9 | using Microsoft.Extensions.Options;
|
8 | 10 | using Microsoft.OpenApi;
|
| 11 | +using Microsoft.OpenApi.Models; |
9 | 12 |
|
10 | 13 | public class OpenApiServiceCollectionExtensions
|
11 | 14 | {
|
@@ -189,4 +192,112 @@ public void AddOpenApi_WithDuplicateDocumentNames_UsesLastRegistration_ValidateO
|
189 | 192 | Assert.Equal(documentName, namedOption.DocumentName);
|
190 | 193 | Assert.Equal(OpenApiSpecVersion.OpenApi2_0, namedOption.OpenApiVersion);
|
191 | 194 | }
|
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void AddOpenApi_WithDefaultDocumentName_RegistersIOpenApiDocumentProviderInterface() |
| 198 | + { |
| 199 | + // Arrange |
| 200 | + var services = new ServiceCollection(); |
| 201 | + // Include dependencies for OpenApiDocumentService |
| 202 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 203 | + { |
| 204 | + EnvironmentName = Environments.Development, |
| 205 | + ApplicationName = "Test Application" |
| 206 | + }); |
| 207 | + services.AddLogging(); |
| 208 | + services.AddRouting(); |
| 209 | + |
| 210 | + // Act |
| 211 | + services.AddOpenApi(); |
| 212 | + var serviceProvider = services.BuildServiceProvider(); |
| 213 | + |
| 214 | + // Assert |
| 215 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(Microsoft.AspNetCore.OpenApi.OpenApiConstants.DefaultDocumentName); |
| 216 | + Assert.NotNull(documentProvider); |
| 217 | + Assert.IsType<OpenApiDocumentService>(documentProvider); |
| 218 | + } |
| 219 | + |
| 220 | + [Fact] |
| 221 | + public void AddOpenApi_WithCustomDocumentName_RegistersIOpenApiDocumentProviderInterface() |
| 222 | + { |
| 223 | + // Arrange |
| 224 | + var services = new ServiceCollection(); |
| 225 | + // Include dependencies for OpenApiDocumentService |
| 226 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 227 | + { |
| 228 | + EnvironmentName = Environments.Development, |
| 229 | + ApplicationName = "Test Application" |
| 230 | + }); |
| 231 | + services.AddLogging(); |
| 232 | + services.AddRouting(); |
| 233 | + var documentName = "v1"; |
| 234 | + |
| 235 | + // Act |
| 236 | + services.AddOpenApi(documentName); |
| 237 | + var serviceProvider = services.BuildServiceProvider(); |
| 238 | + |
| 239 | + // Assert |
| 240 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant()); |
| 241 | + Assert.NotNull(documentProvider); |
| 242 | + Assert.IsType<OpenApiDocumentService>(documentProvider); |
| 243 | + } |
| 244 | + |
| 245 | + [Fact] |
| 246 | + public async Task GetOpenApiDocumentAsync_ReturnsDocument() |
| 247 | + { |
| 248 | + // Arrange |
| 249 | + var services = new ServiceCollection(); |
| 250 | + // Include dependencies for OpenApiDocumentService |
| 251 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 252 | + { |
| 253 | + EnvironmentName = Environments.Development, |
| 254 | + ApplicationName = "Test Application" |
| 255 | + }); |
| 256 | + services.AddLogging(); |
| 257 | + services.AddRouting(); |
| 258 | + |
| 259 | + var documentName = "v1"; |
| 260 | + services.AddOpenApi(documentName); |
| 261 | + var serviceProvider = services.BuildServiceProvider(); |
| 262 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant()); |
| 263 | + |
| 264 | + // Act |
| 265 | + var document = await documentProvider.GetOpenApiDocumentAsync(); |
| 266 | + |
| 267 | + // Assert |
| 268 | + Assert.NotNull(document); |
| 269 | + Assert.IsType<OpenApiDocument>(document); |
| 270 | + |
| 271 | + // Verify basic document structure |
| 272 | + Assert.NotNull(document.Info); |
| 273 | + Assert.Equal($"Test Application | {documentName.ToLowerInvariant()}", document.Info.Title); |
| 274 | + Assert.Equal("1.0.0", document.Info.Version); |
| 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 | + } |
192 | 303 | }
|
0 commit comments