Path: redb-route/tests/redb.Route.Tests.Xml/XmlContainerAndLoadingTests.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/XmlContainerAndLoadingTests.cs ---- using System.Xml.Linq; using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Xml; namespace redb.Route.Tests.Xml; /// /// Container batch: handlers on <routes> for every route of the file, the /// extension channel exercised from outside the core, expression-escaping equivalence and /// the glob loading overload. /// public class XmlContainerAndLoadingTests : IAsyncLifetime { private readonly RouteContext _context = new(); public Task InitializeAsync() => Task.CompletedTask; public async Task DisposeAsync() { await _context.DisposeAsync(); GC.SuppressFinalize(this); } private async Task StartAndProducer(string fromUri) { await _context.Start(); var producer = _context.GetEndpoint(fromUri).CreateProducer(); await producer.Start(); return producer; } // ── container-level handlers ───────────────────────────────────────────── [Fact] public async Task ContainerOnException_AppliesToEveryRouteOfTheFile() { _context.AddXmlRoutesFromContent(""" """); await _context.Start(); var producerA = _context.GetEndpoint("direct://cont-a-in").CreateProducer(); var producerB = _context.GetEndpoint("direct://cont-b-in").CreateProducer(); await producerA.Start(); await producerB.Start(); var a = new Exchange(new Message("x")); await producerA.Process(a); var b = new Exchange(new Message("x")); await producerB.Process(b); a.In.Headers["rescued"].Should().Be("yes"); b.In.Headers["rescued"].Should().Be("yes"); } [Fact] public async Task ContainerIntercept_RunsForEveryRouteOfTheFile() { var seen = new List(); _context.AddRoutes(r => r.From("direct://cont-i-sink").Process(e => seen.Add(e.In.Headers["tapped"]))); _context.AddXmlRoutesFromContent(""" """); await _context.Start(); foreach (var uri in new[] { "direct://cont-i-1-in", "direct://cont-i-2-in" }) { var producer = _context.GetEndpoint(uri).CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message("x"))); } seen.Should().Equal("yes", "yes"); } [Fact] public void SkipSendToOriginalEndpoint_OnAPlainIntercept_IsASchemaError() { // The attribute only means something on ; on the other // intercepts it is a misplaced intention, not something to apply silently. var act = () => _context.AddXmlRoutesFromContent(""" """); act.Should().Throw() .WithMessage("*skipSendToOriginalEndpoint*only*interceptSendToEndpoint*"); } // ── the extension channel from outside the core ────────────────────────── private sealed class StampContribution : IXmlElementContribution { public string Name => "stamp"; public XmlElementKind Kind => XmlElementKind.Step; // The spec is what admits the attribute past the generated schema: // an opaque contribution parses, but its attributes fail validation — by design. public ElementSpec Spec => ElementSpec.Leaf("stamp", new AttributeSpec("value", AttributeType.String)); public IRouteDefinition Apply(XElement element, IRouteDefinition current, XmlParseContext context) => current.SetHeader("stamped", context.Attr(element, "value") ?? "yes"); } private sealed class DuplicateToContribution : IXmlElementContribution { public string Name => "to"; public XmlElementKind Kind => XmlElementKind.Step; public IRouteDefinition Apply(XElement element, IRouteDefinition current, XmlParseContext context) => current; } [Fact] public async Task ExternalContribution_Registered_ParsesLikeACoreElement() { _context.AddXmlRoutesFromContent(""" """, options: new XmlRouteLoaderOptions { Extensions = [new StampContribution()] }); var producer = await StartAndProducer("direct://ext-in"); var exchange = new Exchange(new Message("x")); await producer.Process(exchange); exchange.In.Headers["stamped"].Should().Be("external"); } [Fact] public void ExternalContribution_NotRegistered_ItsElementIsRejected() { var act = () => _context.AddXmlRoutesFromContent(""" """); act.Should().Throw().WithMessage("*unknown element *"); } [Fact] public void DuplicateElementName_IsAHardRegistrationError() { var act = () => _context.AddXmlRoutesFromContent( """""", options: new XmlRouteLoaderOptions { Extensions = [new DuplicateToContribution()] }); act.Should().Throw().WithMessage("*'to' is already registered*"); } // ── expression escaping equivalence ────────────────────────────────────── [Fact] public async Task EscapedAndBareGreaterThan_AreTheSameExpression() { var passed = new List(); _context.AddRoutes(r => r.From("direct://esc-sink").Process(e => passed.Add((string)e.In.Body!))); _context.AddXmlRoutesFromContent(""" """); await _context.Start(); foreach (var (uri, tag) in new[] { ("direct://esc-bare-in", "bare"), ("direct://esc-escaped-in", "escaped") }) { var producer = _context.GetEndpoint(uri).CreateProducer(); await producer.Start(); var over = new Exchange(new Message(tag)); over.In.Headers["a"] = 20; await producer.Process(over); var under = new Exchange(new Message(tag + "-under")); under.In.Headers["a"] = 5; await producer.Process(under); } passed.Should().Equal("bare", "escaped"); } // ── glob loading ───────────────────────────────────────────────────────── [Fact] public async Task GlobPattern_LoadsEveryMatchingFile_Deterministically() { var dir = Path.Combine(Path.GetTempPath(), "redb-xml-glob-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); try { File.WriteAllText(Path.Combine(dir, "a.route.xml"), """ """); File.WriteAllText(Path.Combine(dir, "b.route.xml"), """ """); File.WriteAllText(Path.Combine(dir, "ignored.txt"), "not a route file"); _context.AddXmlRoutes(Path.Combine(dir, "*.route.xml")); await _context.Start(); _context.Routes.Select(r => r.RouteId).Should().Contain(["glob-a", "glob-b"]); } finally { Directory.Delete(dir, recursive: true); } } [Fact] public void GlobPattern_MatchingNothing_IsAnError_NamingTheSearchedPlaces() { var dir = Path.Combine(Path.GetTempPath(), "redb-xml-glob-empty-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); try { var act = () => _context.AddXmlRoutes(Path.Combine(dir, "*.route.xml")); act.Should().Throw() .WithMessage("*no route files match*Searched:*"); } finally { Directory.Delete(dir, recursive: true); } } // ── in / not in in markup conditions ───────────────────────────── [Fact] public async Task InAndNotIn_WorkInMarkupConditions_WithALiteralAndAMessageList() { _context.AddXmlRoutesFromContent(""" """); var producer = await StartAndProducer("direct://in-markup"); async Task Send(string region, string status) { var exchange = new Exchange(new Message("x")); exchange.In.Headers["region"] = region; exchange.In.Headers["status"] = status; exchange.Properties["closedStatuses"] = new[] { "closed", "void" }; await producer.Process(exchange); return exchange; } var eu = await Send("uk", "open"); var us = await Send("us", "closed"); eu.In.Headers["zone"].Should().Be("europe"); eu.In.Headers["open"].Should().Be("yes"); us.In.Headers["zone"].Should().Be("other"); us.In.Headers.ContainsKey("open").Should().BeFalse("closed is in the closed list"); } }