using System.Text.Json; using System.Text.Json.Serialization; using Bit.Core.Utilities; using Xunit; namespace Bit.Core.Test.Utilities; public class HtmlEncodingStringConverterTests { [Fact] public void Serialize_WhenEncodedValueIsNotNull_SerializesHtmlEncodedString() { // Arrange var obj = new HtmlEncodedString { EncodedValue = "This is <b>bold</b>", NonEncodedValue = "This is bold" }; const string expectedJsonString = "{\"EncodedValue\":\"This is bold\",\"NonEncodedValue\":\"This is bold\"}"; // This is necessary to prevent the serializer from double encoding the string var serializerOptions = new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; // Act var jsonString = JsonSerializer.Serialize(obj, serializerOptions); // Assert Assert.Equal(expectedJsonString, jsonString); } [Fact] public void Serialize_WhenEncodedValueIsNull_SerializesNull() { // Arrange var obj = new HtmlEncodedString { EncodedValue = null, NonEncodedValue = null }; const string expectedJsonString = "{\"EncodedValue\":null,\"NonEncodedValue\":null}"; // Act var jsonString = JsonSerializer.Serialize(obj); // Assert Assert.Equal(expectedJsonString, jsonString); } [Fact] public void Deserialize_WhenJsonContainsHtmlEncodedString_ReturnsDecodedString() { // Arrange const string json = "{\"EncodedValue\":\"This is bold\",\"NonEncodedValue\":\"This is bold\"}"; const string expectedEncodedValue = "This is <b>bold</b>"; const string expectedNonEncodedValue = "This is bold"; // Act var obj = JsonSerializer.Deserialize(json); // Assert Assert.Equal(expectedEncodedValue, obj.EncodedValue); Assert.Equal(expectedNonEncodedValue, obj.NonEncodedValue); } [Fact] public void Deserialize_WhenJsonContainsNull_ReturnsNull() { // Arrange const string json = "{\"EncodedValue\":null,\"NonEncodedValue\":null}"; // Act var obj = JsonSerializer.Deserialize(json); // Assert Assert.Null(obj.EncodedValue); Assert.Null(obj.NonEncodedValue); } } public class HtmlEncodedString { [JsonConverter(typeof(HtmlEncodingStringConverter))] public string EncodedValue { get; set; } public string NonEncodedValue { get; set; } }