< Summary

Information
Class: IceRpc.RouterExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/RouterExtensions.cs
Tag: 1856_27024993493
Line coverage
70%
Covered lines: 14
Uncovered lines: 6
Coverable lines: 20
Total lines: 77
Line coverage: 70%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
75%
Covered methods: 3
Fully covered methods: 3
Total methods: 4
Method coverage: 75%
Full 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    /// <param name="router">The router being configured.</param>
 13    /// <param name="service">The target service of this route.</param>
 14    /// <returns>The router being configured.</returns>
 15    /// <exception cref="InvalidOperationException">Thrown if <see cref="IDispatcher.DispatchAsync" /> was already
 16    /// called on this router.</exception>
 17    /// <exception cref="ArgumentException">Thrown if the interface(s) implemented by <paramref name="service" /> do not
 18    /// have a <see cref="DefaultServicePathAttribute"/> attribute, or if <paramref name="service" /> implements
 19    /// multiple interfaces with a <see cref="DefaultServicePathAttribute"/> attribute.</exception>
 20    public static Router Map(this Router router, IDispatcher service) =>
 521        router.Map(service.GetType().GetDefaultServicePath(), service);
 22
 23    /// <summary>Registers a route to a service that uses the service's default path as the route path. If there is an
 24    /// existing route at the same path, it is replaced.</summary>
 25    /// <typeparam name="TService">A service interface with the <see cref="DefaultServicePathAttribute"/> attribute.
 26    /// The path of the mapped service corresponds to the value of this attribute.</typeparam>
 27    /// <param name="router">The router being configured.</param>
 28    /// <param name="service">The target service of this route.</param>
 29    /// <returns>The router being configured.</returns>
 30    /// <exception cref="InvalidOperationException">Thrown if <see cref="IDispatcher.DispatchAsync" /> was already
 31    /// called on this router.</exception>
 32    /// <exception cref="ArgumentException">Thrown if <typeparamref name="TService"/> does not have a
 33    /// <see cref="DefaultServicePathAttribute"/> attribute.</exception>
 34    public static Router Map<TService>(this Router router, IDispatcher service)
 35        where TService : class =>
 336        router.Map(typeof(TService).GetDefaultServicePath(), service);
 37
 38    /// <summary>Creates a sub-router, configures this sub-router and mounts it (with
 39    /// <see cref="Router.Mount(string, IDispatcher)" />) at the given <c>prefix</c>.</summary>
 40    /// <param name="router">The router being configured.</param>
 41    /// <param name="prefix">The prefix of the route to the sub-router.</param>
 42    /// <param name="configure">A delegate that configures the new sub-router.</param>
 43    /// <returns>The new sub-router.</returns>
 44    /// <exception cref="FormatException">Thrown if <paramref name="prefix" /> is not a valid path.</exception>
 45    public static Router Route(this Router router, string prefix, Action<Router> configure)
 846    {
 847        ServiceAddress.CheckPath(prefix);
 848        var subRouter = new Router($"{router.AbsolutePrefix}{prefix}");
 849        configure(subRouter);
 850        router.Mount(prefix, subRouter);
 851        return subRouter;
 852    }
 53
 54    /// <summary>Adds a middleware that creates and inserts the <see cref="IDispatchInformationFeature" /> feature
 55    /// in all requests.</summary>
 56    /// <param name="router">The router being configured.</param>
 57    /// <returns>The router being configured.</returns>
 58    public static Router UseDispatchInformation(this Router router) =>
 059        router.Use(next => new InlineDispatcher((request, cancellationToken) =>
 060        {
 061            request.Features = request.Features.With<IDispatchInformationFeature>(
 062                new DispatchInformationFeature(request));
 063            return next.DispatchAsync(request, cancellationToken);
 064        }));
 65
 66    /// <summary>Adds a middleware that sets a feature in all requests.</summary>
 67    /// <typeparam name="TFeature">The type of the feature.</typeparam>
 68    /// <param name="router">The router being configured.</param>
 69    /// <param name="feature">The value of the feature to set in all requests.</param>
 70    /// <returns>The router being configured.</returns>
 71    public static Router UseFeature<TFeature>(this Router router, TFeature feature) =>
 472        router.Use(next => new InlineDispatcher((request, cancellationToken) =>
 273        {
 274            request.Features = request.Features.With(feature);
 275            return next.DispatchAsync(request, cancellationToken);
 676        }));
 77}