public async Task PolicyDirectlyOnEndpoint_GetsUsed() { var globalOnRejectedInvoked = false; var options = CreateOptionsAccessor(); // Policy will disallow var policy = new TestRateLimiterPolicy("myKey", 404, false); var defaultRateLimiterPolicy = new DefaultRateLimiterPolicy(RateLimiterOptions.ConvertPartitioner <string>(null, policy.GetPartition), policy.OnRejected); options.Value.OnRejected = (context, token) => { globalOnRejectedInvoked = true; context.HttpContext.Response.StatusCode = 429; return(ValueTask.CompletedTask); }; var middleware = new RateLimitingMiddleware(c => { return(Task.CompletedTask); }, new NullLoggerFactory().CreateLogger <RateLimitingMiddleware>(), options, Mock.Of <IServiceProvider>()); var context = new DefaultHttpContext(); context.SetEndpoint(new Endpoint(c => Task.CompletedTask, new EndpointMetadataCollection(new EnableRateLimitingAttribute(defaultRateLimiterPolicy)), "Test endpoint")); await middleware.Invoke(context).DefaultTimeout(); Assert.False(globalOnRejectedInvoked); Assert.Equal(StatusCodes.Status404NotFound, context.Response.StatusCode); }
internal EnableRateLimitingAttribute(DefaultRateLimiterPolicy policy) { Policy = policy; }