From 7fd690c17bed1e465ecc557fb199cdb3a94d3f0a Mon Sep 17 00:00:00 2001 From: Konstantin Semenenko Date: Wed, 24 May 2023 16:40:49 +0200 Subject: [PATCH] fix tests --- .../Extensions/ClientBuilderExtensions.cs | 1 + .../Extensions/ServiceCollectionExtensions.cs | 14 ++++++++++ .../OrleansBaseRateLimitingMiddleware.cs | 2 +- .../OrleansIpRateLimitingMiddleware.cs | 6 ++-- .../OrleansUserRateLimitingMiddleware.cs | 28 +++++++++---------- .../SignalRTests.cs | 13 +++++++++ .../TestApp/Controllers/TestController.cs | 2 +- .../TestApp/HttpHostProgram.cs | 8 ++++-- 8 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 ManagedCode.Orleans.RateLimiting.Client/Extensions/ServiceCollectionExtensions.cs diff --git a/ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs b/ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs index a6f2b65..aaa01a6 100644 --- a/ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs +++ b/ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Builder; using Orleans.Hosting; namespace ManagedCode.Orleans.RateLimiting.Client.Extensions; diff --git a/ManagedCode.Orleans.RateLimiting.Client/Extensions/ServiceCollectionExtensions.cs b/ManagedCode.Orleans.RateLimiting.Client/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..2bf3c8d --- /dev/null +++ b/ManagedCode.Orleans.RateLimiting.Client/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using ManagedCode.Orleans.RateLimiting.Client.Middlewares; +using Microsoft.Extensions.DependencyInjection; + +namespace ManagedCode.Orleans.RateLimiting.Client.Extensions; + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddOrleansRateLimiting(this IServiceCollection collection) + { + //collection.AddTransient(); + //collection.AddTransient(); + return collection; + } +} \ No newline at end of file diff --git a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs index 34ddce5..1f6d70c 100644 --- a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs +++ b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs @@ -82,7 +82,7 @@ public async Task Invoke(HttpContext httpContext) return (attribute, postfix); } - protected ILimiterHolder? TryGetLimiterHolder(HttpContext httpContext, string key, string configurationName) + protected ILimiterHolder? TryGetLimiterHolder(string key, string configurationName) { var limiter = _client.GetRateLimiterByConfig(key, configurationName, _services.GetService>()); diff --git a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansIpRateLimitingMiddleware.cs b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansIpRateLimitingMiddleware.cs index a62d407..b5bf7a4 100644 --- a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansIpRateLimitingMiddleware.cs +++ b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansIpRateLimitingMiddleware.cs @@ -10,7 +10,7 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares; public class OrleansIpRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware { - public OrleansIpRateLimitingMiddleware(ILogger logger, RequestDelegate next, IClusterClient client, IServiceProvider services) : + public OrleansIpRateLimitingMiddleware(ILogger logger, IClusterClient client, IServiceProvider services, RequestDelegate next) : base(logger, next, client, services) { } @@ -25,8 +25,8 @@ private bool AddIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder { var attribute = TryGetAttribute(httpContext); if (attribute.HasValue) - return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!), - attribute.Value.postfix!)); + return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!), + attribute.Value.attribute.ConfigurationName)); return false; } diff --git a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansUserRateLimitingMiddleware.cs b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansUserRateLimitingMiddleware.cs index d7611dc..bfef008 100644 --- a/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansUserRateLimitingMiddleware.cs +++ b/ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansUserRateLimitingMiddleware.cs @@ -10,56 +10,54 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares; public class OrleansUserRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware { - protected OrleansUserRateLimitingMiddleware(ILogger logger, RequestDelegate next, IClusterClient client, IServiceProvider services) : base(logger, next, client, - services) + public OrleansUserRateLimitingMiddleware(ILogger logger, IClusterClient client, IServiceProvider services, RequestDelegate next) + : base(logger, next, client, services) { } protected override void AddLimiters(HttpContext httpContext, GroupLimiterHolder holder) { - AddAnonymousIpRateLimiter(httpContext, holder); + AddAnonymousRateLimiter(httpContext, holder); // if user is authenticated add in role limiter - if (!AddInRoleIpRateLimiter(httpContext, holder)) + if (!AddInRoleRateLimiter(httpContext, holder)) // if user is not authenticated add authorized limiter - AddAuthorizedIpRateLimiter(httpContext, holder); + AddAuthorizedRateLimiter(httpContext, holder); } - private bool AddAnonymousIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) + private bool AddAnonymousRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) { if (httpContext.User?.Identity?.IsAuthenticated is not true) { var attribute = TryGetAttribute(httpContext); if (attribute.HasValue) - return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!), - attribute.Value.postfix!)); + return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!), + attribute.Value.attribute.ConfigurationName)); } return false; } - private bool AddAuthorizedIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) + private bool AddAuthorizedRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) { if (httpContext.User?.Identity?.IsAuthenticated is true) { var attribute = TryGetAttribute(httpContext); if (attribute.HasValue) - return holder.AddLimiter(TryGetLimiterHolder(httpContext, - CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.postfix!), attribute.Value.postfix!)); + return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name ?? "rate-user-name", attribute.Value.postfix!), attribute.Value.attribute.ConfigurationName)); } return false; } - private bool AddInRoleIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) + private bool AddInRoleRateLimiter(HttpContext httpContext, GroupLimiterHolder holder) { var attribute = TryGetAttribute(httpContext); if (attribute.HasValue) if (httpContext.User?.Identity?.IsAuthenticated is true && httpContext.User.IsInRole(attribute.Value.attribute.Role)) - return holder.AddLimiter(TryGetLimiterHolder(httpContext, - CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!), - attribute.Value.postfix!)); + return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!), + attribute.Value.attribute.ConfigurationName)); return false; } diff --git a/ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs b/ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs index 5f1994f..2acb12a 100644 --- a/ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs +++ b/ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs @@ -23,8 +23,21 @@ public SignalRTests(TestClusterApplication testApp, ITestOutputHelper outputHelp [Fact] public async Task Some() { + try + { + var anonymousHub11 = _testApp.CreateSignalRClient(nameof(TestHub)); + await anonymousHub11.StartAsync(); + anonymousHub11.State.Should().Be(HubConnectionState.Connected); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + var anonymousHub = _testApp.CreateSignalRClient(nameof(TestHub)); await anonymousHub.StartAsync(); anonymousHub.State.Should().Be(HubConnectionState.Connected); + } } \ No newline at end of file diff --git a/ManagedCode.Orleans.RateLimiting.Tests/TestApp/Controllers/TestController.cs b/ManagedCode.Orleans.RateLimiting.Tests/TestApp/Controllers/TestController.cs index d4984fc..b74584a 100644 --- a/ManagedCode.Orleans.RateLimiting.Tests/TestApp/Controllers/TestController.cs +++ b/ManagedCode.Orleans.RateLimiting.Tests/TestApp/Controllers/TestController.cs @@ -13,7 +13,7 @@ public class TestController : ControllerBase [HttpGet("authorize")] public async Task> Authorize() { - await Task.Delay(300); + await Task.Delay(500); return "Authorize"; } } \ No newline at end of file diff --git a/ManagedCode.Orleans.RateLimiting.Tests/TestApp/HttpHostProgram.cs b/ManagedCode.Orleans.RateLimiting.Tests/TestApp/HttpHostProgram.cs index 3cb35f9..217aaf0 100644 --- a/ManagedCode.Orleans.RateLimiting.Tests/TestApp/HttpHostProgram.cs +++ b/ManagedCode.Orleans.RateLimiting.Tests/TestApp/HttpHostProgram.cs @@ -3,6 +3,7 @@ using ManagedCode.Orleans.RateLimiting.Core.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace ManagedCode.Orleans.RateLimiting.Tests.TestApp; @@ -14,11 +15,14 @@ public static void Main(string[] args) builder.Services.AddControllers(); builder.Services.AddSignalR(); + builder.Services.AddLogging(log=>log.AddSimpleConsole()); + builder.Services.AddOrleansRateLimiting(); + builder.Services.AddOrleansRateLimiterOptions("ip", new FixedWindowRateLimiterOptions { QueueLimit = 5, - PermitLimit = 10, + PermitLimit = 5, Window = TimeSpan.FromSeconds(1) }); @@ -46,7 +50,7 @@ public static void Main(string[] args) app.UseOrleansIpRateLimiting(); app.UseOrleansUserRateLimiting(); - app.UseRateLimiter(); + //app.UseRateLimiter(); app.Run(); }