Path: redb-route/tests/redb.Route.Tests.Amqp/AmqpHeaderFilterStrategyTests.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.Amqp/AmqpHeaderFilterStrategyTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Amqp;
namespace redb.Route.Tests.Amqp;
///
/// headerFilterStrategy=#name on AMQP 1.0 endpoints: on a producer it decides which exchange headers go out
/// with the request and which application-properties of the reply come back, on a consumer which application-properties
/// come in and which headers go out with the reply. Standard property names and the consumer's redbAmqp.* metadata stay
/// outside the strategy.
///
[Trait("Category", "Integration")]
public sealed class AmqpHeaderFilterStrategyTests
{
private static string Uri(string queue, string? extra = null)
=> $"amqp://{queue}?host=localhost&port=5673&user=admin&password=admin{extra}";
private static RouteContext Context()
{
var ctx = new RouteContext();
ctx.AddComponent(new AmqpComponent());
ctx.AddToRegistry("correlationOnly", new DefaultHeaderFilterStrategy { OutFilter = ["Correlation-ID"], FilterOnMatch = false });
ctx.AddToRegistry("noSecrets", new DefaultHeaderFilterStrategy { InFilter = ["Authorization"] });
return ctx;
}
private static async Task> Deliver(string producerOptions, string consumerOptions)
{
var queue = $"hdr-strategy-{Guid.NewGuid():N}";
var received = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using var ctx = Context();
ctx.AddRoutes(r => r.From(Uri(queue, consumerOptions)).Process(e => received.TrySetResult(e)));
await ctx.Start();
var exchange = new Exchange(new Message("body"));
exchange.In.Headers["Correlation-ID"] = "c-42";
exchange.In.Headers["X-Corr"] = "c1";
exchange.In.Headers["Authorization"] = "Bearer secret";
exchange.In.Headers["MessageId"] = "m-1";
var producer = ctx.GetEndpoint(Uri(queue, producerOptions)).CreateProducer();
await producer.Start();
await producer.Process(exchange);
await producer.Stop();
return new Dictionary((await received.Task.WaitAsync(TimeSpan.FromSeconds(30))).In.Headers);
}
private static async Task> Reply(string producerOptions, string consumerOptions)
{
var queue = $"hdr-strategy-rpc-{Guid.NewGuid():N}";
await using var ctx = Context();
ctx.AddRoutes(r => r.From(Uri(queue, consumerOptions)).Process(e =>
{
e.In.Headers.Clear();
e.In.Headers["Correlation-ID"] = "c-42";
e.In.Headers["X-Reply"] = "r1";
e.In.Headers["Authorization"] = "Bearer secret";
e.In.Body = "reply";
}));
await ctx.Start();
var producer = ctx.GetEndpoint(Uri(queue, "&replyTo=true&timeout=15" + producerOptions)).CreateProducer();
await producer.Start();
var exchange = new Exchange(new Message("request"));
await producer.Process(exchange);
await producer.Stop();
return new Dictionary((exchange.HasOut ? exchange.Out! : exchange.In).Headers);
}
[Fact]
public async Task OnAProducer_AWhitelistSendsOnlyTheNamedHeader()
{
var headers = await Deliver("&headerFilterStrategy=#correlationOnly", "");
headers["Correlation-ID"].Should().Be("c-42");
headers.Should().NotContainKey("X-Corr");
headers.Should().NotContainKey("Authorization");
headers["MessageId"].Should().Be("m-1", "a standard property name goes into the property whatever the strategy says");
headers.Should().ContainKey(AmqpHeaders.Address, "the consumer's own metadata is set whatever the strategy says");
}
[Fact]
public async Task OnAConsumer_TheStrategyDecidesWhatComesIn()
{
var headers = await Deliver("", "&headerFilterStrategy=#noSecrets");
headers["X-Corr"].Should().Be("c1");
headers.Should().NotContainKey("Authorization", "the consumer's strategy drops it on the way in");
}
[Fact]
public async Task OnAReplyingConsumer_TheStrategyDecidesWhatTheReplyCarries()
{
var headers = await Reply("", "&headerFilterStrategy=#correlationOnly");
headers["Correlation-ID"].Should().Be("c-42");
headers.Should().NotContainKey("X-Reply");
headers.Should().NotContainKey("Authorization");
}
[Fact]
public async Task OnARequestReplyProducer_TheStrategyDecidesWhatComesBackWithTheReply()
{
var headers = await Reply("&headerFilterStrategy=#noSecrets", "");
headers["X-Reply"].Should().Be("r1");
headers.Should().NotContainKey("Authorization", "the producer's strategy drops it on the way back in");
}
}