private static void SetLimitHeaders(IResponse response, RateLimitResult result)
        {
            var headerResults = RateLimitHeader.Create(result?.Results);

            using (var config = JsConfig.BeginScope())
            {
                config.ExcludeTypeInfo = true;
                foreach (var header in headerResults)
                {
                    response.AddHeader(header.HeaderName, header.Limits.ToJson());
                }
            }
        }
        public void ProcessRequest_ExecutesLuaScript(string sha1, RateLimitResult rateLimitResult)
        {
            var client = A.Fake<IRedisClient>();
            A.CallTo(() => redisManager.GetClient()).Returns(client);
            A.CallTo(() => limitProvider.GetRateLimitScriptId()).Returns(sha1);

            A.CallTo(() => client.ExecLuaSha(A<string>.Ignored, A<string[]>.Ignored, A<string[]>.Ignored))
                .Returns(new RedisText { Text = rateLimitResult.ToJson() });

            var feature = GetSut();
            var mockHttpResponse = new MockHttpResponse();
            feature.ProcessRequest(new MockHttpRequest(), mockHttpResponse, null);

            mockHttpResponse.Headers[Redis.Headers.HttpHeaders.RateLimitUser].Should().NotBeNullOrWhiteSpace();
            mockHttpResponse.Headers[Redis.Headers.HttpHeaders.RateLimitRequest].Should().NotBeNullOrWhiteSpace();
        }
 public void Results_DefaultedToEmptyArray()
 {
     var results = new RateLimitResult();
     results.Results.Should().BeEmpty();
 }
        private void ProcessResult(IResponse response, RateLimitResult rateLimitResult)
        {
            SetLimitHeaders(response, rateLimitResult);

            // NOTE By default we return an empty RateLimitResult which will have an 'Access' value of null.. which we default to true. Is this correct?
            if (!rateLimitResult?.Access ?? true)
            {
                if (log.IsDebugEnabled)
                {
                    var request = response.Request;
                    log.Debug(
                        $"Rate limit exceeded for {request.AbsoluteUri}, user {KeyGenerator.GetConsumerId(request)}. Returning status code: {LimitStatusCode}");
                }
                response.StatusCode = LimitStatusCode;
                response.StatusDescription = StatusDescription;
                response.Close();
            }
        }