ASF-injectie 5/5: end-to-end test op de master-host, ASF Website:AuthoredBaseUrl, rooktest-uitbreiding, docs

- ASF appsettings: Website:AuthoredBaseUrl=https://andreaslop.nl, PublicBaseUrl leeg (env).
- Api.Tests/AsfInjectionPipelineTests: pagina met markers, echte database; een CMS-wijziging is direct zichtbaar (ETag wijzigt), data-blok == publieke API, 304, sitemap-adres.
- smoke-site.sh: onverwerkte CMS-markers op / falen; data-blok moet siteUrl van deze host hebben.
- docs: asf-host-injection 'as built'.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 16:16:27 +01:00
co-authored by Claude Sonnet 5.5
parent 4baadf3144
commit 571aaf9cd2
6 changed files with 207 additions and 3 deletions
+18 -1
View File
@@ -2,7 +2,7 @@
- **Slug**: asf-host-injection
- **Type**: Feature, **proposal; not approved, build not started**
- **Status**: Plan agreed with the ASF session (their counterpart: `AndreaSlopFotografie-packages/aidlc-docs/features/asf-cms-host-injection/plan.md`, section "Host contract"). Bryan accepted the approach (neutral list first, styled cards after hydration; the host writes the list HTML) on 2026-11-15 but said **not to start building yet**: he first reads the plan and finds out whether .NET 10 can run on the hosting package (hosting bundle or self-contained).
- **Status**: **Built** on `feature/contact-forms-module` (commits 7d66944, a136615, ef4fb9d, 4baadf3 and the one after it; tests green locally), **not yet deployed**: needs the website build with the markers and `Website__PublicBaseUrl=https://test.andreaslop.nl` in the ASF-test env file. Before: plan agreed with the ASF session (their counterpart: `AndreaSlopFotografie-packages/aidlc-docs/features/asf-cms-host-injection/plan.md`, section "Host contract"). Bryan accepted the approach (neutral list first, styled cards after hydration; the host writes the list HTML) on 2026-11-15 but said **not to start building yet**: he first reads the plan and finds out whether .NET 10 can run on the hosting package (hosting bundle or self-contained).
- **Affected components (planned)**: `SlpModularCms.Core` (`Hosting/Website`: generalise `IndexHtmlInjector`, hook in `PrerenderedSiteMiddleware`), `SlpModularCms.Modules.Offerings` (marker providers), `scripts/smoke-site.sh`, docs
- **Documented**: 2026-11-15
- **Documentation Language**: English
@@ -42,3 +42,20 @@ Injected pages: `Cache-Control: no-cache`, strong ETag from the final body, 304
## Risks
Markers stripped by a build (confirmed not the case for ASF's Vite/TanStack prerender); proxy or IIS caching of HTML; .NET 10 unavailable on the package; the per-base-URL cache keyed from the Host header when no `PublicBaseUrl` is configured (to be capped in this unit); several app instances (invalidation is per process; the 60-second TTL bounds the lag). Estimated effort: about 1.5 days for the host side, plus ASF's front-end rework.
## As built (2026-11-15)
| Piece | Where |
|---|---|
| Marker edits (regions, data block, JSON-LD, inline values, script escaping) | `Core/Hosting/Website/HtmlMarkerEditor.cs` |
| Injector for every html/xml/txt file of the prerendered host and for the SpaFallback index.html; caching (60 s, direct invalidation, capped at 256 entries), ETag/304, address replacement, per-part fail-open | `Core/Hosting/Website/HtmlMarkerInjector.cs`; hook in `PrerenderedSiteMiddleware` (`ServeFile`); registered by `AddCmsWebsite` |
| Neutral list and banner HTML, price rules (whole euros, `1.250`) | `Offerings/Services/CmsPageHtml.cs`; golden files of the website repo (ASF commit 16027f5) in `Offerings.Tests/Golden/host-injection`, `GoldenHtmlTests` |
| Marker transform (regions, `#cms-data` with `siteUrl`, JSON-LD price, `data-cms-price`) | `Offerings/Services/OfferingsHtmlTransform.cs` (`IHtmlPageTransform`) |
| Settings | ASF `appsettings.json`: `Website:AuthoredBaseUrl=https://andreaslop.nl`, `Website:PublicBaseUrl` empty (env `Website__PublicBaseUrl`) |
| End-to-end test (real database, ASF settings, temporary web root) | `Api.Tests/AsfInjectionPipelineTests` |
| Smoke test | `scripts/smoke-site.sh`: unprocessed `CMS:DATA` / `CMS:OFFER-JSON-LD` markers on `/` fail; a data block must carry this host's `siteUrl`; ETag and no-cache on the injected home page |
Behaviour choices: a successful empty answer empties a region (no live package, no active banner); a failing service leaves the baked content; a package without a derivable price removes `offers` from the JSON-LD (a failing service does not); a file without markers and without the authored address stays a plain static file; 404.html is not injected.
**Deploy order matters**: set `Website__PublicBaseUrl=https://test.andreaslop.nl` on ASF-test before the first website build with markers is deployed, otherwise the data block's `siteUrl` is the production address and the smoke test fails on purpose.
+1 -1
View File
@@ -24,6 +24,6 @@ Only hosts in `WebsiteMode.SpaFallback` (SlpSoftware). The ASF host (`Prerendere
| `KnownPaths` | Only these paths are pages (200, the SPA shell); any other website path is a real 404 (still the shell). `/api`, `/admin` and assets keep working. |
| `Sitemap:Paths` | Paths listed in the sitemap. |
## Index markers (`IndexHtmlInjector`)
## Index markers (`HtmlMarkerInjector` (until 2026-11-15 `IndexHtmlInjector`; generalised for the prerendered host, see asf-host-injection))
The host fills markers in `index.html`: `<!--SLP:OFFERINGS-JSON-LD-->` (schema.org `ItemList` of `Offer`) and `<!--SLP:OFFERINGS-HTML-->` (a static `#packages-static` list), rendered by `OfferingsIndexMarkers` from the live offerings. 60-second cache, invalidated directly by every offering change, `ETag` with 304 support; if a provider fails the plain page is still served. The markers must stay in `<body>` above `#root`; the website removes `#packages-static` on start-up.
+19
View File
@@ -54,6 +54,25 @@ expect_status /api/v1/offerings/all 401 "the administrator list is protected"
expect_header / X-Robots-Tag noindex "noindex on the website"
expect_body /robots.txt "Disallow: /" "robots.txt disallows everything"
# The home page is served by the host. When the website build left CMS markers in it, the host must have processed
# them: an unprocessed <!--CMS:DATA--> or JSON-LD marker means the page does not go through the injector (a proxy or
# IIS serving files itself, or a host without the injection), and a deployed data block must carry this host's address.
home="$(curl_get "$base/" || true)"
if printf '%s' "$home" | grep -qE '<!--CMS:(DATA|OFFER-JSON-LD)'; then
fail "CMS markers on the home page were NOT processed by the host"
else
pass "no unprocessed CMS markers on the home page"
fi
if printf '%s' "$home" | grep -q 'id="cms-data"'; then
if printf '%s' "$home" | grep -q "\"siteUrl\":\"$base\""; then
pass "the data block carries this host's address (siteUrl $base)"
else
fail "the data block's siteUrl is not $base (is Website__PublicBaseUrl set on this host?)"
fi
expect_header / ETag '"' "the injected home page has an ETag"
expect_header / Cache-Control no-cache "the injected home page is no-cache"
fi
case "$profile" in
sls-test)
for m in Identity Availability Offerings Contact Master; do expect_module "$m"; done
@@ -50,7 +50,15 @@
"Mode": "PrerenderedPages",
// Secure default: every website response carries noindex and /robots.txt disallows all. A
// production instance must set Website__NoIndex=false explicitly.
"NoIndex": true
"NoIndex": true,
// The public address of this instance (env: Website__PublicBaseUrl), e.g. https://test.andreaslop.nl on the test
// host. Empty = the address the site was built with is served as it is (production).
"PublicBaseUrl": "",
// The address the website build writes into its pages, sitemap.xml and robots.txt (canonical, og:*, JSON-LD).
// When PublicBaseUrl is set to something else, the host replaces it by PublicBaseUrl while serving those files,
// so test and production run the same website build. Also used as the data block's siteUrl when
// PublicBaseUrl is empty.
"AuthoredBaseUrl": "https://andreaslop.nl"
},
// Mail (invitations, password reset, notices). Mail is OFF while Email:Smtp:Host is empty and every
// flow then works as before (copy the link). Enable per environment with environment variables:
@@ -0,0 +1,150 @@
using System.Net;
using System.Text.Json;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Api.Tests;
/// <summary>
/// The whole chain on the master host with Andrea Slop Fotografie's settings: a prerendered page with the website
/// build's markers, the real Offerings module over a real database, and the injector. A change made through the
/// module's services must show in the served HTML at once (cache invalidation), including the ETag.
/// </summary>
public sealed class AsfInjectionFactory : AsfVariantFactory, IDisposable
{
public string WebRoot { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-injection-" + Guid.NewGuid().ToString("N"));
public AsfInjectionFactory() => Directory.CreateDirectory(Path.Combine(WebRoot, "web"));
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseWebRoot(WebRoot);
builder.UseSetting("Website:PublicBaseUrl", "https://test.andreaslop.nl");
builder.ConfigureAppConfiguration((_, config) =>
config.AddInMemoryCollection(new Dictionary<string, string?> { ["Website:PublicBaseUrl"] = "https://test.andreaslop.nl" }));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && Directory.Exists(WebRoot)) Directory.Delete(WebRoot, recursive: true);
}
}
public class AsfInjectionPipelineTests : IClassFixture<AsfInjectionFactory>
{
private const string Page =
"<!doctype html><html><head>" +
"<link rel=\"canonical\" href=\"https://andreaslop.nl/soorten-fotoshoots/smoke\">" +
"<!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script>" +
"<!--CMS:OFFER-JSON-LD slug=\"smoke\"--><script type=\"application/ld+json\">{\"@type\":\"Service\",\"offers\":{\"@type\":\"Offer\",\"price\":1,\"priceCurrency\":\"EUR\"}}</script>" +
"</head><body><!--CMS:BANNER-TOP-HTML-->baked top<!--/CMS:BANNER-TOP-HTML-->" +
"<!--CMS:OFFERINGS-HTML-->baked list<!--/CMS:OFFERINGS-HTML-->" +
"<p>vanaf <span data-cms-price=\"smoke\">€&nbsp;1</span></p></body></html>";
private readonly AsfInjectionFactory _factory;
public AsfInjectionPipelineTests(AsfInjectionFactory factory)
{
_factory = factory;
File.WriteAllText(Path.Combine(factory.WebRoot, "web", "smoke.html"), Page);
File.WriteAllText(Path.Combine(factory.WebRoot, "web", "sitemap.xml"), "<urlset><url><loc>https://andreaslop.nl/</loc></url></urlset>");
}
private async Task<T> InScope<T>(Func<IServiceProvider, Task<T>> action)
{
_ = _factory.CreateClient(); // starts the host and migrates the database
await using var scope = _factory.Services.CreateAsyncScope();
return await action(scope.ServiceProvider);
}
[Fact]
public async Task ACmsChange_ShowsInTheServedPage_AtOnce_AndTheAddressIsTheTestHosts()
{
var client = _factory.CreateClient();
var caller = Guid.NewGuid();
// Clean slate for the slug of this test (the database is shared with other tests).
await InScope(async sp =>
{
var service = sp.GetRequiredService<IOfferingsService>();
foreach (var existing in (await service.ListAllAsync()).Where(o => o.Slug == "smoke")) await service.DeleteAsync(Guid.Parse(existing.Id), caller);
return 0;
});
// Nothing live: regions are emptied, the data block has no package, the price element is gone, the address is the host's.
var empty = await client.GetAsync("/smoke");
empty.StatusCode.Should().Be(HttpStatusCode.OK);
var emptyBody = await empty.Content.ReadAsStringAsync();
emptyBody.Should().Contain("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->");
emptyBody.Should().Contain("https://test.andreaslop.nl/soorten-fotoshoots/smoke").And.NotContain("https://andreaslop.nl");
emptyBody.Should().NotContain("\"offers\"").And.NotContain("baked");
var etagBefore = empty.Headers.ETag!.Tag;
// Create the package through the module: the very next request shows it.
await InScope(async sp =>
{
await sp.GetRequiredService<IOfferingsService>().CreateAsync(
new CreateOfferingRequest("Smoke", null, "€ 1.250", null, ["Eén uur", "O'Brien & zoon"], "Boek", Slug: "smoke"), caller);
await sp.GetRequiredService<IOfferingBannersService>().CreateAsync(
new OfferingBannerRequest("", "Nog twee plekken", Position: "top"), caller);
return 0;
});
var live = await client.GetAsync("/smoke");
var body = await live.Content.ReadAsStringAsync();
live.Headers.ETag!.Tag.Should().NotBe(etagBefore);
body.Should().Contain("<li class=\"cms-offering\" data-slug=\"smoke\"><h3>Smoke</h3><p class=\"cms-price\">€&nbsp;1.250</p>");
body.Should().Contain("<li>O&#39;Brien &amp; zoon</li>");
body.Should().Contain("<!--CMS:BANNER-TOP-HTML--><aside class=\"cms-banner\"><span>Nog twee plekken</span></aside><!--/CMS:BANNER-TOP-HTML-->");
body.Should().Contain("<span data-cms-price=\"smoke\">€&nbsp;1.250</span>");
body.Should().Contain("\"price\":1250").And.Contain("\"priceCurrency\":\"EUR\"");
// The data block is the public API's answer plus the site's address.
var start = body.IndexOf("<script type=\"application/json\" id=\"cms-data\">", StringComparison.Ordinal) + "<script type=\"application/json\" id=\"cms-data\">".Length;
var json = body[start..body.IndexOf("</script>", start, StringComparison.Ordinal)];
var data = JsonDocument.Parse(json).RootElement;
data.GetProperty("siteUrl").GetString().Should().Be("https://test.andreaslop.nl");
var api = JsonDocument.Parse(await client.GetStringAsync("/api/v1/offerings")).RootElement;
Same(data.GetProperty("offerings"), api).Should().BeTrue("the data block carries the same JSON as GET /api/v1/offerings");
var topApi = await client.GetStringAsync("/api/v1/offerings/banner?position=top");
Same(data.GetProperty("banners").GetProperty("top"), JsonDocument.Parse(topApi).RootElement).Should().BeTrue();
data.GetProperty("banners").GetProperty("bottom").ValueKind.Should().Be(JsonValueKind.Null);
// A matching ETag is a 304.
var conditional = new HttpRequestMessage(HttpMethod.Get, "/smoke");
conditional.Headers.IfNoneMatch.Add(live.Headers.ETag);
(await client.SendAsync(conditional)).StatusCode.Should().Be(HttpStatusCode.NotModified);
// Switching the package off removes it again, and the JSON-LD loses its offers.
await InScope(async sp =>
{
var service = sp.GetRequiredService<IOfferingsService>();
var id = Guid.Parse((await service.ListAllAsync()).Single(o => o.Slug == "smoke").Id);
await service.DeleteAsync(id, caller);
return 0;
});
var gone = await client.GetStringAsync("/smoke");
gone.Should().NotContain("data-slug=\"smoke\"").And.NotContain("\"offers\"");
}
// The same JSON value; the writers may differ in escaping (the API writes "€" as is, the data block as €).
private static bool Same(JsonElement a, JsonElement b) =>
System.Text.Json.Nodes.JsonNode.DeepEquals(System.Text.Json.Nodes.JsonNode.Parse(a.GetRawText()), System.Text.Json.Nodes.JsonNode.Parse(b.GetRawText()));
[Fact]
public async Task TheSitemap_GetsTheTestHostsAddress_ButStillComesFromTheBuild()
{
var client = _factory.CreateClient();
var response = await client.GetAsync("/sitemap.xml");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("<loc>https://test.andreaslop.nl/</loc>");
}
}
@@ -133,6 +133,16 @@ public class SiteContractTests
config.GetSection("SecurityHeaders:AllowedConnectOrigins").Get<string[]>().Should().BeEmpty();
}
[Fact]
public void AndreaSlopFotografie_WritesItsProductionAddressInTheBuild_AndHasNoPublicAddressByDefault()
{
// The build is the same on test and production: the host swaps the authored address for the instance's own.
var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie);
config["Website:AuthoredBaseUrl"].Should().Be("https://andreaslop.nl");
config["Website:PublicBaseUrl"].Should().BeNullOrEmpty("the instance's own address is environment configuration");
}
[Fact]
public void AndreaSlopFotografie_MailBrandingPointsAtTheAdminAssets()
{