Path: redb-route/tests/redb.Route.Tests.Xml/StructuredEndpointTests.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.Xml/StructuredEndpointTests.cs
----
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
///
/// The structured endpoint form — one generic normalization into the same
/// URI string a hand-written address would be, for every scheme, with zero per-connector code.
/// Exact-string assertions on the normalizer plus behavioral proof that the canon is identical
/// (a structured address and a hand-written URI meet at the same endpoint).
///
public class StructuredEndpointTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
GC.SuppressFinalize(this);
}
// ── the normalizer, exact strings ────────────────────────────────────────
private string? Resolve(string stepXml, out List errors)
{
var ctx = new XmlParseContext(_context, ElementRegistry.CreateDefault(), "probe.xml");
var step = XElement.Parse(stepXml, LoadOptions.SetLineInfo);
var uri = StructuredEndpoint.Resolve(step, ctx, out _);
errors = [.. ctx.Errors];
return uri;
}
[Fact]
public void Attributes_BecomeQueryOptions_PathAttributeIsThePath()
{
var uri = Resolve("""
""", out var errors);
errors.Should().BeEmpty();
uri.Should().Be("kafka://orders?key=${header.tripId}&groupId=orders-svc");
}
[Fact]
public void CdataPath_ParamFamily_AndTextOption_AllNormalizeIntoOptions()
{
var uri = Resolve("""
""", out var errors);
errors.Should().BeEmpty();
uri.Should().Be("sql://SELECT * FROM outbox WHERE sent = 0" +
"?dataSource=#main-db" +
"¶m.login=${header.login}" +
"&onSuccess=UPDATE outbox SET sent = 1 AND flags = 2");
}
[Fact]
public void AmpersandAndPercent_InOptionValues_SurviveTheEngineRoundTrip()
{
var uri = Resolve("""
""", out var errors);
errors.Should().BeEmpty();
// Only the two characters the query parse cannot survive are encoded…
uri.Should().Be("sql://q?onSuccess=UPDATE t SET n = a %26 b WHERE p LIKE '%25x%25'");
// …and the engine's own parser hands the original text back.
EndpointUriParser.Parse(uri!).RawParameters["onSuccess"]
.Should().Be("UPDATE t SET n = a & b WHERE p LIKE '%x%'");
}
[Fact]
public void UriAndChild_Together_IsASchemaError()
{
Resolve("""""", out var errors);
errors.Should().ContainSingle().Which.Should().Contain("either uri= or a structured endpoint child");
}
[Fact]
public void TwoEndpointChildren_IsASchemaError()
{
Resolve("""""", out var errors);
errors.Should().ContainSingle().Which.Should().Contain("exactly one endpoint child");
}
[Fact]
public void NeitherUriNorChild_IsASchemaError()
{
Resolve("""""", out var errors);
errors.Should().ContainSingle().Which.Should().Contain("requires the uri attribute or one endpoint child");
}
[Fact]
public void PathAttributeAndContent_Together_IsASchemaError()
{
Resolve("""SELECT 1""", out var errors);
errors.Should().ContainSingle().Which.Should().Contain("carries its path more than once");
}
[Fact]
public void QuestionMarkInPath_IsASchemaError()
{
Resolve("""""", out var errors);
errors.Should().ContainSingle().Which.Should().Contain("contains '?'");
}
[Fact]
public void MalformedOptionChild_NamesBothValidShapes()
{
Resolve("""""", out var errors);
errors.Should().ContainSingle().Which
.Should().Contain("family entry").And.Contain("text option");
}
// ── the canon is one: structured and hand-written meet at the same endpoint ──
[Fact]
public async Task StructuredTo_DeliversToTheHandWrittenUriConsumer()
{
object? delivered = null;
_context.AddRoutes(r => r.From("direct://s72-sink").Process(e => delivered = e.In.Body));
_context.AddXmlRoutesFromContent("""
""");
await _context.Start();
var producer = _context.GetEndpoint("direct://s72-in").CreateProducer();
await producer.Start();
await producer.Process(new Exchange(new Message("canon")));
delivered.Should().Be("canon");
}
[Fact]
public async Task StructuredFrom_IsAConsumerLikeAnyOther()
{
var seen = new List