From 73ffae91b4762fa7a2546f5efce8bf027650eced Mon Sep 17 00:00:00 2001 From: Joshua Harms Date: Mon, 20 Jan 2025 12:51:43 -0600 Subject: [PATCH] chore: add tests for serializing and deserializing HTML-encodable characters with the Serializer's Newtonsoft methods See #1118 --- .../Serialization/SerializerTests.cs | 141 ++++++++++++++++++ ShopifySharp.sln.DotSettings | 1 + 2 files changed, 142 insertions(+) create mode 100644 ShopifySharp.Tests/Infrastructure/Serialization/SerializerTests.cs diff --git a/ShopifySharp.Tests/Infrastructure/Serialization/SerializerTests.cs b/ShopifySharp.Tests/Infrastructure/Serialization/SerializerTests.cs new file mode 100644 index 000000000..0fb62af78 --- /dev/null +++ b/ShopifySharp.Tests/Infrastructure/Serialization/SerializerTests.cs @@ -0,0 +1,141 @@ +using FluentAssertions; +using JetBrains.Annotations; +using ShopifySharp.Infrastructure; +using Xunit; + +namespace ShopifySharp.Tests.Infrastructure.Serialization; + +[Trait("Category", "Serialization"), TestSubject(typeof(Serializer))] +public class SerializerTests +{ + #region (Newtonsoft) Serializer.Serialize + + [Theory] + [InlineData("º")] + [InlineData("<")] + [InlineData(">")] + public void Serialize_WhenSerializingHtmlEncodableCharacters_ShouldNotEncodeToHtml(string encodableCharacter) + { + // Act + var json = Serializer.Serialize(encodableCharacter); + + // Assert + json.Should().Be($"\"{encodableCharacter}\""); + } + + [Theory] + [InlineData("Spiral flute 40º")] + [InlineData("Spiral flute 40<")] + [InlineData("Spiral flute 40>")] + public void Serialize_WhenSerializingProductObjectWithHtmlEncodableCharacters_ShouldNotEncodeToHtml(string productTitle) + { + // Setup + var expectedJson = $$"""{"title":"{{productTitle}}","published_at":null}"""; + var product = new Product { Title = productTitle }; + + // Act + var json = Serializer.Serialize(product); + + // Assert + json.Should().Be(expectedJson); + } + + #endregion + + #region (Newtonsoft) Serializer.Deserialize(string json, Type objectType) + + [Theory] + [InlineData("º")] + [InlineData("<")] + [InlineData(">")] + public void Deserialize_WhenDeserializingHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string encodableCharacter) + { + // Setup + var json = $$"""{"foo":"{{encodableCharacter}}"}"""; + + // Act + var result = Serializer.Deserialize(json, typeof(TestFoo)); + + // Assert + result.Should().BeOfType(typeof(TestFoo)); + result.As().Foo.Should().Be(encodableCharacter); + } + + [Theory] + [InlineData("Spiral flute 40º")] + [InlineData("Spiral flute 40<")] + [InlineData("Spiral flute 40>")] + public void Deserialize_WhenDeserializingProductObjectWithHtmlEncodableCharacters_ShouldNotDeserializeToEncodableHtml(string productTitle) + { + // Setup + var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; + var expectedProduct = new Product { Title = productTitle, PublishedAt = null }; + + // Act + var result = Serializer.Deserialize(json, typeof(Product)); + + // Assert + result.Should().BeOfType(typeof(Product)); + result.As().Should().BeEquivalentTo(expectedProduct); + } + + #endregion + + #region (Newtonsoft) Serializer.Deserialize(string json, string rootElementPath = null, DateParseHandling? dateParseHandlingOverride = null)} + + [Theory] + [InlineData("º")] + [InlineData("<")] + [InlineData(">")] + public void Deserialize_T_WhenDeserializingHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string encodableCharacter) + { + // Setup + var json = $$"""{"foo":"{{encodableCharacter}}"}"""; + + // Act + var result = Serializer.Deserialize(json); + + // Assert + result.Foo.Should().Be(encodableCharacter); + } + + [Theory] + [InlineData("Spiral flute 40º")] + [InlineData("Spiral flute 40<")] + [InlineData("Spiral flute 40>")] + public void Deserialize_T_WhenDeserializingProductObjectWithHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string productTitle) + { + // Setup + var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; + var expectedProduct = new Product { Title = productTitle, PublishedAt = null }; + + // Act + var result = Serializer.Deserialize(json); + + // Assert + result.Should().BeEquivalentTo(expectedProduct); + } + + [Theory] + [InlineData("Spiral flute 40º")] + [InlineData("Spiral flute 40<")] + [InlineData("Spiral flute 40>")] + public void Deserialize_T_WhenDeserializingProductObjectWithHtmlEncodableStrings_AndUsingRootPath_ShouldNotDeserializeToEncodableHtml(string productTitle) + { + // Setup + var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; + + // Act + var result = Serializer.Deserialize(json, "title"); + + // Assert + result.Should().BeEquivalentTo(productTitle); + } + + #endregion + + private record TestFoo + { + public string Foo { get; set; } + } +} diff --git a/ShopifySharp.sln.DotSettings b/ShopifySharp.sln.DotSettings index 2d6bce25d..60fc72569 100644 --- a/ShopifySharp.sln.DotSettings +++ b/ShopifySharp.sln.DotSettings @@ -1,6 +1,7 @@  False True + True True True True