Skip to content

Commit 7fd690c

Browse files
committed
fix tests
1 parent 16117e0 commit 7fd690c

8 files changed

Lines changed: 52 additions & 22 deletions

File tree

ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Builder;
12
using Orleans.Hosting;
23

34
namespace ManagedCode.Orleans.RateLimiting.Client.Extensions;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ManagedCode.Orleans.RateLimiting.Client.Middlewares;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace ManagedCode.Orleans.RateLimiting.Client.Extensions;
5+
6+
public static class ServiceCollectionExtensions
7+
{
8+
public static IServiceCollection AddOrleansRateLimiting(this IServiceCollection collection)
9+
{
10+
//collection.AddTransient<OrleansUserRateLimitingMiddleware>();
11+
//collection.AddTransient<OrleansIpRateLimitingMiddleware>();
12+
return collection;
13+
}
14+
}

ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async Task Invoke(HttpContext httpContext)
8282
return (attribute, postfix);
8383
}
8484

85-
protected ILimiterHolder? TryGetLimiterHolder(HttpContext httpContext, string key, string configurationName)
85+
protected ILimiterHolder? TryGetLimiterHolder(string key, string configurationName)
8686
{
8787
var limiter = _client.GetRateLimiterByConfig(key, configurationName, _services.GetService<IEnumerable<RateLimiterConfig>>());
8888

ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansIpRateLimitingMiddleware.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares;
1010

1111
public class OrleansIpRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware
1212
{
13-
public OrleansIpRateLimitingMiddleware(ILogger<OrleansIpRateLimitingMiddleware> logger, RequestDelegate next, IClusterClient client, IServiceProvider services) :
13+
public OrleansIpRateLimitingMiddleware(ILogger<OrleansIpRateLimitingMiddleware> logger, IClusterClient client, IServiceProvider services, RequestDelegate next) :
1414
base(logger, next, client, services)
1515
{
1616
}
@@ -25,8 +25,8 @@ private bool AddIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder
2525
{
2626
var attribute = TryGetAttribute<IpRateLimiterAttribute>(httpContext);
2727
if (attribute.HasValue)
28-
return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
29-
attribute.Value.postfix!));
28+
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
29+
attribute.Value.attribute.ConfigurationName));
3030

3131
return false;
3232
}

ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansUserRateLimitingMiddleware.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,54 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares;
1010

1111
public class OrleansUserRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware
1212
{
13-
protected OrleansUserRateLimitingMiddleware(ILogger logger, RequestDelegate next, IClusterClient client, IServiceProvider services) : base(logger, next, client,
14-
services)
13+
public OrleansUserRateLimitingMiddleware(ILogger<OrleansUserRateLimitingMiddleware> logger, IClusterClient client, IServiceProvider services, RequestDelegate next)
14+
: base(logger, next, client, services)
1515
{
1616
}
1717

1818

1919
protected override void AddLimiters(HttpContext httpContext, GroupLimiterHolder holder)
2020
{
21-
AddAnonymousIpRateLimiter(httpContext, holder);
21+
AddAnonymousRateLimiter(httpContext, holder);
2222

2323
// if user is authenticated add in role limiter
24-
if (!AddInRoleIpRateLimiter(httpContext, holder))
24+
if (!AddInRoleRateLimiter(httpContext, holder))
2525
// if user is not authenticated add authorized limiter
26-
AddAuthorizedIpRateLimiter(httpContext, holder);
26+
AddAuthorizedRateLimiter(httpContext, holder);
2727
}
2828

29-
private bool AddAnonymousIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
29+
private bool AddAnonymousRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
3030
{
3131
if (httpContext.User?.Identity?.IsAuthenticated is not true)
3232
{
3333
var attribute = TryGetAttribute<AnonymousIpRateLimiterAttribute>(httpContext);
3434
if (attribute.HasValue)
35-
return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
36-
attribute.Value.postfix!));
35+
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
36+
attribute.Value.attribute.ConfigurationName));
3737
}
3838

3939
return false;
4040
}
4141

42-
private bool AddAuthorizedIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
42+
private bool AddAuthorizedRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
4343
{
4444
if (httpContext.User?.Identity?.IsAuthenticated is true)
4545
{
4646
var attribute = TryGetAttribute<AuthorizedIpRateLimiterAttribute>(httpContext);
4747
if (attribute.HasValue)
48-
return holder.AddLimiter(TryGetLimiterHolder(httpContext,
49-
CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.postfix!), attribute.Value.postfix!));
48+
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name ?? "rate-user-name", attribute.Value.postfix!), attribute.Value.attribute.ConfigurationName));
5049
}
5150

5251
return false;
5352
}
5453

55-
private bool AddInRoleIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
54+
private bool AddInRoleRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
5655
{
5756
var attribute = TryGetAttribute<InRoleIpRateLimiterAttribute>(httpContext);
5857
if (attribute.HasValue)
5958
if (httpContext.User?.Identity?.IsAuthenticated is true && httpContext.User.IsInRole(attribute.Value.attribute.Role))
60-
return holder.AddLimiter(TryGetLimiterHolder(httpContext,
61-
CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!),
62-
attribute.Value.postfix!));
59+
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!),
60+
attribute.Value.attribute.ConfigurationName));
6361

6462
return false;
6563
}

ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@ public SignalRTests(TestClusterApplication testApp, ITestOutputHelper outputHelp
2323
[Fact]
2424
public async Task Some()
2525
{
26+
try
27+
{
28+
var anonymousHub11 = _testApp.CreateSignalRClient(nameof(TestHub));
29+
await anonymousHub11.StartAsync();
30+
anonymousHub11.State.Should().Be(HubConnectionState.Connected);
31+
}
32+
catch (Exception e)
33+
{
34+
Console.WriteLine(e);
35+
throw;
36+
}
37+
2638
var anonymousHub = _testApp.CreateSignalRClient(nameof(TestHub));
2739
await anonymousHub.StartAsync();
2840
anonymousHub.State.Should().Be(HubConnectionState.Connected);
41+
2942
}
3043
}

ManagedCode.Orleans.RateLimiting.Tests/TestApp/Controllers/TestController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestController : ControllerBase
1313
[HttpGet("authorize")]
1414
public async Task<ActionResult<string>> Authorize()
1515
{
16-
await Task.Delay(300);
16+
await Task.Delay(500);
1717
return "Authorize";
1818
}
1919
}

ManagedCode.Orleans.RateLimiting.Tests/TestApp/HttpHostProgram.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ManagedCode.Orleans.RateLimiting.Core.Extensions;
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
67

78
namespace ManagedCode.Orleans.RateLimiting.Tests.TestApp;
89

@@ -14,11 +15,14 @@ public static void Main(string[] args)
1415

1516
builder.Services.AddControllers();
1617
builder.Services.AddSignalR();
18+
builder.Services.AddLogging(log=>log.AddSimpleConsole());
1719

20+
builder.Services.AddOrleansRateLimiting();
21+
1822
builder.Services.AddOrleansRateLimiterOptions("ip", new FixedWindowRateLimiterOptions
1923
{
2024
QueueLimit = 5,
21-
PermitLimit = 10,
25+
PermitLimit = 5,
2226
Window = TimeSpan.FromSeconds(1)
2327
});
2428

@@ -46,7 +50,7 @@ public static void Main(string[] args)
4650
app.UseOrleansIpRateLimiting();
4751
app.UseOrleansUserRateLimiting();
4852

49-
app.UseRateLimiter();
53+
//app.UseRateLimiter();
5054

5155
app.Run();
5256
}

0 commit comments

Comments
 (0)