Sessie: een start-refresh zonder oordeel (429/5xx/netwerk) zet de gebruiker niet meer op gast maar toont een retry-scherm; Retry-After wordt gestuurd en gerespecteerd

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 21:04:49 +01:00
co-authored by Claude Sonnet 5.5
parent b8f6b25121
commit 64a467b41b
12 changed files with 297 additions and 12 deletions
@@ -0,0 +1,52 @@
using System.Net;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SlpModularCms.Core.Hosting;
namespace SlpModularCms.Core.Tests.Hosting;
/// <summary>A rate-limited call says how long to wait (Retry-After), so the admin app's start-up retries do not hammer the same window.</summary>
public class RateLimitRetryAfterTests
{
[Fact]
public async Task ARejectedCall_Is429_WithARetryAfterInWholeSeconds()
{
var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?>
{
["RateLimiting:Refresh:PermitLimit"] = "1",
["RateLimiting:Refresh:WindowSeconds"] = "60"
}).Build();
using var host = await new HostBuilder().ConfigureWebHost(web =>
{
web.UseTestServer();
web.ConfigureServices(s =>
{
s.AddLogging();
s.AddRouting();
s.AddCmsRateLimiting(config);
});
web.Configure(app =>
{
app.UseRouting();
app.UseRateLimiter();
app.UseEndpoints(e => e.MapPost("/refresh", () => "ok").RequireRateLimiting("refresh"));
});
}).StartAsync();
var client = host.GetTestClient();
(await client.PostAsync("/refresh", null)).StatusCode.Should().Be(HttpStatusCode.OK);
var rejected = await client.PostAsync("/refresh", null);
rejected.StatusCode.Should().Be(HttpStatusCode.TooManyRequests);
rejected.Headers.RetryAfter.Should().NotBeNull();
rejected.Headers.RetryAfter!.Delta!.Value.TotalSeconds.Should().BeGreaterThanOrEqualTo(1).And.BeLessThanOrEqualTo(60);
}
}
@@ -183,6 +183,29 @@ public static class ServiceCollectionExtensions
.GetMetadata<EnableRateLimitingAttribute>()?.PolicyName ?? "(unknown)",
context.HttpContext.Request.Path);
// Tells a well-behaved client (the admin app's start-up retries) how long to wait, instead of
// having it retry into the same window and stay limited.
// A sliding-window lease carries no RetryAfter, so the "refresh" policy falls back to one segment
// (a quarter of the window: the earliest moment a permit frees up); other policies to their window.
var policyName = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<EnableRateLimitingAttribute>()?.PolicyName;
var section = policyName switch
{
"login" => "Login",
"refresh" => "Refresh",
_ => null
};
double? waitSeconds = null;
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
waitSeconds = retryAfter.TotalSeconds;
else if (section is not null)
{
var window = configuration.GetValue<int>($"RateLimiting:{section}:WindowSeconds", 60);
waitSeconds = policyName == "refresh" ? window / 4.0 : window;
}
if (waitSeconds is not null)
context.HttpContext.Response.Headers.RetryAfter =
Math.Max(1, (int)Math.Ceiling(waitSeconds.Value)).ToString(System.Globalization.CultureInfo.InvariantCulture);
return ValueTask.CompletedTask;
};