|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.OpenApi; |
| 3 | +using Microsoft.AspNetCore.Routing; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using Vulthil.xUnit; |
| 7 | + |
| 8 | +namespace Vulthil.SharedKernel.Api.Tests; |
| 9 | + |
| 10 | +public sealed class DependencyInjectionTests : BaseUnitTestCase |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void AddOpenApiServicesWithNoArgumentsRegistersOpenApiServicesForTheDefaultDocumentName() |
| 14 | + { |
| 15 | + // Arrange |
| 16 | + var services = new ServiceCollection(); |
| 17 | + |
| 18 | + // Act |
| 19 | + services.AddOpenApiServices(); |
| 20 | + using var provider = services.BuildServiceProvider(); |
| 21 | + var options = provider.GetRequiredService<IOptionsMonitor<OpenApiOptions>>().Get(DependencyInjection.DefaultDocumentName); |
| 22 | + |
| 23 | + // Assert |
| 24 | + Assert.Equal("v1", DependencyInjection.DefaultDocumentName); |
| 25 | + Assert.Equal(DependencyInjection.DefaultDocumentName, options.DocumentName); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void AddOpenApiServicesWithDocumentNameRegistersOpenApiServicesUnderThatName() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var services = new ServiceCollection(); |
| 33 | + const string documentName = "internal"; |
| 34 | + |
| 35 | + // Act |
| 36 | + services.AddOpenApiServices(documentName); |
| 37 | + using var provider = services.BuildServiceProvider(); |
| 38 | + var options = provider.GetRequiredService<IOptionsMonitor<OpenApiOptions>>().Get(documentName); |
| 39 | + |
| 40 | + // Assert |
| 41 | + Assert.Equal(documentName, options.DocumentName); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void AddOpenApiServicesWithConfigureInvokesTheConfigureCallbackForTheNamedDocument() |
| 46 | + { |
| 47 | + // Arrange |
| 48 | + var services = new ServiceCollection(); |
| 49 | + const string documentName = "internal"; |
| 50 | + var configureCalled = false; |
| 51 | + |
| 52 | + // Act |
| 53 | + services.AddOpenApiServices(documentName, _ => configureCalled = true); |
| 54 | + using var provider = services.BuildServiceProvider(); |
| 55 | + var options = provider.GetRequiredService<IOptionsMonitor<OpenApiOptions>>().Get(documentName); |
| 56 | + |
| 57 | + // Assert |
| 58 | + Assert.True(configureCalled); |
| 59 | + Assert.Equal(documentName, options.DocumentName); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void AddOpenApiServicesWithConfigureThrowsOnNullConfigure() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + var services = new ServiceCollection(); |
| 67 | + |
| 68 | + // Act & Assert |
| 69 | + Assert.Throws<ArgumentNullException>(() => services.AddOpenApiServices("v1", null!)); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void MapOpenApiEndpointsMapsTheOpenApiDocumentRouteAndReturnsAConventionBuilder() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + var builder = WebApplication.CreateBuilder(); |
| 77 | + builder.Services.AddOpenApiServices(); |
| 78 | + using var app = builder.Build(); |
| 79 | + |
| 80 | + // Act |
| 81 | + var conventionBuilder = app.MapOpenApiEndpoints(); |
| 82 | + |
| 83 | + // Assert |
| 84 | + Assert.NotNull(conventionBuilder); |
| 85 | + IEndpointRouteBuilder endpointRouteBuilder = app; |
| 86 | + var routePatterns = endpointRouteBuilder.DataSources |
| 87 | + .SelectMany(dataSource => dataSource.Endpoints) |
| 88 | + .OfType<RouteEndpoint>() |
| 89 | + .Select(endpoint => endpoint.RoutePattern.RawText) |
| 90 | + .ToArray(); |
| 91 | + Assert.Contains(routePatterns, pattern => pattern != null && pattern.Contains("openapi", StringComparison.OrdinalIgnoreCase)); |
| 92 | + } |
| 93 | +} |
0 commit comments