ASF-injectie 2/5: HtmlMarkerInjector (alle html/xml/txt van de prerendered host, adresvervanging, transforms, fail-open, begrensde cache) + tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 16:06:17 +01:00
co-authored by Claude Sonnet 5.5
parent 7d66944019
commit a13661517d
7 changed files with 549 additions and 137 deletions
@@ -0,0 +1,274 @@
using System.Net;
using System.Net.Http.Headers;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Hosting;
using SlpModularCms.Core.Hosting.Security;
using SlpModularCms.Core.Hosting.Website;
using Xunit;
namespace SlpModularCms.Core.Tests.Hosting.Website;
/// <summary>
/// The CMS host filling prerendered pages: CMS content, and the production address of the build replaced by the
/// instance's own. In-memory host over a temporary <c>wwwroot/web</c>; no database.
/// </summary>
public sealed class HtmlMarkerInjectorTests : IDisposable
{
private const string Authored = "https://andreaslop.nl";
private const string Own = "https://test.andreaslop.nl";
private readonly string _root = Path.Combine(Path.GetTempPath(), "slp-inject-" + Guid.NewGuid().ToString("N"));
private readonly List<WebApplication> _apps = [];
public void Dispose()
{
foreach (var app in _apps) app.DisposeAsync().AsTask().GetAwaiter().GetResult();
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
}
private void Write(string relative, string content)
{
var path = Path.Combine(_root, "wwwroot", "web", relative);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, content);
}
private sealed class MarkerProvider : IIndexHtmlMarkerProvider
{
public bool Fail;
public Task<IReadOnlyDictionary<string, string>> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken)
{
if (Fail) throw new InvalidOperationException("db down");
return Task.FromResult<IReadOnlyDictionary<string, string>>(new Dictionary<string, string> { ["SLP:X"] = $"[x@{publicBaseUrl}]" });
}
}
/// <summary>Replaces the content of the CMS:LIST region by the site address, so a test can see what it was given.</summary>
private sealed class RegionTransform : IHtmlPageTransform
{
public static bool Throw;
public static int Applied;
public bool Wants(string html) => HtmlMarkerEditor.HasMarkers(html);
public Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
{
Applied++;
if (Throw) throw new InvalidOperationException("boom");
return Task.FromResult(HtmlMarkerEditor.ReplaceRegion(html, "CMS:LIST", $"<ul><li>{context.SiteUrl}</li></ul>"));
}
}
private async Task<(HttpClient Client, WebApplication App)> StartAsync(Dictionary<string, string?>? settings = null, bool transform = true)
{
Directory.CreateDirectory(Path.Combine(_root, "wwwroot"));
RegionTransform.Throw = false;
RegionTransform.Applied = 0;
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
EnvironmentName = Environments.Production,
ContentRootPath = _root,
WebRootPath = Path.Combine(_root, "wwwroot"),
});
builder.WebHost.UseTestServer();
builder.Logging.ClearProviders();
var config = new Dictionary<string, string?>
{
["SecurityHeaders:DefaultPolicy"] = "StrictSite",
["Website:AuthoredBaseUrl"] = Authored,
};
foreach (var (key, value) in settings ?? []) config[key] = value;
builder.Configuration.AddInMemoryCollection(config);
builder.Services.AddCmsSecurityHeaders(builder.Configuration);
builder.Services.AddCmsWebsite(builder.Configuration);
builder.Services.AddSingleton(new MarkerProvider());
builder.Services.AddScoped<IIndexHtmlMarkerProvider>(sp => sp.GetRequiredService<MarkerProvider>());
if (transform) builder.Services.AddScoped<IHtmlPageTransform, RegionTransform>();
var app = builder.Build();
app.UseCmsSecurityHeaders();
app.UseCmsStaticContent(WebsiteMode.PrerenderedPages);
app.UseCmsPrerenderedSite();
app.MapCmsSpaFallbacks(WebsiteMode.PrerenderedPages);
await app.StartAsync();
_apps.Add(app);
return (app.GetTestClient(), app);
}
private static Dictionary<string, string?> Own_() => new() { ["Website:PublicBaseUrl"] = Own };
// ---- the address of the build becomes the instance's own ----
[Fact]
public async Task TheAuthoredAddress_BecomesTheInstancesOwn_InHtml_Sitemap_AndRobots()
{
Write("index.html", $"<link rel=\"canonical\" href=\"{Authored}/\"><meta property=\"og:image\" content=\"{Authored}/og.png\">");
Write("sitemap.xml", $"<urlset><url><loc>{Authored}/</loc></url></urlset>");
Write("robots.txt", $"User-agent: *\nAllow: /\nSitemap: {Authored}/sitemap.xml\n");
var (client, _) = await StartAsync(Own_());
var home = await client.GetAsync("/");
var homeBody = await home.Content.ReadAsStringAsync();
homeBody.Should().Contain($"{Own}/\"").And.Contain($"{Own}/og.png").And.NotContain(Authored);
home.Content.Headers.ContentType!.ToString().Should().Be("text/html; charset=utf-8");
home.Headers.CacheControl!.NoCache.Should().BeTrue();
home.Headers.ETag.Should().NotBeNull();
var sitemap = await client.GetAsync("/sitemap.xml");
(await sitemap.Content.ReadAsStringAsync()).Should().Contain($"<loc>{Own}/</loc>").And.NotContain(Authored);
sitemap.Content.Headers.ContentType!.MediaType.Should().Be("text/xml");
var robots = await client.GetAsync("/robots.txt");
(await robots.Content.ReadAsStringAsync()).Should().Contain($"Sitemap: {Own}/sitemap.xml").And.NotContain(Authored);
robots.Content.Headers.ContentType!.MediaType.Should().Be("text/plain");
}
[Theory]
[InlineData(null)]
[InlineData(Authored)]
public async Task WithoutADifferentPublicBaseUrl_NothingIsReplaced_AndTheFileStaysAPlainStaticFile(string? publicBaseUrl)
{
Write("index.html", $"<link rel=\"canonical\" href=\"{Authored}/\">");
var (client, _) = await StartAsync(new() { ["Website:PublicBaseUrl"] = publicBaseUrl });
var response = await client.GetAsync("/");
(await response.Content.ReadAsStringAsync()).Should().Contain($"{Authored}/");
response.Content.Headers.LastModified.Should().NotBeNull("a plain static file keeps its native headers");
}
[Fact]
public async Task TheNoIndexRobotsAnswerOfTheHost_StillGoesFirst()
{
Write("robots.txt", $"Sitemap: {Authored}/sitemap.xml");
var (client, _) = await StartAsync(new() { ["Website:PublicBaseUrl"] = Own, ["Website:NoIndex"] = "true" });
(await client.GetStringAsync("/robots.txt")).Should().Be("User-agent: *\nDisallow: /\n");
}
// ---- markers, transforms, fail-open ----
[Fact]
public async Task APageWithoutMarkersAndWithoutTheAuthoredAddress_IsAPlainStaticFile()
{
Write("over-mij.html", "<!doctype html><title>Over mij</title>");
var (client, _) = await StartAsync(Own_());
var response = await client.GetAsync("/over-mij");
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Content.Headers.LastModified.Should().NotBeNull();
RegionTransform.Applied.Should().Be(0, "a page with no marker is not handed to any transform");
}
[Fact]
public async Task ATransformFillsItsRegion_AndAProviderFillsItsMarker_OnTheSamePage()
{
Write("index.html", "<body><!--SLP:X--><!--CMS:LIST-->baked<!--/CMS:LIST--></body>");
var (client, _) = await StartAsync(Own_());
var body = await client.GetStringAsync("/");
body.Should().Be($"<body>[x@{Own}]<!--CMS:LIST--><ul><li>{Own}</li></ul><!--/CMS:LIST--></body>");
}
[Fact]
public async Task TheTransformSeesTheAuthoredAddress_WhenNoPublicBaseUrlIsConfigured()
{
Write("index.html", "<!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, _) = await StartAsync();
(await client.GetStringAsync("/")).Should().Contain($"<li>{Authored}</li>");
}
[Fact]
public async Task AFailingTransform_ServesTheBakedPage_StillWithTheOwnAddress()
{
Write("index.html", $"<a href=\"{Authored}/\">x</a><!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, _) = await StartAsync(Own_());
RegionTransform.Throw = true;
var response = await client.GetAsync("/");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Be($"<a href=\"{Own}/\">x</a><!--CMS:LIST-->baked<!--/CMS:LIST-->");
}
[Fact]
public async Task AFailingProvider_ServesTheMarkerUnfilled_AndTheTransformStillRuns()
{
Write("index.html", "<!--SLP:X--><!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, app) = await StartAsync(Own_());
app.Services.GetRequiredService<MarkerProvider>().Fail = true;
var body = await client.GetStringAsync("/");
body.Should().Be($"<!--SLP:X--><!--CMS:LIST--><ul><li>{Own}</li></ul><!--/CMS:LIST-->");
}
// ---- HTTP behaviour ----
[Fact]
public async Task AMatchingETag_Gets304_AndHeadHasNoBody()
{
Write("index.html", "<!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, _) = await StartAsync(Own_());
var etag = (await client.GetAsync("/")).Headers.ETag!;
var conditional = new HttpRequestMessage(HttpMethod.Get, "/");
conditional.Headers.IfNoneMatch.Add(etag);
(await client.SendAsync(conditional)).StatusCode.Should().Be(HttpStatusCode.NotModified);
var head = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, "/"));
head.StatusCode.Should().Be(HttpStatusCode.OK);
head.Headers.ETag.Should().Be(etag);
(await head.Content.ReadAsByteArrayAsync()).Should().BeEmpty();
}
[Fact]
public async Task TheResult_IsCached_UntilInvalidatedOrTheFileChanges()
{
Write("index.html", "<!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, app) = await StartAsync(Own_());
await client.GetStringAsync("/");
await client.GetStringAsync("/");
RegionTransform.Applied.Should().Be(1, "the second request is served from the cache");
app.Services.GetRequiredService<IIndexHtmlCache>().Invalidate();
await client.GetStringAsync("/");
RegionTransform.Applied.Should().Be(2);
Write("index.html", "<!--CMS:LIST-->baked two<!--/CMS:LIST-->");
File.SetLastWriteTimeUtc(Path.Combine(_root, "wwwroot", "web", "index.html"), DateTime.UtcNow.AddMinutes(1));
await client.GetStringAsync("/");
RegionTransform.Applied.Should().Be(3, "a changed file is picked up without waiting for the TTL");
}
[Fact]
public async Task TheCache_CannotBeFilledThroughTheHostHeader()
{
Write("index.html", "<!--CMS:LIST-->baked<!--/CMS:LIST-->");
var (client, app) = await StartAsync(new() { ["Website:AuthoredBaseUrl"] = "" });
var injector = app.Services.GetRequiredService<HtmlMarkerInjector>();
for (var i = 0; i < HtmlMarkerInjector.MaxEntries + 50; i++)
{
var request = new HttpRequestMessage(HttpMethod.Get, "/");
request.Headers.Host = $"h{i}.example.test";
(await client.SendAsync(request)).StatusCode.Should().Be(HttpStatusCode.OK);
}
injector.CachedEntryCount.Should().BeLessThanOrEqualTo(HtmlMarkerInjector.MaxEntries);
}
}