Path: redb-route/tests/redb.Route.Tests/Expressions/ExpressionSourceTests.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/Expressions/ExpressionSourceTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using XPathExpr = redb.Route.Expressions.XPathExpression;
namespace redb.Route.Tests.Expressions;
///
/// A query language reads the message body by default and whatever the route names otherwise.
///
/// Without a source, XML that arrived in a header has to be moved into the body before it can be
/// queried — an extra step that also damages the body for the rest of the route.
///
///
/// In the language the source is a second argument with the path staying first:
/// xpath('/order/id', header.payload). Camel writes xpath(input,exp), source first,
/// which makes the first argument mean different things at one and two arguments; we do not copy
/// that, and the divergence is deliberate.
///
///
[Collection("ExpressionResolver")]
public class ExpressionSourceTests : IDisposable
{
private const string HeaderXml = "H-1yes";
private const string BodyXml = "B-1";
private const string HeaderJson = """{"id":"J-1","vip":true}""";
public ExpressionSourceTests() => ExpressionResolver.ClearAllCaches();
public void Dispose()
{
ExpressionResolver.ClearAllCaches();
GC.SuppressFinalize(this);
}
private static IExchange Exchange()
{
var exchange = new Exchange(new Message(BodyXml));
exchange.In.Headers["payload"] = HeaderXml;
exchange.In.Headers["json"] = HeaderJson;
exchange.Properties["doc"] = HeaderJson;
return exchange;
}
// ── Object form ──
[Fact]
public void XPath_reads_the_named_source_instead_of_the_body()
{
var exchange = Exchange();
new XPathExpr("/order/id").Evaluate(exchange).Should().Be("B-1");
new XPathExpr("/order/id").From(new HeaderExpression("payload"))
.Evaluate(exchange).Should().Be("H-1");
}
[Fact]
public void JsonPath_reads_the_named_source_instead_of_the_body()
{
var exchange = Exchange();
new JsonPathExpression("$.id").From(new PropertyExpression("doc"))
.Evaluate(exchange).Should().Be("J-1");
}
[Fact]
public void Reading_a_source_leaves_the_body_alone()
{
var exchange = Exchange();
new XPathExpr("/order/id").From(new HeaderExpression("payload")).Evaluate(exchange);
exchange.In.Body.Should().Be(BodyXml, "querying a header must not cost the route its body");
}
[Fact]
public void From_returns_a_new_expression_and_leaves_the_original_reading_the_body()
{
var onBody = new XPathExpr("/order/id");
var onHeader = onBody.From(new HeaderExpression("payload"));
var exchange = Exchange();
onHeader.Evaluate(exchange).Should().Be("H-1");
onBody.Evaluate(exchange).Should().Be("B-1");
}
[Fact]
public void The_source_composes_with_the_result_type()
{
var exchange = Exchange();
new XPathExpr("/order/id", XPathResult.String).From(new HeaderExpression("payload"))
.Evaluate