Path: redb-route/tests/redb.Route.Tests.Soap/SoapMtomTests.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.Soap/SoapMtomTests.cs ---- using System.Net; using System.Net.Sockets; using System.Text; using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Soap; using SoapDsl = redb.Route.Soap.Fluent.Soap; namespace redb.Route.Tests.Soap; /// /// MTOM/XOP (camel-cxf mtom-enabled parity): binary attachments as multipart/related, exposed as /// the message attachments. Unit round-trip of the packer plus a full producer→consumer loopback carrying /// attachments in both directions. /// public class SoapMtomTests { private static int FreePort() => global::redb.Route.Tests.Shared.TestPorts.Next(); private static Attachment Part(string contentId, string contentType, byte[] content) => new() { Name = contentId, ContentId = contentId, ContentType = contentType, Content = AttachmentContent.FromBytes(content), }; [Fact] public void Multipart_Write_Then_Parse_RoundTrips() { var payload = new byte[] { 1, 2, 3, 4, 5, 250, 251, 252 }; var atts = new List { Part("blob-1", "application/octet-stream", payload) }; var root = Encoding.UTF8.GetBytes(""); var (body, contentType) = SoapMultipart.Write(root, "text/xml; charset=utf-8", atts); contentType.Should().Contain("multipart/related").And.Contain("application/xop+xml").And.Contain("start-info=\"text/xml\""); var (parsedRoot, parsedAtts) = SoapMultipart.Parse(body, contentType); Encoding.UTF8.GetString(parsedRoot).Should().Contain("Envelope"); parsedAtts.Should().ContainSingle(); parsedAtts[0].ContentId.Should().Be("blob-1"); parsedAtts[0].Name.Should().Be("blob-1"); parsedAtts[0].ContentType.Should().Be("application/octet-stream"); parsedAtts[0].Content.ReadAllBytes().Should().Equal(payload); } [Fact] public void Multipart_Write_AddressesAnAttachmentWithoutContentId_ByItsName() { var atts = new List { new() { Name = "report.pdf", ContentType = "application/pdf", Content = AttachmentContent.FromBytes([7, 8]) }, }; var root = Encoding.UTF8.GetBytes(""); var (body, contentType) = SoapMultipart.Write(root, "text/xml", atts); var (_, parsedAtts) = SoapMultipart.Parse(body, contentType); parsedAtts.Should().ContainSingle().Which.ContentId.Should().Be("report.pdf"); } // Builds a raw multipart/related body with a chosen (short) boundary, so we can exercise the parser's // robustness against foreign wire shapes the GUID-boundary writer never produces. private static byte[] Raw(string s) => Encoding.ASCII.GetBytes(s.Replace("\n", "\r\n")); [Fact] public void Parse_DoesNotSplit_OnBoundaryBytesInsideAttachmentContent() { // The attachment body literally contains "--BND", but NOT at a line start — it must survive intact. const string ct = "multipart/related; boundary=BND; type=\"application/xop+xml\"; start=\"\""; var body = Raw( "--BND\n" + "Content-Type: application/xop+xml; type=\"text/xml\"\n" + "Content-ID: \n" + "\n" + "\n" + "--BND\n" + "Content-Type: application/octet-stream\n" + "Content-ID: \n" + "\n" + "XX--BNDYY\n" + // boundary bytes mid-content, not line-anchored "--BND--\n"); var (root, atts) = SoapMultipart.Parse(body, ct); Encoding.ASCII.GetString(root).Should().Contain("Envelope"); atts.Should().ContainSingle(); Encoding.ASCII.GetString(atts[0].Content.ReadAllBytes()).Should().Be("XX--BNDYY"); } [Fact] public void Parse_KeepsAllAttachments_WhenRootPartHasNoContentId() { // Root part is plain text/xml with no Content-ID and no `start` param → fallback picks the first part, // and BOTH attachments must survive (regression: the old fallback dropped the first attachment). const string ct = "multipart/related; boundary=BND"; var body = Raw( "--BND\n" + "Content-Type: text/xml\n" + "\n" + "\n" + "--BND\n" + "Content-Type: application/octet-stream\n" + "Content-ID: \n" + "\n" + "AAA\n" + "--BND\n" + "Content-Type: application/octet-stream\n" + "Content-ID: \n" + "\n" + "BBB\n" + "--BND--\n"); var (root, atts) = SoapMultipart.Parse(body, ct); Encoding.ASCII.GetString(root).Should().Contain("Envelope"); atts.Select(a => a.ContentId).Should().BeEquivalentTo(new[] { "a1", "b1" }); atts.Select(a => Encoding.ASCII.GetString(a.Content.ReadAllBytes())).Should().BeEquivalentTo(new[] { "AAA", "BBB" }); } [Fact] public void Parse_Throws_OnMultipartContentTypeButNoDelimiter() { const string ct = "multipart/related; boundary=BND"; var body = Encoding.ASCII.GetBytes(" plain, no boundary here"); var act = () => SoapMultipart.Parse(body, ct); act.Should().Throw(); } [Fact] public async Task Mtom_Loopback_CarriesAttachmentsBothWays() { var port = FreePort(); byte[]? consumerGot = null; var uploaded = Encoding.UTF8.GetBytes("the-uploaded-bytes"); var replied = Encoding.UTF8.GetBytes("the-reply-bytes"); const string xop = "http://www.w3.org/2004/08/xop/include"; await using var ctx = new RouteContext(); ctx.AddComponent(new SoapComponent()); ctx.AddToRegistry("mtom", new SoapConnectionFactory { EndpointUrl = $"http://127.0.0.1:{port}/svc", Mtom = true, }); ctx.AddRoutes(r => { r.From(SoapDsl.Listen("/svc").Host("127.0.0.1").Port(port).ConnectionFactory("mtom")) .Process(e => { consumerGot = e.In.Attachments.Find("up-1")?.Content.ReadAllBytes(); e.In.Body = $""; e.In.Attachments.Clear(); e.In.Attachments.Add(Part("resp-1", "text/plain", replied)); }); r.From("direct://call").To(SoapDsl.Call($"http://127.0.0.1:{port}/svc").ConnectionFactory("mtom")); }); await ctx.Start(); var producer = ctx.GetEndpoint("direct://call").CreateProducer(); await producer.Start(); var msg = new Message($""); msg.Attachments.Add(Part("up-1", "application/octet-stream", uploaded)); var exchange = new Exchange(msg); await producer.Process(exchange); // The consumer received our uploaded attachment, and its reply attachment came back to the producer. consumerGot.Should().Equal(uploaded); var back = exchange.Out!.Attachments; back.Should().ContainSingle(); back[0].ContentId.Should().Be("resp-1"); back[0].Content.ReadAllBytes().Should().Equal(replied); } [Fact] public async Task Mtom_Consumer_DoesNotEcho_InboundAttachments_WhenRouteSetsNoReplyList() { var port = FreePort(); await using var ctx = new RouteContext(); ctx.AddComponent(new SoapComponent()); ctx.AddToRegistry("mtom", new SoapConnectionFactory { EndpointUrl = $"http://127.0.0.1:{port}/svc", Mtom = true, }); ctx.AddRoutes(r => { r.From(SoapDsl.Listen("/svc").Host("127.0.0.1").Port(port).ConnectionFactory("mtom")) .Process(e => { // Read the inbound attachment but do NOT change the attachments of the reply. _ = e.In.Attachments.Find("up-1")?.Content.ReadAllBytes(); e.In.Body = "ok"; }); r.From("direct://call").To(SoapDsl.Call($"http://127.0.0.1:{port}/svc").ConnectionFactory("mtom")); }); await ctx.Start(); var producer = ctx.GetEndpoint("direct://call").CreateProducer(); await producer.Start(); var msg = new Message(""); msg.Attachments.Add(Part("up-1", "application/octet-stream", Encoding.UTF8.GetBytes("caller-secret"))); var exchange = new Exchange(msg); await producer.Process(exchange); // The caller's own inbound attachment must NOT come back on the response. exchange.Out!.Attachments.Should().BeEmpty(); } }