Path: redb-route/tests/redb.Route.Tests/Serialization/XmlMessageSerializerTests.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/Serialization/XmlMessageSerializerTests.cs
----
using System.Xml;
using redb.Route.Serialization;
namespace redb.Route.Tests.Serialization;
///
/// Tests for .
///
public class XmlMessageSerializerTests
{
private readonly XmlMessageSerializer _sut = new();
[Fact]
public void ContentType_IsApplicationXml()
{
_sut.ContentType.Should().Be("application/xml");
}
[Fact]
public void Serialize_ReturnsNonEmptyBytes()
{
var bytes = _sut.Serialize(new XmlTestOrder { Id = "ORD-1", Amount = 99.5m });
bytes.Should().NotBeEmpty();
}
[Fact]
public void Serialize_ProducesValidXml()
{
var bytes = _sut.Serialize(new XmlTestOrder { Id = "ORD-1", Amount = 42m });
var xml = System.Text.Encoding.UTF8.GetString(bytes);
xml.Should().Contain("ORD-1");
xml.Should().Contain("42");
}
[Fact]
public void Serialize_OmitsXmlDeclarationByDefault()
{
var bytes = _sut.Serialize(new XmlTestOrder { Id = "X" });
var xml = System.Text.Encoding.UTF8.GetString(bytes);
xml.Should().NotContain("(bytes);
restored.Should().NotBeNull();
restored!.Id.Should().Be("ORD-2");
restored.Amount.Should().Be(100.25m);
}
[Fact]
public void Deserialize_UntypedOverload_ReturnsCorrectType()
{
var original = new XmlTestOrder { Id = "ORD-3", Amount = 50m };
var bytes = _sut.Serialize(original);
var restored = _sut.Deserialize(bytes, typeof(XmlTestOrder));
restored.Should().BeOfType();
((XmlTestOrder)restored!).Id.Should().Be("ORD-3");
}
[Fact]
public void Deserialize_NullData_Throws()
{
var act = () => _sut.Deserialize(null!);
act.Should().Throw();
}
[Fact]
public void Deserialize_Untyped_NullData_Throws()
{
var act = () => _sut.Deserialize(null!, typeof(XmlTestOrder));
act.Should().Throw();
}
[Fact]
public void Deserialize_Untyped_NullType_Throws()
{
var act = () => _sut.Deserialize(new byte[] { 1 }, null!);
act.Should().Throw();
}
[Fact]
public void CustomSettings_WithXmlDeclaration_IncludesDeclaration()
{
var writerSettings = new XmlWriterSettings
{
OmitXmlDeclaration = false,
Indent = true
};
var sut = new XmlMessageSerializer(writerSettings, XmlMessageSerializer.DefaultReaderSettings);
var bytes = sut.Serialize(new XmlTestOrder { Id = "X" });
var xml = System.Text.Encoding.UTF8.GetString(bytes);
xml.Should().Contain(" new XmlMessageSerializer(null!, XmlMessageSerializer.DefaultReaderSettings);
act.Should().Throw();
}
[Fact]
public void Constructor_NullReaderSettings_Throws()
{
var act = () => new XmlMessageSerializer(XmlMessageSerializer.DefaultWriterSettings, null!);
act.Should().Throw();
}
/// Test DTO for XML serialization. Must be public with parameterless ctor for XmlSerializer.
public class XmlTestOrder
{
public string? Id { get; set; }
public decimal Amount { get; set; }
}
}