Path: redb-route/tests/redb.Route.Tests.Kafka/KafkaHeaderFilterStrategyTests.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.Kafka/KafkaHeaderFilterStrategyTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Kafka;
namespace redb.Route.Tests.Kafka;
///
/// headerFilterStrategy=#name on Kafka endpoints: on a producer it decides which exchange headers go into the
/// record, on a consumer which record headers come into the exchange; the record's content type and the consumer's
/// CamelKafka* metadata are set whatever the strategy says.
///
[Trait("Category", "Integration")]
public sealed class KafkaHeaderFilterStrategyTests
{
private const string Brokers = "localhost:29092";
private static async Task> RoundTrip(string producerOptions, string consumerOptions)
{
var topic = $"hdr-strategy-{Guid.NewGuid():N}";
var received = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using var ctx = new RouteContext();
ctx.AddComponent(new KafkaComponent());
ctx.AddToRegistry("correlationOnly", new DefaultHeaderFilterStrategy { OutFilter = ["Correlation-ID"], FilterOnMatch = false });
ctx.AddToRegistry("noSecrets", new DefaultHeaderFilterStrategy { InFilter = ["Authorization"] });
ctx.AddRoutes(r => r
.From($"kafka://{topic}?brokers={Brokers}&groupId=grp-{Guid.NewGuid():N}&autoOffsetReset=Earliest{consumerOptions}")
.Process(e => received.TrySetResult(e)));
await ctx.Start();
var exchange = new Exchange(new Message("body") { ContentType = "application/json" });
exchange.In.Headers["Correlation-ID"] = "c-42";
exchange.In.Headers["X-Corr"] = "c1";
exchange.In.Headers["Authorization"] = "Bearer secret";
var producer = ctx.GetEndpoint($"kafka://{topic}?brokers={Brokers}{producerOptions}").CreateProducer();
await producer.Start();
await producer.Process(exchange);
await producer.Stop();
var arrived = await received.Task.WaitAsync(TimeSpan.FromSeconds(30));
arrived.In.ContentType.Should().Be("application/json", "the content type crosses whatever the strategy says");
return new Dictionary(arrived.In.Headers);
}
[Fact]
public async Task OnAProducer_AWhitelistSendsOnlyTheNamedHeader()
{
var headers = await RoundTrip("&headerFilterStrategy=#correlationOnly", "");
headers["Correlation-ID"].Should().Be("c-42");
headers.Should().NotContainKey("X-Corr");
headers.Should().NotContainKey("Authorization");
headers.Should().ContainKey(KafkaHeaders.Topic, "the consumer's own metadata is set whatever the strategy says");
}
[Fact]
public async Task OnAConsumer_TheStrategyDecidesWhatComesIn()
{
var headers = await RoundTrip("", "&headerFilterStrategy=#noSecrets");
headers["X-Corr"].Should().Be("c1");
headers.Should().NotContainKey("Authorization", "the consumer's strategy drops it on the way in");
}
}