< Summary

Information
Class: ArturRios.Util.WebApi.Configuration.ApplicationBuilderExtensions
Assembly: ArturRios.Util.WebApi
File(s): /home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Configuration/ApplicationBuilderExtensions.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 91
Line coverage: 100%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage
100%
Covered methods: 3
Fully covered methods: 3
Total methods: 3
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseStandardMiddlewares(...)100%44100%
UseWebApiMiddlewares(...)100%44100%
UseWebApiSwagger(...)66.66%66100%

File(s)

/home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Configuration/ApplicationBuilderExtensions.cs

#LineLine coverage
 1using ArturRios.Util.WebApi.Middleware;
 2using ArturRios.Util.WebApi.Security.Middleware;
 3using Microsoft.AspNetCore.Builder;
 4using Microsoft.Extensions.DependencyInjection;
 5using Swashbuckle.AspNetCore.Swagger;
 6using TokenAuthenticationOptions = ArturRios.Util.WebApi.Security.Configuration.AuthenticationOptions;
 7
 8namespace ArturRios.Util.WebApi.Configuration;
 9
 10/// <summary>Pipeline-side steps of the standard web API setup. <see cref="WebApiStartup"/> runs them in order, and
 11/// each one can be used on its own with a plain <see cref="WebApplication"/>.</summary>
 12public static class ApplicationBuilderExtensions
 13{
 14    /// <param name="app">The application builder.</param>
 15    extension(IApplicationBuilder app)
 16    {
 17        /// <summary>Adds the standard middlewares, in order: ASP.NET Core's forwarded headers (a no-op until
 18        /// <c>ForwardedHeadersOptions</c> is configured, so the client IP behind a trusted proxy is the real one),
 19        /// <see cref="TraceActivityMiddleware"/>,
 20        /// <see cref="ExceptionMiddleware"/>, Swagger (see <see cref="UseWebApiSwagger"/>), CORS when
 21        /// <paramref name="corsPolicy"/> is given, then <see cref="AuthenticationMiddleware"/> when token authenticatio
 22        /// was registered with <c>AddTokenAuthentication</c>. Swagger and CORS come before authentication, so the
 23        /// Swagger UI and CORS preflight requests are never rejected for lacking a token.</summary>
 24        /// <param name="corsPolicy">The name of a CORS policy registered with <c>AddCors</c>, or <see langword="null"/>
 25        /// for no CORS.</param>
 26        /// <returns>The same <paramref name="app"/> instance, for chaining.</returns>
 27        public IApplicationBuilder UseStandardMiddlewares(string? corsPolicy = null)
 28        {
 2029            app.UseForwardedHeaders();
 2030            app.UseMiddleware<TraceActivityMiddleware>();
 2031            app.UseMiddleware<ExceptionMiddleware>();
 2032            app.UseWebApiSwagger();
 33
 2034            if (!string.IsNullOrWhiteSpace(corsPolicy))
 35            {
 136                app.UseCors(corsPolicy);
 37            }
 38
 2039            if (app.ApplicationServices.GetService<TokenAuthenticationOptions>() is not null)
 40            {
 341                app.UseMiddleware<AuthenticationMiddleware>();
 42            }
 43
 2044            return app;
 45        }
 46
 47        /// <summary>Registers each given middleware type on the pipeline, in order.</summary>
 48        /// <param name="middlewares">The middleware types to register. Each must derive from <see cref="WebApiMiddlewar
 49        /// <returns>The same <paramref name="app"/> instance, for chaining.</returns>
 50        /// <exception cref="ArgumentException">A type does not derive from <see cref="WebApiMiddleware"/>.</exception>
 51        public IApplicationBuilder UseWebApiMiddlewares(params IEnumerable<Type> middlewares)
 52        {
 2053            ArgumentNullException.ThrowIfNull(middlewares);
 54
 2055            var types = middlewares.ToArray();
 2256            var invalid = types.FirstOrDefault(middleware => !middleware.IsSubclassOf(typeof(WebApiMiddleware)));
 57
 2058            if (invalid is not null)
 59            {
 160                throw new ArgumentException(
 161                    $"{invalid.FullName} does not derive from {nameof(WebApiMiddleware)}.", nameof(middlewares));
 62            }
 63
 4064            foreach (var middleware in types)
 65            {
 166                app.UseMiddleware(middleware);
 67            }
 68
 1969            return app;
 70        }
 71
 72        /// <summary>Serves the Swagger JSON endpoint and UI when the Swagger generator was registered, typically by
 73        /// <see cref="WebApplicationBuilderExtensions.AddWebApiSwagger"/> in an environment that allows it; otherwise d
 74        /// nothing, so the generator and the UI can never disagree.</summary>
 75        /// <returns>The same <paramref name="app"/> instance, for chaining.</returns>
 76        public IApplicationBuilder UseWebApiSwagger()
 77        {
 2078            var isService = app.ApplicationServices.GetService<IServiceProviderIsService>();
 79
 2080            if (isService?.IsService(typeof(ISwaggerProvider)) is not true)
 81            {
 1682                return app;
 83            }
 84
 485            app.UseSwagger();
 486            app.UseSwaggerUI();
 87
 488            return app;
 89        }
 90    }
 91}