Path: redb-route/redb.Route/Components/MockResponse.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/Components/MockResponse.cs ---- using redb.Route.Abstractions; namespace redb.Route.Components; /// /// Scripted behaviour of a for one message () /// or for every message (): set the reply body or a header, /// delay, throw, or run arbitrary code. Actions run in the order they were added, after the exchange /// has been recorded, so a throwing mock still counts the message it rejected. /// /// This is what makes mock:// usable behind Enrich / PollEnrich / request-reply: /// the reply body is whatever left in the exchange. /// /// public sealed class MockResponse { private readonly List> _actions = []; internal MockResponse(int? messageNumber) { MessageNumber = messageNumber; } /// 1-based ordinal of the message this response applies to; null = every message. public int? MessageNumber { get; } /// Replaces the message body (the reply seen by Enrich / request-reply callers). public MockResponse SetBody(object? body) { _actions.Add((exchange, _) => { exchange.In.Body = body; return Task.CompletedTask; }); return this; } /// Sets a header on the message. public MockResponse SetHeader(string name, object? value) { ArgumentException.ThrowIfNullOrWhiteSpace(name); _actions.Add((exchange, _) => { exchange.In.Headers[name] = value; return Task.CompletedTask; }); return this; } /// Waits before continuing — simulates a slow downstream system. public MockResponse Delay(TimeSpan delay) { if (delay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(delay)); _actions.Add((_, ct) => Task.Delay(delay, ct)); return this; } /// Throws the given exception — simulates a failing downstream system. public MockResponse Throw(Exception exception) { ArgumentNullException.ThrowIfNull(exception); _actions.Add((_, _) => throw exception); return this; } /// Throws a new with the given message. public MockResponse Throw(string message) where TException : Exception { ArgumentNullException.ThrowIfNull(message); _actions.Add((_, _) => throw (Exception)Activator.CreateInstance(typeof(TException), message)!); return this; } /// Runs arbitrary synchronous code against the exchange. public MockResponse Do(Action action) { ArgumentNullException.ThrowIfNull(action); _actions.Add((exchange, _) => { action(exchange); return Task.CompletedTask; }); return this; } /// Runs arbitrary asynchronous code against the exchange. public MockResponse Do(Func action) { ArgumentNullException.ThrowIfNull(action); _actions.Add(action); return this; } internal bool AppliesTo(int messageNumber) => MessageNumber is null || MessageNumber == messageNumber; internal async Task ApplyAsync(IExchange exchange, CancellationToken ct) { foreach (var action in _actions) await action(exchange, ct).ConfigureAwait(false); } }