< Summary

Information
Class: NanoRoute.RouterBase
Assembly: NanoRoute.dll
File(s): /home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Public/RouterBase.cs
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 98
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
RouterBase(...)70
Route(...)50

File(s)

/home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Public/RouterBase.cs

#LineLine coverage
 1/********************************************************************************
 2* RouterBase.cs                                                                 *
 3*                                                                               *
 4* Author: Denes Solti                                                           *
 5********************************************************************************/
 6using System;
 7using System.Net.Http;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace NanoRoute
 12{
 13    using Internals;
 14
 15    /// <summary>
 16    /// Provides shared configuration storage and request-pipeline execution for transport-specific router adapters.
 17    /// </summary>
 18    /// <typeparam name="TConfig">The immutable router configuration type used by the adapter.</typeparam>
 19    /// <remarks>
 20    /// Derive from this type when a custom transport needs to translate its native request model into an
 21    /// <see cref="HttpRequestMessage"/> while reusing NanoRoute's matching, value parsing, middleware, and handler
 22    /// pipeline. The constructor captures an immutable route snapshot, and derived adapters can call
 23    /// <see cref="Route(HttpRequestMessage, IServiceProvider, CancellationToken)"/> from their public entry point.
 24    /// </remarks>
 25    /// <example>
 26    /// <code>
 27    /// public sealed class MyRouter : RouterBase&lt;RouterConfig&gt;
 28    /// {
 29    ///     private MyRouter(RouteScopeBuilder routes, RouterConfig config)
 30    ///         : base(routes, config)
 31    ///     {
 32    ///     }
 33    ///
 34    ///     public async Task&lt;HttpResponseMessage&gt; Route(MyRequest request, IServiceProvider services, Cancellatio
 35    ///     {
 36    ///         using HttpRequestMessage httpRequest = request.ToHttpRequestMessage();
 37    ///         return await Route(httpRequest, services, cancellation).ConfigureAwait(false);
 38    ///     }
 39    /// }
 40    /// </code>
 41    /// </example>
 42    public abstract class RouterBase<TConfig> where TConfig : RouterConfig
 43    {
 44        private readonly RequestPipeline _pipeline;
 45
 46        /// <summary>
 47        /// Initializes a router from the current route scope and configuration.
 48        /// </summary>
 49        /// <param name="routes">The route scope whose registered routes are captured by the router.</param>
 50        /// <param name="config">The configuration assigned to the router.</param>
 51        /// <exception cref="ArgumentNullException">
 52        /// Thrown when <paramref name="routes"/> or <paramref name="config"/> is <see langword="null"/>.
 53        /// </exception>
 254        protected RouterBase(RouteScopeBuilder routes, TConfig config)
 255        {
 256            Ensure.NotNull(routes);
 257            Ensure.NotNull(config);
 58
 259            Config = config;
 260            _pipeline = new RequestPipeline(routes.CreateSnapshot(), config.MatchingPrecedence);
 261        }
 62
 63        /// <summary>
 64        /// Gets the configuration assigned to this router instance.
 65        /// </summary>
 66        public TConfig Config { get; }
 67
 68        /// <summary>
 69        /// Routes a single HTTP request message and returns the produced response.
 70        /// </summary>
 71        /// <param name="request">The request to process.</param>
 72        /// <param name="services">The service provider exposed to handlers through <see cref="RequestContext.Services"/
 73        /// <param name="cancellation">A token that can cancel request processing.</param>
 74        /// <returns>The response produced by the matching handlers.</returns>
 75        /// <exception cref="ArgumentNullException">
 76        /// Thrown when <paramref name="request"/> or <paramref name="services"/> is <see langword="null"/>.
 77        /// </exception>
 78        /// <exception cref="InvalidOperationException">Thrown when the request uses an unsupported HTTP method.</except
 79        /// <exception cref="HttpRequestException">
 80        /// Thrown when no handler matches the request path or a matched handler signals an HTTP failure that is not
 81        /// translated by middleware.
 82        /// </exception>
 83        /// <exception cref="OperationCanceledException">
 84        /// Thrown when the caller cancels <paramref name="cancellation"/>.
 85        /// </exception>
 86        /// <remarks>
 87        /// The returned <see cref="HttpResponseMessage"/> is not disposed by the router. Callers should dispose it
 88        /// after reading the response body.
 89        /// </remarks>
 90        protected Task<HttpResponseMessage> Route(HttpRequestMessage request, IServiceProvider services, CancellationTok
 291        {
 292            Ensure.NotNull(request);
 293            Ensure.NotNull(services);
 94
 295            return _pipeline.ExecuteAsync(request, services, cancellation);
 296        }
 97    }
 98}