U6: IndexHtmlInjector (markers OFFERINGS-JSON-LD / OFFERINGS-HTML), 60s cache, invalidatie bij wijziging, ETag
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,7 +34,76 @@ public sealed class SpaSeoTests : IDisposable
|
||||
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
|
||||
}
|
||||
|
||||
private async Task<HttpClient> StartAsync(Dictionary<string, string?> settings)
|
||||
private sealed class CountingProvider : IIndexHtmlMarkerProvider
|
||||
{
|
||||
public int Calls;
|
||||
public string Value = "one";
|
||||
public bool Fail;
|
||||
|
||||
public Task<IReadOnlyDictionary<string, string>> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken)
|
||||
{
|
||||
Interlocked.Increment(ref Calls);
|
||||
if (Fail) throw new InvalidOperationException("db down");
|
||||
return Task.FromResult<IReadOnlyDictionary<string, string>>(
|
||||
new Dictionary<string, string> { ["SLP:TEST"] = $"[{Value}@{publicBaseUrl}]" });
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<(HttpClient Client, CountingProvider Provider, IIndexHtmlCache Cache)> StartWithMarkersAsync()
|
||||
{
|
||||
File.WriteAllText(Path.Combine(_root, "wwwroot", "web", "index.html"), "<body><!--SLP:TEST--><div id=\"root\"></div></body>");
|
||||
var provider = new CountingProvider();
|
||||
var client = await StartAsync(Config(noIndex: false, baseUrl: "https://slpsoftware.nl"), services => services.AddSingleton<IIndexHtmlMarkerProvider>(provider));
|
||||
return (client, provider, _apps[^1].Services.GetRequiredService<IIndexHtmlCache>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Markers_AreFilled_CachedAndRefreshedOnInvalidate_WithAnETag()
|
||||
{
|
||||
var (client, provider, cache) = await StartWithMarkersAsync();
|
||||
|
||||
var first = await client.GetAsync("/");
|
||||
(await first.Content.ReadAsStringAsync()).Should().Contain("[one@https://slpsoftware.nl]").And.NotContain("<!--SLP:TEST-->");
|
||||
first.Headers.CacheControl!.NoCache.Should().BeTrue();
|
||||
var etag = first.Headers.ETag!;
|
||||
|
||||
provider.Value = "two";
|
||||
(await client.GetStringAsync("/privacy")).Should().Contain("[one@"); // served from the cache
|
||||
provider.Calls.Should().Be(1);
|
||||
|
||||
cache.Invalidate();
|
||||
var second = await client.GetAsync("/");
|
||||
(await second.Content.ReadAsStringAsync()).Should().Contain("[two@");
|
||||
second.Headers.ETag.Should().NotBe(etag);
|
||||
provider.Calls.Should().Be(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AMatchingETag_Gets304()
|
||||
{
|
||||
var (client, _, _) = await StartWithMarkersAsync();
|
||||
var etag = (await client.GetAsync("/")).Headers.ETag!;
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "/");
|
||||
request.Headers.IfNoneMatch.Add(etag);
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotModified);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenAProviderFails_ThePlainPageIsStillServed()
|
||||
{
|
||||
var (client, provider, _) = await StartWithMarkersAsync();
|
||||
provider.Fail = true;
|
||||
|
||||
var response = await client.GetAsync("/");
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
(await response.Content.ReadAsStringAsync()).Should().Contain("<!--SLP:TEST-->").And.Contain("id=\"root\"");
|
||||
}
|
||||
|
||||
private async Task<HttpClient> StartAsync(Dictionary<string, string?> settings, Action<IServiceCollection>? configure = null)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
|
||||
{
|
||||
@@ -48,6 +117,7 @@ public sealed class SpaSeoTests : IDisposable
|
||||
builder.Configuration.AddInMemoryCollection(settings);
|
||||
builder.Services.AddCmsSecurityHeaders(builder.Configuration);
|
||||
builder.Services.AddCmsSpaSeo(builder.Configuration);
|
||||
configure?.Invoke(builder.Services);
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseCmsSecurityHeaders();
|
||||
|
||||
Reference in New Issue
Block a user