|
1 | | -using OrchardCoreContrib.HealthChecks.Tests.Tests; |
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Options; |
| 5 | +using Moq; |
| 6 | +using OrchardCore.Environment.Shell.Configuration; |
| 7 | +using OrchardCoreContrib.HealthChecks.Services; |
| 8 | +using OrchardCoreContrib.HealthChecks.Tests.Tests; |
2 | 9 | using System.Net; |
3 | 10 |
|
4 | 11 | namespace OrchardCoreContrib.HealthChecks.Tests; |
@@ -30,4 +37,71 @@ public async Task CorrectOrder_BlockedIp_ShouldReturn403_WithoutConsumingLimit() |
30 | 37 | Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); |
31 | 38 | } |
32 | 39 | } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void BlockingRateLimiting_ShouldRunBefore_RateLimiting() |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + var shellConfiguration = Mock.Of<IShellConfiguration>(); |
| 46 | + var blockingStartup = new BlockingRateLimitingStartup(shellConfiguration); |
| 47 | + var rateLimitingStartup = new RateLimitingStartup(shellConfiguration); |
| 48 | + |
| 49 | + // Act |
| 50 | + var blockingOrder = blockingStartup.Order; |
| 51 | + var rateLimitingOrder = rateLimitingStartup.Order; |
| 52 | + |
| 53 | + // Assert |
| 54 | + Assert.True(blockingOrder < rateLimitingOrder, |
| 55 | + $"BlockingRateLimiting order ({blockingOrder}) should be less than RateLimiting order ({rateLimitingOrder})."); |
| 56 | + } |
| 57 | + |
| 58 | + //[Fact] |
| 59 | + //public async Task BlockingMiddleware_ShouldRunBefore_RateLimitingMiddleware() |
| 60 | + //{ |
| 61 | + // var executed = new List<string>(); |
| 62 | + |
| 63 | + // var builder = WebApplication.CreateBuilder(); |
| 64 | + // builder.Services.AddOptions<HealthChecksBlockingRateLimitingOptions>() |
| 65 | + // .Configure(o => |
| 66 | + // { |
| 67 | + // o.PermitLimit = 1; |
| 68 | + // o.Window = TimeSpan.FromSeconds(5); |
| 69 | + // o.SegmentsPerWindow = 5; |
| 70 | + // o.BlockDuration = TimeSpan.FromSeconds(10); |
| 71 | + // }); |
| 72 | + // builder.Services.AddSingleton<IHealthCheckRateLimiter, HealthCheckRateLimiter>(); |
| 73 | + |
| 74 | + // var app = builder.Build(); |
| 75 | + |
| 76 | + // // Register blocking first |
| 77 | + // app.Use(async (ctx, next) => |
| 78 | + // { |
| 79 | + // executed.Add("Blocking"); |
| 80 | + // //var middleware = new HealthChecksBlockingRateLimitingMiddleware(next, |
| 81 | + // // ctx.RequestServices.GetRequiredService<IOptions<HealthChecksBlockingRateLimitingOptions>>(), |
| 82 | + // // ctx.RequestServices.GetRequiredService<IClock>()); |
| 83 | + // //await middleware.InvokeAsync(ctx); |
| 84 | + // }); |
| 85 | + |
| 86 | + // // Register plain limiter second |
| 87 | + // app.Use(async (ctx, next) => |
| 88 | + // { |
| 89 | + // executed.Add("RateLimiting"); |
| 90 | + // //var middleware = new HealthChecksRateLimitingMiddleware(next, |
| 91 | + // // ctx.RequestServices.GetRequiredService<IHealthCheckRateLimiter>()); |
| 92 | + // //await middleware.InvokeAsync(ctx); |
| 93 | + // }); |
| 94 | + |
| 95 | + // app.Map("/health", () => Results.Ok("Healthy")); |
| 96 | + |
| 97 | + // var client = app.CreateClient(); |
| 98 | + // var response = await client.GetAsync("/health"); |
| 99 | + |
| 100 | + // // Assert pipeline order |
| 101 | + // Assert.Equal("Blocking", executed[0]); |
| 102 | + // Assert.Equal("RateLimiting", executed[1]); |
| 103 | + |
| 104 | + // // Also assert response is OK for first request |
| 105 | + // Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 106 | + //} |
33 | 107 | } |
0 commit comments