Path: redb-route/tests/redb.Route.Tests/Expressions/XPathParameterTests.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/XPathParameterTests.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 value from the message takes part in an XPath query by being bound to a
/// $name variable, never by being pasted into the query text.
///
/// Concatenation is what Apache Camel offers as allowSimple, and it is the reason XPath
/// injection has an OWASP entry and a CodeQL query of its own. Binding is the settled answer
/// everywhere else: JAXP's XPathVariableResolver, XQuery's declare variable $x
/// external, .NET's XsltArgumentList, SQL prepared statements.
///
///
public class XPathParameterTests
{
private const string Orders =
"""
1020
""";
private static IExchange Exchange(params (string Key, object? Value)[] headers)
{
var exchange = new Exchange(new Message(Orders));
foreach (var (key, value) in headers)
exchange.In.Headers[key] = value;
return exchange;
}
[Fact]
public void A_bound_value_selects_the_matching_node()
{
var total = new XPathExpr("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")))
.Evaluate(Exchange(("wanted", "B-2")));
total.Should().Be("20");
}
[Fact]
public void The_same_expression_answers_differently_for_different_messages()
{
var expression = new XPathExpr("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")));
expression.Evaluate(Exchange(("wanted", "A-1"))).Should().Be("10");
expression.Evaluate(Exchange(("wanted", "B-2"))).Should().Be("20");
}
[Fact]
public void A_number_is_bound_as_a_number()
{
var id = new XPathExpr("/orders/order[total > $min]/@id")
.WithParameters(("min", new HeaderExpression("min")))
.Evaluate(Exchange(("min", 15)));
id.Should().Be("B-2", "an int must reach XPath as a number, not as the text '15'");
}
[Fact]
public void A_decimal_is_bound_invariantly()
{
// A comma decimal separator would make the same route match different documents on
// different machines, which is the class of bug the template engine already ruled out.
var id = new XPathExpr("/orders/order[total > $min]/@id")
.WithParameters(("min", new HeaderExpression("min")))
.Evaluate(Exchange(("min", 15.5m)));
id.Should().Be("B-2");
}
[Fact]
public void Several_parameters_bind_together()
{
var id = new XPathExpr("/orders/order[total > $min and total < $max]/@id")
.WithParameters(("min", new HeaderExpression("min")), ("max", new HeaderExpression("max")))
.Evaluate(Exchange(("min", 5), ("max", 15)));
id.Should().Be("A-1");
}
[Fact]
public void Bindings_added_in_two_calls_are_both_kept()
{
var id = new XPathExpr("/orders/order[total > $min and total < $max]/@id")
.WithParameters(("min", new HeaderExpression("min")))
.WithParameters(("max", new HeaderExpression("max")))
.Evaluate(Exchange(("min", 5), ("max", 15)));
id.Should().Be("A-1");
}
// ── The point of the exercise ──
[Fact]
public void A_value_carrying_XPath_syntax_stays_a_value()
{
var expression = new XPathExpr("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")));
var hostile = expression.Evaluate(Exchange(("wanted", "B-2' or '1'='1")));
hostile.Should().BeNull(
"the value is compared, not parsed: no order has that literal id, so nothing matches");
}
[Fact]
public void A_parameterised_expression_refuses_the_template_form_instead_of_dropping_its_bindings()
{
// Review finding: the first cut serialised to ${xpath(path)} — the constant path was the
// point being made, but the serialised form had lost its bindings, so evaluating it would
// fail on the unbound $id. The same refusal the result type already gets applies here.
var expression = new XPathExpr("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")));
var act = () => expression.ToTemplateString();
act.Should().Throw().WithMessage("*bindings would be dropped*");
}
[Fact]
public void A_sourced_expression_refuses_the_template_form_instead_of_silently_reading_the_body()
{
var expression = new XPathExpr("/orders/order[1]/total").From(new HeaderExpression("doc"));
var act = () => expression.ToTemplateString();
act.Should().Throw().WithMessage("*read the body instead*");
}
// ── Composition and refusals ──
[Fact]
public void Parameters_compose_with_a_source_and_a_result_type()
{
var exchange = new Exchange(new Message(""));
exchange.In.Headers["doc"] = Orders;
exchange.In.Headers["wanted"] = "A-1";
var value = new XPathExpr("/orders/order[@id=$id]/total", XPathResult.String)
.From(new HeaderExpression("doc"))
.WithParameters(("id", new HeaderExpression("wanted")))
.Evaluate