Path: redb-route/tests/redb.Route.Tests.Xml/DataFormatContextTests.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/DataFormatContextTests.cs ---- using System.Text; using System.Xml.Linq; using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Serialization; using redb.Route.Xml; namespace redb.Route.Tests.Xml; /// A format with one option, as the redb.Route.DataFormats.* serializers are. public sealed class ProbeDataFormat : IMessageSerializer { public string Prefix { get; set; } = "plain"; public string ContentType => "application/x-probe"; public byte[] Serialize(T value) => Encoding.UTF8.GetBytes($"{Prefix}:{value}"); public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T)); public object? Deserialize(byte[] data, Type type) => Encoding.UTF8.GetString(data); } /// The options of , as CsvDataFormatOptions is for CsvDataFormat. public sealed class OptionsBuiltFormatOptions { public string Delimiter { get; set; } = ","; } /// Built from an options object only, the shape of the redb.Route.DataFormats.* serializers. public sealed class OptionsBuiltFormat(OptionsBuiltFormatOptions? options = null) : IMessageSerializer { private readonly OptionsBuiltFormatOptions _options = options ?? new(); public string ContentType => "text/x-built"; public byte[] Serialize(T value) => Encoding.UTF8.GetBytes($"a{_options.Delimiter}{value}"); public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T)); public object? Deserialize(byte[] data, Type type) => Encoding.UTF8.GetString(data); } /// /// <dataFormat> in context.xml registers a format for <marshal format="…"/>, as /// context.AddDataFormat(...) does in code. Before, a module in markup could not register one, and on the Tsak /// worker <marshal format="csv"/> failed with "Data format 'csv' is not registered". /// public sealed class DataFormatContextTests : IAsyncLifetime { private static readonly string ProbeType = $"{typeof(ProbeDataFormat).FullName}, {typeof(ProbeDataFormat).Assembly.GetName().Name}"; private readonly RouteContext _context = new(); public Task InitializeAsync() => Task.CompletedTask; public async Task DisposeAsync() => await _context.DisposeAsync(); private void LoadContext(string inner) => new XmlContextLoader(_context).Load(XDocument.Parse($""" {inner} """, LoadOptions.SetLineInfo), "context.xml"); private async Task MarshalAsync(string format) { object? body = null; _context.AddRoutes(r => r.From("direct://df-out").Process(e => body = e.In.Body)); _context.AddXmlRoutesFromContent($""" """); await _context.Start(); var producer = _context.GetEndpoint("direct://df-in").CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message("order"))); return body; } [Fact] public async Task A_format_declared_in_context_xml_is_found_by_its_name_with_its_options() { LoadContext($""""""); var body = await MarshalAsync("probe"); Encoding.UTF8.GetString((byte[])body!).Should().Be("csv-like:order"); } [Fact] public async Task A_format_is_also_found_by_its_own_content_type() { LoadContext($""""""); var body = await MarshalAsync("application/x-probe"); Encoding.UTF8.GetString((byte[])body!).Should().Be("plain:order"); } [Fact] public async Task A_format_built_from_an_options_object_gets_its_options_from_the_attributes() { var built = $"{typeof(OptionsBuiltFormat).FullName}, {typeof(OptionsBuiltFormat).Assembly.GetName().Name}"; LoadContext($""""""); var body = await MarshalAsync("csv"); Encoding.UTF8.GetString((byte[])body!).Should().Be("a;order", "the format has no parameterless constructor; the attributes set its options object"); } [Fact] public void A_type_that_is_not_a_serializer_is_an_error_of_the_document() { var act = () => LoadContext(""""""); act.Should().Throw().Which.Errors.Should().Contain(e => e.Contains("IMessageSerializer")); } }