Path: redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQReplyToBridgeTests.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/tests/redb.Route.Tests.RabbitMQ/RabbitMQReplyToBridgeTests.cs
----
using System.Text;
using RabbitMQ.Client;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.RabbitMQ;
namespace redb.Route.Tests.RabbitMQ;
///
/// A sender's reply-to does not travel across a bridge, as in Apache Camel's JMS: a message consumed with a reply-to and
/// forwarded by an ordinary send goes out without it, unless the producer sets preserveMessageQos=true. The
/// correlation id is carried; a request-reply producer sets its own reply-to. Expects RabbitMQ at localhost:5672.
///
[Trait("Category", "Integration")]
public sealed class RabbitMQReplyToBridgeTests
{
private const string Connection = "host=localhost&port=5672&username=admin&password=admin";
private static async Task ConnectAsync()
=> await new ConnectionFactory { HostName = "localhost", Port = 5672, UserName = "admin", Password = "admin" }.CreateConnectionAsync();
///
/// Publishes one message to an input queue with the given reply-to and correlation id, bridges it by a route to an
/// output queue, and returns the basic properties the forwarded message arrived with.
///
private static async Task Bridge(string producerOptions, string? senderReplyTo,
Action? routeStep = null)
{
var input = $"bridge-in-{Guid.NewGuid():N}";
var output = $"bridge-out-{Guid.NewGuid():N}";
var replies = $"bridge-replies-{Guid.NewGuid():N}";
await using var connection = await ConnectAsync();
await using var channel = await connection.CreateChannelAsync();
foreach (var queue in new[] { input, output, replies })
await channel.QueueDeclareAsync(queue, durable: true, exclusive: false, autoDelete: false);
try
{
var properties = new BasicProperties { CorrelationId = "corr-1" };
if (senderReplyTo is not null)
properties.ReplyTo = replies;
await channel.BasicPublishAsync(string.Empty, input, mandatory: false, properties,
Encoding.UTF8.GetBytes("bridged"));
await using var context = new RouteContext();
context.AddComponent(new RabbitMQComponent());
context.AddRoutes(r => r
.From($"rabbitmq://{input}?{Connection}")
.Process(ex => routeStep?.Invoke(ex))
.To($"rabbitmq://{output}?{Connection}{producerOptions}"));
await context.Start();
var deadline = DateTime.UtcNow.AddSeconds(20);
while (DateTime.UtcNow < deadline)
{
var got = await channel.BasicGetAsync(output, autoAck: true);
if (got is not null)
{
Encoding.UTF8.GetString(got.Body.Span).Should().Be("bridged");
return got.BasicProperties;
}
await Task.Delay(100);
}
throw new TimeoutException($"Nothing arrived on {output}.");
}
finally
{
foreach (var queue in new[] { input, output, replies })
await channel.QueueDeleteAsync(queue);
}
}
[Fact]
public async Task A_forwarded_message_goes_out_without_the_senders_reply_to_and_keeps_its_correlation_id()
{
var forwarded = await Bridge(producerOptions: "", senderReplyTo: "yes");
forwarded.ReplyTo.Should().BeNullOrEmpty("an ordinary send does not carry a sender's reply-to on");
forwarded.CorrelationId.Should().Be("corr-1");
}
[Fact]
public async Task With_preserveMessageQos_the_forwarded_message_keeps_the_senders_reply_to()
{
var forwarded = await Bridge(producerOptions: "&preserveMessageQos=true", senderReplyTo: "yes");
forwarded.ReplyTo.Should().StartWith("bridge-replies-");
forwarded.CorrelationId.Should().Be("corr-1");
}
[Theory]
[InlineData("", false)]
[InlineData("&preserveMessageQos=true", true)]
public async Task A_reply_to_header_set_by_the_route_follows_the_same_rule(string producerOptions, bool kept)
{
var forwarded = await Bridge(producerOptions, senderReplyTo: null,
routeStep: ex => ex.In.Headers[RmqHeaders.ReplyTo] = "route-named-replies");
if (kept)
forwarded.ReplyTo.Should().Be("route-named-replies");
else
forwarded.ReplyTo.Should().BeNullOrEmpty();
}
[Fact]
public async Task A_request_reply_producer_sets_its_own_reply_to_over_a_forwarded_one()
{
var queue = $"bridge-rpc-{Guid.NewGuid():N}";
await using var context = new RouteContext();
context.AddComponent(new RabbitMQComponent());
context.AddRoutes(r => r
.From($"rabbitmq://{queue}?{Connection}&declare=true&autoDelete=true")
.Process(ex => ex.Out = new Message("pong")));
await context.Start();
var producer = context.GetEndpoint($"rabbitmq://{queue}?{Connection}&declare=true&autoDelete=true&replyTo=true&timeout=15")
.CreateProducer();
await producer.Start();
try
{
var exchange = new Exchange(new Message("ping"));
exchange.In.Headers[RmqHeaders.ReplyTo] = "a-queue-of-another-system";
exchange.In.Headers[RmqHeaders.CorrelationId] = "corr-1";
await producer.Process(exchange);
exchange.HasOut.Should().BeTrue("the reply came back to the producer's own reply queue");
Encoding.UTF8.GetString((byte[])exchange.Out!.Body!).Should().Be("pong");
}
finally
{
await producer.Stop();
}
}
}