< Summary

Information
Class: IceRpc.RouterExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/RouterExtensions.cs
Tag: 275_13775359185
Line coverage
68%
Covered lines: 13
Uncovered lines: 6
Coverable lines: 19
Total lines: 62
Line coverage: 68.4%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
75%
Covered methods: 3
Total methods: 4
Method coverage: 75%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Map(...)100%11100%
Route(...)100%11100%
UseDispatchInformation(...)100%210%
UseFeature(...)100%11100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/RouterExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Features;
 4
 5namespace IceRpc;
 6
 7/// <summary>Provides extension methods for <see cref="Router" />.</summary>
 8public static class RouterExtensions
 9{
 10    /// <summary>Registers a route to a service that uses the service's default path as the route path. If there is an
 11    /// existing route at the same path, it is replaced.</summary>
 12    /// <typeparam name = "TService" > An interface with the DefaultServicePathAttribute attribute. The path of the
 13    /// mapped service corresponds to the value of this attribute.</typeparam>
 14    /// <param name="router">The router being configured.</param>
 15    /// <param name="service">The target service of this route.</param>
 16    /// <returns>The router being configured.</returns>
 17    /// <exception cref="InvalidOperationException">Thrown if <see cref="IDispatcher.DispatchAsync" /> was already
 18    /// called on this router.</exception>
 19    public static Router Map<TService>(this Router router, IDispatcher service)
 20        where TService : class =>
 121        router.Map(typeof(TService).GetDefaultServicePath(), service);
 22
 23    /// <summary>Creates a sub-router, configures this sub-router and mounts it (with
 24    /// <see cref="Router.Mount(string, IDispatcher)" />) at the given <c>prefix</c>.</summary>
 25    /// <param name="router">The router being configured.</param>
 26    /// <param name="prefix">The prefix of the route to the sub-router.</param>
 27    /// <param name="configure">A delegate that configures the new sub-router.</param>
 28    /// <returns>The new sub-router.</returns>
 29    /// <exception cref="FormatException">Thrown if <paramref name="prefix" /> is not a valid path.</exception>
 30    public static Router Route(this Router router, string prefix, Action<Router> configure)
 1631    {
 1632        ServiceAddress.CheckPath(prefix);
 1633        var subRouter = new Router($"{router.AbsolutePrefix}{prefix}");
 1634        configure(subRouter);
 1635        router.Mount(prefix, subRouter);
 1636        return subRouter;
 1637    }
 38
 39    /// <summary>Adds a middleware that creates and inserts the <see cref="IDispatchInformationFeature" /> feature
 40    /// in all requests.</summary>
 41    /// <param name="router">The router being configured.</param>
 42    /// <returns>The router being configured.</returns>
 43    public static Router UseDispatchInformation(this Router router) =>
 044        router.Use(next => new InlineDispatcher((request, cancellationToken) =>
 045        {
 046            request.Features = request.Features.With<IDispatchInformationFeature>(
 047                new DispatchInformationFeature(request));
 048            return next.DispatchAsync(request, cancellationToken);
 049        }));
 50
 51    /// <summary>Adds a middleware that sets a feature in all requests.</summary>
 52    /// <typeparam name="TFeature">The type of the feature.</typeparam>
 53    /// <param name="router">The router being configured.</param>
 54    /// <param name="feature">The value of the feature to set in all requests.</param>
 55    /// <returns>The router being configured.</returns>
 56    public static Router UseFeature<TFeature>(this Router router, TFeature feature) =>
 257        router.Use(next => new InlineDispatcher((request, cancellationToken) =>
 158        {
 159            request.Features = request.Features.With(feature);
 160            return next.DispatchAsync(request, cancellationToken);
 361        }));
 62}