Exemple #1
0
        public void LimitProviderIntervalReject()
        {
            var limitProvider = new SlidingWindowWithInterval();

            var timeStamp = _dateTimeService.Timestamp;

            var evaluationContext = new EvaluationContext
            {
                RequestContext = new RequestContext
                {
                    ClientContext = new ClientContext {
                        Token = "test"
                    },
                    TimeStamp = _dateTimeService.Timestamp
                },
                RuleSet = new List <LimitRule>
                {
                    new LimitRule {
                        Threshold = 1, TimeSpan = 10
                    }                                             // a request per 10 seconds
                }
            };

            var visitContext = new VisitContext
            {
                Counter     = 1,
                LastAccess  = timeStamp - 5,
                WindowStart = timeStamp - 5
            };

            Assert.IsTrue(visitContext.Equals(limitProvider.Evaluate(evaluationContext, visitContext)));

            evaluationContext.RequestContext.TimeStamp += 6;
            visitContext = limitProvider.Evaluate(evaluationContext, visitContext);

            Assert.IsTrue(visitContext.LastAccess == evaluationContext.RequestContext.TimeStamp);
        }
Exemple #2
0
        public void LimitProviderUpdatedWindow()
        {
            var limitProvider = new SlidingWindowWithInterval();

            var timeStamp = _dateTimeService.Timestamp;

            var evaluationContext = new EvaluationContext
            {
                RequestContext = new RequestContext
                {
                    ClientContext = new ClientContext {
                        Token = "test"
                    },
                    TimeStamp = _dateTimeService.Timestamp
                },
                RuleSet = new List <LimitRule>
                {
                    new LimitRule {
                        Threshold = 5, TimeSpan = 10
                    }                                             // Represents 5 request per 10 second
                }
            };

            var visitContext = new VisitContext
            {
                Counter     = 4,
                LastAccess  = timeStamp - 1,
                WindowStart = timeStamp - 11
            };

            visitContext = limitProvider.Evaluate(evaluationContext, visitContext);

            Assert.IsTrue(visitContext.Counter == 1);
            Assert.IsTrue(visitContext.LastAccess == timeStamp);
            Assert.IsTrue(visitContext.WindowStart == timeStamp);
        }