Path: redb-route/redb.Route.TestKit/RouteContextSendExtensions.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/RouteContextSendExtensions.cs ---- using redb.Route.Abstractions; using redb.Route.Components; using redb.Route.Core; namespace redb.Route.TestKit; /// /// One-line sends for tests: replaces the GetEndpoint / CreateProducer / Start / Process /// plumbing with , and , /// plus to reach the mock standing in for an endpoint. /// Each call uses a short-lived , so producers are started and stopped /// around the send and nothing leaks between tests. /// public static class RouteContextSendExtensions { /// Sends to (in-only). public static async Task SendBody(this IRouteContext context, string endpointUri, object? body, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(context); using var template = Template(context); var exchange = Exchange.Create(new Message(body), null); try { await template.SendAsync(endpointUri, exchange, ct).ConfigureAwait(false); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// Sends with the given headers to (in-only). public static async Task SendBodyAndHeaders(this IRouteContext context, string endpointUri, object? body, IEnumerable> headers, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(context); ArgumentNullException.ThrowIfNull(headers); using var template = Template(context); var exchange = Exchange.Create(WithHeaders(body, headers), null); try { await template.SendAsync(endpointUri, exchange, ct).ConfigureAwait(false); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// Sends with a single header to (in-only). public static Task SendBodyAndHeader(this IRouteContext context, string endpointUri, object? body, string headerName, object? headerValue, CancellationToken ct = default) => context.SendBodyAndHeaders(endpointUri, body, [new KeyValuePair(headerName, headerValue)], ct); /// Request-reply: sends and returns the reply body converted to . public static async Task RequestBody(this IRouteContext context, string endpointUri, object? body, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(context); using var template = Template(context); var exchange = Exchange.Create(new Message(body), null); try { var reply = await template.RequestAsync(endpointUri, exchange, ct).ConfigureAwait(false); return ReplyAs(reply, endpointUri); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// Request-reply with headers; returns the reply body converted to . public static async Task RequestBodyAndHeaders(this IRouteContext context, string endpointUri, object? body, IEnumerable> headers, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(context); ArgumentNullException.ThrowIfNull(headers); using var template = Template(context); var exchange = Exchange.Create(WithHeaders(body, headers), null); try { var reply = await template.RequestAsync(endpointUri, exchange, ct).ConfigureAwait(false); return ReplyAs(reply, endpointUri); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// /// The standing in for : either a mock://name /// URI or the original endpoint URI that replaced /// (kafka://orders-vip resolves to mock://kafka:orders-vip, see ). /// The endpoint is created on first access, so expectations can be set before any message arrives. /// public static MockEndpoint Mock(this IRouteContext context, string uri) { ArgumentNullException.ThrowIfNull(context); var endpoint = context.GetEndpoint(MockUri.For(uri)); return endpoint as MockEndpoint ?? throw new InvalidOperationException($"'{uri}' resolved to '{endpoint.GetType().Name}', not a MockEndpoint."); } private static ProducerTemplate Template(IRouteContext context) { var template = new ProducerTemplate(context); template.Start(); return template; } private static Message WithHeaders(object? body, IEnumerable> headers) { var message = new Message(body); foreach (var (key, value) in headers) message.Headers[key] = value; return message; } /// /// The reply body converted to , taken before the helper ends the exchange. A body that reads /// from resources of the exchange () would be dead once the helper returns, so it is /// refused; a nullable converts to its underlying type. /// private static T? ReplyAs(IExchange reply, string endpointUri) { var replyBody = reply.Out?.Body ?? reply.In.Body; if (replyBody is IExchangeBoundBody) throw new InvalidOperationException( $"The reply of '{EndpointUri.Sanitize(endpointUri)}' is a {ReadableName(replyBody.GetType())} that reads from " + "resources of its exchange, and the helper ends the exchange before it returns. Use " + "ProducerTemplate.RequestAsync, read the body, then dispose the exchange."); return replyBody switch { null => default, T typed => typed, _ => (T)RouteDecimal.ChangeType(replyBody, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), System.Globalization.CultureInfo.InvariantCulture), }; } /// StreamedQueryResult<Order> rather than the runtime name StreamedQueryResult`1. private static string ReadableName(Type type) => type.IsGenericType ? $"{type.Name[..type.Name.IndexOf('`')]}<{string.Join(", ", type.GetGenericArguments().Select(ReadableName))}>" : type.Name; }