H4: hero in de eerste HTML (regio, preload), gedeeld datablok met contributors (Offerings byte-identiek), ASF golden files, volledige pijplijntest
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Hosting.Website;
|
||||
|
||||
public class CmsDataBlockTransformTests
|
||||
{
|
||||
private static readonly HtmlTransformContext Context = new("https://test.example.nl");
|
||||
private const string Open = "<script type=\"application/json\" id=\"cms-data\">";
|
||||
|
||||
private static ICmsDataBlockContributor Contributor(string key, int order, object? value)
|
||||
{
|
||||
var c = Substitute.For<ICmsDataBlockContributor>();
|
||||
c.Key.Returns(key);
|
||||
c.Order.Returns(order);
|
||||
c.GetAsync(default!, default).ReturnsForAnyArgs(value);
|
||||
return c;
|
||||
}
|
||||
|
||||
private static JsonElement Data(string html)
|
||||
{
|
||||
var start = html.IndexOf(Open, StringComparison.Ordinal) + Open.Length;
|
||||
return JsonDocument.Parse(html[start..html.IndexOf("</script>", start, StringComparison.Ordinal)]).RootElement;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<!--CMS:DATA-->", true)]
|
||||
[InlineData("<p>nothing</p>", false)]
|
||||
[InlineData("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->", false)]
|
||||
public void Wants_OnlyPagesWithTheDataMarker(string html, bool expected)
|
||||
=> new CmsDataBlockTransform([]).Wants(html).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public async Task TheKeys_FollowTheOrderOfTheContributors_AfterTheSiteUrl()
|
||||
{
|
||||
var transform = new CmsDataBlockTransform([Contributor("hero", 200, new { a = 1 }), Contributor("offerings", 100, new[] { 1, 2 }), Contributor("banners", 110, new { top = (string?)null })]);
|
||||
|
||||
var html = await transform.ApplyAsync("<head><!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>", Context, default);
|
||||
|
||||
html.Should().StartWith("<head>" + Open).And.EndWith("</script></head>").And.NotContain("baked");
|
||||
var data = Data(html);
|
||||
data.EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl", "offerings", "banners", "hero");
|
||||
data.GetProperty("siteUrl").GetString().Should().Be("https://test.example.nl");
|
||||
data.GetProperty("offerings").GetRawText().Should().Be("[1,2]");
|
||||
data.GetProperty("banners").GetProperty("top").ValueKind.Should().Be(JsonValueKind.Null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ANullValue_IsWrittenAsNull_SoTheWebsiteKeepsItsBakedVersion()
|
||||
{
|
||||
var html = await new CmsDataBlockTransform([Contributor("hero", 200, null)]).ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
Data(html).GetProperty("hero").ValueKind.Should().Be(JsonValueKind.Null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheValues_UseTheCamelCaseShapeOfThePublicApi()
|
||||
{
|
||||
var html = await new CmsDataBlockTransform([Contributor("x", 1, new { SomeName = "a", Nested = new { InnerValue = (int?)null } })]).ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
html.Should().Contain("\"someName\":\"a\"").And.Contain("\"innerValue\":null");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheBlock_CannotCloseTheScriptElement()
|
||||
{
|
||||
var html = await new CmsDataBlockTransform([Contributor("x", 1, new { text = "</script><img src=x onerror=alert(1)> & \u2028" })]).ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
var inner = html[Open.Length..^"</script>".Length];
|
||||
inner.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028");
|
||||
JsonDocument.Parse(inner).RootElement.GetProperty("x").GetProperty("text").GetString().Should().Be("</script><img src=x onerror=alert(1)> & \u2028");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AFailingContributor_FailsTheWholeBlock_SoThePageKeepsItsBakedBlock()
|
||||
{
|
||||
var failing = Substitute.For<ICmsDataBlockContributor>();
|
||||
failing.Key.Returns("hero");
|
||||
failing.Order.Returns(200);
|
||||
failing.GetAsync(default!, default).ThrowsAsyncForAnyArgs(new InvalidOperationException("boom"));
|
||||
|
||||
var act = async () => await new CmsDataBlockTransform([Contributor("offerings", 100, new[] { 1 }), failing]).ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
await act.Should().ThrowAsync<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithoutContributors_TheBlockHoldsTheSiteUrlOnly()
|
||||
{
|
||||
var html = await new CmsDataBlockTransform([]).ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
Data(html).EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user