Path: redb-route/redb.Route.TestKit/RouteContextAdviceExtensions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.TestKit/RouteContextAdviceExtensions.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
namespace redb.Route.TestKit;
///
/// AdviceWith entry points. Advice runs in the window between
/// and : the builders are configured once, the chosen definitions are
/// rewritten in place, and Start() compiles them as rewritten (it does not re-run Configure()).
///
public static class RouteContextAdviceExtensions
{
///
/// Rewrites the route with id (an explicit RouteId("..."), or the
/// endpoint-derived id of an unnamed route) before the context starts.
///
/// The context is already started, or no route has that id.
public static RouteContext AdviceRoute(this RouteContext context, string routeId, Action advice)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(routeId);
ArgumentNullException.ThrowIfNull(advice);
var routes = BuiltRoutes(context);
var match = routes.FirstOrDefault(r => string.Equals(r.Id, routeId, StringComparison.OrdinalIgnoreCase));
if (match.Definition is null)
throw new InvalidOperationException(
$"AdviceWith: no route with id '{routeId}'. Known routes: {string.Join(", ", routes.Select(r => $"'{r.Id}'"))}.");
advice(new AdviceWithBuilder(match.Definition) { RouteId = match.Id });
return context;
}
/// Applies the same advice to every route of the context (e.g. a => a.MockEndpoints("kafka://*")).
public static RouteContext AdviceAllRoutes(this RouteContext context, Action advice)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(advice);
foreach (var (id, definition) in BuiltRoutes(context))
advice(new AdviceWithBuilder(definition) { RouteId = id });
return context;
}
/// Configures the builders once (idempotent) and lists their routes with resolved ids.
private static List<(string Id, RouteDefinition Definition)> BuiltRoutes(RouteContext context)
{
if (context.IsStarted)
throw new InvalidOperationException("AdviceWith must run before RouteContext.Start(): routes are compiled from their definitions at start.");
// Build every builder that has not been built yet (one added after an earlier advice included);
// a built one keeps its definitions, and with them any advice already applied.
foreach (var builder in context.RouteBuilders)
{
if (!builder.IsBuilt)
builder.InternalBuild(context);
}
context.DefinitionsPrebuilt = true;
var routes = new List<(string, RouteDefinition)>();
foreach (var builder in context.RouteBuilders)
{
foreach (var definition in builder.Definitions)
{
var fromUri = definition.GetFromUri()
?? throw new InvalidOperationException($"Route '{definition.GetRouteId() ?? "(unnamed)"}' has no From() endpoint.");
// Same fallback the context uses when compiling an unnamed route.
var id = definition.GetRouteId() ?? RouteIdFactory.ForEndpoint(EndpointUriParser.Parse(fromUri));
routes.Add((id, definition));
}
}
return routes;
}
}