Files
slp-modular-cms/src/SlpModularCms.Core.Tests/Hosting/Website/PrerenderedSiteIntegrationTests.cs
T
SluijsensandClaude Sonnet 5.5 d0c1d26c5c Herstel: admin-bestanden geven 404 in PrerenderedPages-modus
De catch-all 404-route van de website werd ook voor /admin/... gekozen, waardoor de
static-file-middleware (die routes met endpoint overslaat) de adminbestanden niet
serveerde. Een routeconstraint houdt de gereserveerde prefixes buiten die route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 11:12:13 +01:00

520 lines
19 KiB
C#

using System.Diagnostics;
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;
using Xunit.Abstractions;
namespace SlpModularCms.Core.Tests.Hosting.Website;
/// <summary>
/// End-to-end tests of the prerendered website on an in-memory server (no database needed):
/// the real security-header, static-content and website extensions over a temporary
/// <c>wwwroot/web</c>, with a stand-in API route for the reserved prefix.
/// </summary>
public sealed class PrerenderedSiteIntegrationTests : IDisposable
{
private const string Umami = "https://analytics.slpsoftware.nl";
private const string Sentry = "https://o4511795618185216.ingest.de.sentry.io";
private readonly string _root = Path.Combine(Path.GetTempPath(), "slp-site-" + Guid.NewGuid().ToString("N"));
private readonly ITestOutputHelper _output;
private readonly List<WebApplication> _apps = [];
public PrerenderedSiteIntegrationTests(ITestOutputHelper output)
{
_output = output;
Write("index.html", "<!doctype html><title>Home</title>");
Write("over-mij.html", "<!doctype html><title>Over mij</title>");
Write("404.html", "<!doctype html><title>Niet gevonden</title>");
Write("assets/app.abc123.js", "console.log('x');" + new string(' ', 2000));
Write("assets/logo.png", "PNG");
Write("blog/index.html", "<!doctype html><title>Blog</title>");
Write(".well-known/security.txt", "Contact: mailto:a@example.com");
Write(".env", "SECRET=1");
}
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 void WriteAdmin(string relative, string content)
{
var path = Path.Combine(_root, "wwwroot", "admin", relative);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, content);
}
// Regression guard (live defect on the first ASF test deploy): the admin SPA's files must be served
// in PrerenderedPages mode, not only its index through the SPA fallback.
[Theory]
[InlineData("/admin/index.html", "text/html")]
[InlineData("/admin/assets/index-abc123.css", "text/css")]
[InlineData("/admin/assets/index-abc123.js", "text/javascript")]
[InlineData("/admin/favicon-theme.png", "image/png")]
public async Task AdminFiles_AreServed_InPrerenderedMode(string path, string contentType)
{
WriteAdmin("index.html", "<!doctype html><title>Admin</title>");
WriteAdmin("assets/index-abc123.css", "body{}");
WriteAdmin("assets/index-abc123.js", "console.log(1)");
WriteAdmin("favicon-theme.png", "PNG");
var client = await StartAsync();
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Content.Headers.ContentType!.MediaType.Should().Be(contentType);
}
[Fact]
public async Task AdminRoot_And_ClientRoutes_ServeTheAdminIndex_InPrerenderedMode()
{
WriteAdmin("index.html", "<!doctype html><title>Admin</title>");
var client = await StartAsync();
foreach (var path in new[] { "/admin/", "/admin/dashboard", "/admin/setup" })
{
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.OK, path);
(await response.Content.ReadAsStringAsync()).Should().Contain("<title>Admin</title>", path);
}
var missing = await client.GetAsync("/admin/assets/missing.js");
missing.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await missing.Content.ReadAsStringAsync()).Should().NotContain("<");
var unknownApi = await client.GetAsync("/api/v1/unknown");
unknownApi.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await unknownApi.Content.ReadAsStringAsync()).Should().BeEmpty();
// The website keeps its own 404 page for a missing asset.
var siteMissing = await client.GetAsync("/assets/missing.js");
siteMissing.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await siteMissing.Content.ReadAsStringAsync()).Should().Contain("Niet gevonden");
}
private async Task<HttpClient> StartAsync(bool noIndex = false, bool prerendered = true, Dictionary<string, string?>? extra = null)
{
Directory.CreateDirectory(Path.Combine(_root, "wwwroot"));
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
EnvironmentName = Environments.Production,
ContentRootPath = _root,
WebRootPath = Path.Combine(_root, "wwwroot")
});
builder.WebHost.UseTestServer();
builder.Logging.ClearProviders();
var settings = new Dictionary<string, string?>
{
["SecurityHeaders:DefaultPolicy"] = "StrictSite",
["SecurityHeaders:AllowedSiteScriptOrigins:0"] = Umami,
["SecurityHeaders:AllowedSiteConnectOrigins:0"] = Umami,
["SecurityHeaders:AllowedSiteConnectOrigins:1"] = Sentry,
["Website:NoIndex"] = noIndex ? "true" : "false"
};
foreach (var (key, value) in extra ?? [])
{
settings[key] = value;
}
builder.Configuration.AddInMemoryCollection(settings);
builder.Services.AddCmsSecurityHeaders(builder.Configuration);
if (prerendered)
{
builder.Services.AddCmsWebsite(builder.Configuration);
}
var app = builder.Build();
var mode = prerendered ? WebsiteMode.PrerenderedPages : WebsiteMode.SpaFallback;
app.UseCmsSecurityHeaders();
app.UseCmsStaticContent(mode);
if (prerendered)
{
app.UseCmsPrerenderedSite();
}
app.MapGet("/api/v1/ping", () => Results.Text("pong"));
app.MapPost("/sentry-stand-in", () => Results.Ok("tunnel"));
app.MapCmsSpaFallbacks(mode);
if (prerendered)
{
app.MapCmsWebsiteNotFound();
}
await app.StartAsync();
_apps.Add(app);
var client = app.GetTestClient();
return client;
}
private static string[] Header(HttpResponseMessage response, string name) =>
response.Headers.TryGetValues(name, out var values) ? values.ToArray()
: response.Content.Headers.TryGetValues(name, out var cvalues) ? cvalues.ToArray()
: [];
// The full header set Q1 = A requires on every website response.
private static void AssertFullHeaderSet(HttpResponseMessage response, string because)
{
Header(response, "Content-Security-Policy").Should().ContainSingle(because)
.Which.Should().StartWith("default-src 'self'; script-src 'self' " + Umami);
Header(response, "Strict-Transport-Security").Should().Equal(["max-age=31536000; includeSubDomains"], because);
Header(response, "X-Content-Type-Options").Should().Equal(["nosniff"], because);
Header(response, "X-Frame-Options").Should().Equal(["DENY"], because);
Header(response, "Referrer-Policy").Should().Equal(["strict-origin-when-cross-origin"], because);
}
private static HttpClient NoRedirect(HttpClient client) => client;
[Fact]
public async Task Root_ServesIndex_NoCache_WithFullHeaders()
{
var client = await StartAsync();
var response = await client.GetAsync("/");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("<title>Home</title>");
response.Content.Headers.ContentType!.ToString().Should().Be("text/html; charset=utf-8");
Header(response, "Cache-Control").Should().Equal(["no-cache"]);
AssertFullHeaderSet(response, "page");
}
[Fact]
public async Task Page_IsServedFromExtensionlessUrl()
{
var client = await StartAsync();
var response = await client.GetAsync("/over-mij");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("Over mij");
}
[Theory]
[InlineData("/Over-Mij?x=1", "/over-mij?x=1")]
[InlineData("/over-mij.html", "/over-mij")]
[InlineData("/over-mij/", "/over-mij")]
[InlineData("/index", "/")]
[InlineData("/index.html", "/")]
[InlineData("/onbekend/", "/onbekend")]
public async Task NonCanonicalUrl_Gets_One301_WithRelativeLocation_AndFullHeaders(string path, string location)
{
var client = await StartAsync();
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.MovedPermanently);
response.Headers.Location!.OriginalString.Should().Be(location);
AssertFullHeaderSet(response, "301");
}
[Fact]
public async Task Asset_IsImmutable_AndCarriesTheFullHeaderSet()
{
var client = await StartAsync();
var response = await client.GetAsync("/assets/logo.png");
response.StatusCode.Should().Be(HttpStatusCode.OK);
Header(response, "Cache-Control").Should().Equal(["public, max-age=31536000, immutable"]);
AssertFullHeaderSet(response, "asset");
}
[Fact]
public async Task UnknownPath_Gets404Page_WithStatus404_AndFullHeaders()
{
var client = await StartAsync();
var response = await client.GetAsync("/onbekend");
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().Contain("Niet gevonden");
response.Content.Headers.ContentType!.ToString().Should().Be("text/html; charset=utf-8");
AssertFullHeaderSet(response, "404");
}
[Theory]
[InlineData("/404")]
[InlineData("/404.html")]
[InlineData("/assets/missing.js")]
[InlineData("/assets")]
public async Task ForbiddenOrMissing_Is404WithThe404Page(string path)
{
var client = await StartAsync();
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().Contain("Niet gevonden");
}
[Fact]
public async Task Missing404Html_GivesAPlain404()
{
File.Delete(Path.Combine(_root, "wwwroot", "web", "404.html"));
var client = await StartAsync();
var response = await client.GetAsync("/onbekend");
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
}
[Theory]
[InlineData("/.env")]
[InlineData("/.git/config")]
[InlineData("/assets/.hidden")]
public async Task Dotfiles_Are404_AndNeverServed(string path)
{
var client = await StartAsync();
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().NotContain("SECRET");
}
[Fact]
public async Task WellKnown_IsServed()
{
var client = await StartAsync();
var response = await client.GetAsync("/.well-known/security.txt");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("Contact:");
}
[Theory]
[InlineData("/../secret")]
[InlineData("/%2e%2e/secret")]
[InlineData("/assets/..%2f..%2fsecret")]
public async Task Traversal_Is404(string path)
{
var client = await StartAsync();
var response = await client.GetAsync(path);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task ReservedApiPath_IsHandledByTheApi_AndUnknownApiPathStaysA404WithoutTheWebsitePage()
{
var client = await StartAsync();
var known = await client.GetAsync("/api/v1/ping");
var unknown = await client.GetAsync("/api/v1/does-not-exist");
known.StatusCode.Should().Be(HttpStatusCode.OK);
(await known.Content.ReadAsStringAsync()).Should().Be("pong");
unknown.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await unknown.Content.ReadAsStringAsync()).Should().BeEmpty();
}
[Fact]
public async Task NonReservedPostIsLeftToEndpoints()
{
var client = await StartAsync();
var tunnel = await client.PostAsync("/sentry-stand-in", new StringContent("x"));
var other = await client.PostAsync("/over-mij", new StringContent("x"));
tunnel.StatusCode.Should().Be(HttpStatusCode.OK);
other.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed);
other.Content.Headers.Allow.Should().BeEquivalentTo(["GET", "HEAD"]);
}
[Fact]
public async Task Head_BehavesLikeGetWithoutBody()
{
var client = await StartAsync();
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, "/over-mij"));
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
Header(response, "Content-Length").Should().NotBeEmpty();
}
[Fact]
public async Task NoIndex_AddsRobotsHeaderEverywhere_AndServesDisallowAll()
{
var client = await StartAsync(noIndex: true);
var page = await client.GetAsync("/over-mij");
var asset = await client.GetAsync("/assets/logo.png");
var redirect = await client.GetAsync("/Over-Mij");
var notFound = await client.GetAsync("/onbekend");
var robots = await client.GetAsync("/robots.txt");
foreach (var response in new[] { page, asset, redirect, notFound, robots })
{
Header(response, "X-Robots-Tag").Should().Equal(["noindex, nofollow"]);
}
robots.StatusCode.Should().Be(HttpStatusCode.OK);
(await robots.Content.ReadAsStringAsync()).Should().Be("User-agent: *\nDisallow: /\n");
AssertFullHeaderSet(robots, "robots.txt");
}
[Fact]
public async Task WithoutNoIndex_NoRobotsHeader_AndMissingRobotsTxtIs404()
{
var client = await StartAsync();
var page = await client.GetAsync("/over-mij");
var robots = await client.GetAsync("/robots.txt");
Header(page, "X-Robots-Tag").Should().BeEmpty();
robots.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task Gzip_IsUsedForTextTypes_AndBrotliIsNot()
{
var client = await StartAsync();
var request = new HttpRequestMessage(HttpMethod.Get, "/assets/app.abc123.js");
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("br"));
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
var js = await client.SendAsync(request);
var imageRequest = new HttpRequestMessage(HttpMethod.Get, "/assets/logo.png");
imageRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
var png = await client.SendAsync(imageRequest);
Header(js, "Content-Encoding").Should().Equal(["gzip"]);
Header(js, "Vary").Should().Contain("Accept-Encoding");
Header(png, "Content-Encoding").Should().BeEmpty();
}
[Fact]
public async Task ApiResponses_AreNotCompressed()
{
var client = await StartAsync();
var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/ping");
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
var response = await client.SendAsync(request);
Header(response, "Content-Encoding").Should().BeEmpty();
}
[Fact]
public async Task ApiResponses_KeepTheHtmlOnlyHeaderRule()
{
var client = await StartAsync();
var response = await client.GetAsync("/api/v1/ping");
// text/plain from the stand-in route: nosniff and HSTS yes, document headers no.
Header(response, "X-Content-Type-Options").Should().Equal(["nosniff"]);
Header(response, "Content-Security-Policy").Should().BeEmpty();
}
[Fact]
public async Task MissingWebsiteDirectory_KeepsTheHostAlive_AndAnswers404()
{
Directory.Delete(Path.Combine(_root, "wwwroot", "web"), recursive: true);
var client = await StartAsync();
var page = await client.GetAsync("/over-mij");
var api = await client.GetAsync("/api/v1/ping");
page.StatusCode.Should().Be(HttpStatusCode.NotFound);
api.StatusCode.Should().Be(HttpStatusCode.OK);
}
[Fact]
public async Task WebsiteDeliveredAfterStartup_IsServedWithoutRestart()
{
var client = await StartAsync();
(await client.GetAsync("/nieuw")).StatusCode.Should().Be(HttpStatusCode.NotFound);
Write("nieuw.html", "<!doctype html><title>Nieuw</title>");
(await client.GetAsync("/nieuw")).StatusCode.Should().Be(HttpStatusCode.OK);
}
[Fact]
public async Task PlainHttp_NeverRedirectsToHttps_AndLocationsAreRelative()
{
var client = await StartAsync();
var response = await client.GetAsync("/Over-Mij");
response.Headers.Location!.IsAbsoluteUri.Should().BeFalse();
}
[Fact]
public async Task SpaFallbackMode_StaysUntouched_UnknownPathsAreNotTheWebsite404()
{
var client = await StartAsync(prerendered: false);
var response = await client.GetAsync("/Over-Mij");
// Old behaviour: no canonical redirect, SPA-style handling (index.html fallback).
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("<title>Home</title>");
Header(response, "Content-Security-Policy").Should().NotBeEmpty();
}
[Fact]
public async Task LocalTimingCheck_PageServing_IsFast()
{
var client = await StartAsync();
await client.GetAsync("/over-mij");
const int iterations = 500;
var watch = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
(await client.GetAsync("/over-mij")).EnsureSuccessStatusCode();
}
watch.Stop();
var perRequest = watch.Elapsed.TotalMilliseconds / iterations;
// Recorded in the hand-back note (NFR-PF-02); the bound only catches pathological regressions.
_output.WriteLine($"TIMING: {iterations} GET /over-mij via in-memory server = {perRequest:F3} ms/request");
perRequest.Should().BeLessThan(20);
}
}