Exemplo n.º 1
0
        public void SuccessfullyMakeCallsAfterThrottlingEnds()
        {
            var throttle = new ThrottleAttribute()
            {
                CallsPerRateLimitSeconds = 1, RateLimitSeconds = 1
            };
            var authCode = "Key8" + Guid.NewGuid();
            var context  = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context);
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            var context2 = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context2);

            Assert.AreEqual(context2.Response.StatusCode.ToString(), 429.ToString());


            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));

            var context3 = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context3);

            Assert.AreEqual(context3.Response, null);
        }
Exemplo n.º 2
0
 public IHttpActionResult ClearCaching()
 {
     _gameStateService.Calculate();
     _tilgangsKontroll.Nullstill();
     _posisjonsService.Nullstill();
     ThrottleAttribute.Reload();
     return(Ok());
 }
Exemplo n.º 3
0
        public void DefaultsAreSetForThrottling()
        {
            var throttle = new ThrottleAttribute();

            var context = CreateHttpContext("Key" + Guid.NewGuid());

            throttle.OnActionExecuting(context);
            throttle.OnActionExecuting(context);

            Assert.AreEqual(throttle.CallsPerRateLimitSeconds, 10);
            Assert.AreEqual(throttle.RateLimitSeconds, 30);
        }
Exemplo n.º 4
0
        public void CallsDoNotExceedLimit()
        {
            var throttle = new ThrottleAttribute {
                CallsPerRateLimitSeconds = 3, RateLimitSeconds = 1
            };
            var authCode = "Key2" + Guid.NewGuid();
            var context  = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context);

            var context2 = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context2);

            Assert.AreEqual(context2.Response, null);
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TBuilder"></typeparam>
        /// <param name="builder"></param>
        /// <param name="perSecond"></param>
        /// <param name="perMinute"></param>
        /// <param name="perHour"></param>
        /// <param name="perDay"></param>
        /// <param name="policyName"></param>
        /// <returns></returns>
        public static TBuilder Throttle <TBuilder>(this TBuilder builder, long?perSecond = null, long?perMinute = null, long?perHour = null, long?perDay = null, string policyName = null) where TBuilder : IEndpointConventionBuilder
        {
            builder.Add(endpointBuilder =>
            {
                var throttleAttribute = new ThrottleAttribute
                {
                    PerSecond  = perSecond ?? 0,
                    PerMinute  = perMinute ?? 0,
                    PerHour    = perHour ?? 0,
                    PerDay     = perDay ?? 0,
                    PolicyName = policyName
                };

                endpointBuilder.Metadata.Add(throttleAttribute);
            });
            return(builder);
        }
Exemplo n.º 6
0
        public void CallsExceedAllowedCallsInTimeframe()
        {
            var throttle = new ThrottleAttribute()
            {
                CallsPerRateLimitSeconds = 1, RateLimitSeconds = 1
            };
            var authCode = "Key1" + Guid.NewGuid();
            var context  = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context);
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            var context2 = CreateHttpContext(authCode);

            throttle.OnActionExecuting(context2);

            Assert.AreEqual(context2.Response.StatusCode.ToString(), 429.ToString());
        }
Exemplo n.º 7
0
        public void MultipleCallsFromDifferentUsersDoNotExceed()
        {
            var throttle = new ThrottleAttribute()
            {
                CallsPerRateLimitSeconds = 1, RateLimitSeconds = 1
            };

            var context = CreateHttpContext("Key3" + Guid.NewGuid());

            throttle.OnActionExecuting(context);

            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            var context2 = CreateHttpContext("Key4" + Guid.NewGuid());

            throttle.OnActionExecuting(context2);

            Assert.AreEqual(context2.Response, null);
        }