public async void RequestIsMadeWithoutClientIdAndAnonymousClientsAreNotAllowed()
        {
            // Given
            var testServer = TestServerFactory.Create(services =>
            {
                services.AddCongestionControl(options =>
                {
                    options.AllowAnonymousClients = false;
                    options.AddRequestRateLimiter();
                });

                services.AddSingleton <IStartupFilter, StartupFilterWithCongestionControl>();
            });

            var client = testServer.CreateClient();

            // When the request made
            var response = await client.GetAsync("api/values");

            // Then it should result in unauthorized response
            response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
        }
        public async void Making5RequestsToApiLimitedAt2RequestsPer10SecondsWithBurstingFactorOf2UsingRedis()
        {
            // Given
            const int AsyncRequests = 5;

            var testServer = TestServerFactory.Create(services =>
            {
                services.AddCongestionControl(options =>
                {
                    // Try another client identifier strategy for good measure
                    options.AddQueryBasedClientIdentifierProvider();
                    options.AddRedisStorage("127.0.0.1:6379");
                    options.AddRequestRateLimiter(rrl =>
                    {
                        rrl.AverageRate = 2;
                        rrl.Interval    = 10;
                        rrl.Bursting    = 2;
                    });
                });

                services.AddSingleton <IStartupFilter, StartupFilterWithCongestionControl>();
            });

            var client = testServer.CreateClient();

            // When the requests are made
            var tasks  = new List <Task <HttpResponseMessage> >();
            var apiKey = Guid.NewGuid().ToString();

            for (var i = 0; i < AsyncRequests; i++)
            {
                tasks.Add(client.GetAsync($"api/values?api_key={apiKey}"));
            }

            var response = await Task.WhenAll(tasks);

            // Then it should not allow 1 out of 5 requests
            response.SingleOrDefault(resp => resp.StatusCode == HttpStatusCode.TooManyRequests).Should().NotBeNull();
        }
        public async void Making4RequestsToApiLimitedAt2RequestsPer10SecondsWithBurstingFactorOf2UsingRedis()
        {
            // Given
            const int AsyncRequests = 4;

            var testServer = TestServerFactory.Create(services =>
            {
                services.AddCongestionControl(options =>
                {
                    options.AddRedisStorage("127.0.0.1:6379");
                    options.AddRequestRateLimiter(rrl =>
                    {
                        rrl.AverageRate = 2;
                        rrl.Interval    = 10;
                        rrl.Bursting    = 2;
                    });
                });

                services.AddSingleton <IStartupFilter, StartupFilterWithCongestionControl>();
            });

            var client = testServer.CreateClient();

            client.DefaultRequestHeaders.Add("x-api-key", Guid.NewGuid().ToString());

            // When the requests are made
            var tasks = new List <Task <HttpResponseMessage> >();

            for (var i = 0; i < AsyncRequests; i++)
            {
                tasks.Add(client.GetAsync("api/values"));
            }

            var response = await Task.WhenAll(tasks);

            // Then it should allow all requests
            response.Any(resp => resp.StatusCode != HttpStatusCode.OK).Should().BeFalse();
        }
        public async void Making5RequestsToApiLimitedAt2RequestsPer10SecondsWithBurstingFactorOf2()
        {
            // Given
            const int AsyncRequests = 5;

            var testServer = TestServerFactory.Create(services =>
            {
                services.AddCongestionControl(options =>
                {
                    options.AddRequestRateLimiter(rrl =>
                    {
                        rrl.AverageRate = 2;
                        rrl.Interval    = 10;
                        rrl.Bursting    = 2;
                    });
                });

                services.AddSingleton <IStartupFilter, StartupFilterWithCongestionControl>();
            });

            var client = testServer.CreateClient();

            client.DefaultRequestHeaders.Add("x-api-key", Guid.NewGuid().ToString());

            // When the requests are made
            var tasks = new List <Task <HttpResponseMessage> >();

            for (var i = 0; i < AsyncRequests; i++)
            {
                tasks.Add(client.GetAsync("api/values"));
            }

            var response = await Task.WhenAll(tasks);

            // Then it should not allow 1 out of 5 requests
            response.SingleOrDefault(resp => resp.StatusCode == HttpStatusCode.TooManyRequests).Should().NotBeNull();
        }
Пример #5
0
        public async void Making3ConcurrentRequestsToApiLimitedAt2ConcurrentRequestsPer60Seconds()
        {
            // Given
            const int AsyncRequests = 3;

            var testServer = TestServerFactory.Create(services =>
            {
                services.AddCongestionControl(options =>
                {
                    options.AddConcurrentRequestLimiter(crl =>
                    {
                        crl.Capacity          = 2;
                        crl.RequestTimeToLive = 60;
                    });
                });

                services.AddSingleton <IStartupFilter, StartupFilterWithCongestionControl>();
            });

            var client = testServer.CreateClient();

            client.DefaultRequestHeaders.Add("x-api-key", Guid.NewGuid().ToString());

            // When the requests are made
            var tasks = new List <Task <HttpResponseMessage> >();

            for (var i = 0; i < AsyncRequests; i++)
            {
                tasks.Add(client.GetAsync("api/values"));
            }

            var response = await Task.WhenAll(tasks);

            // Then it should not allow 1 out of 3 requests
            response.SingleOrDefault(resp => resp.StatusCode == HttpStatusCode.TooManyRequests).Should().NotBeNull();
        }