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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user