public void SetterAndGetterInSync()
            {
                var target = new FastRequestsProtectionAttribute {
                    RequestsPerSecondAndClient = 42
                };

                Assert.AreEqual(42, target.RequestsPerSecondAndClient);
            }
            public void NotBlocksLessThanConfiguredFastCalls()
            {
                var context = MvcTestBase.CreateRequestContext();

                // we want to allow max 2 requests in one second
                var target = new FastRequestsProtectionAttribute {
                    RequestsPerSecondAndClient = 2
                };

                // calling 2 times should NOT block
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);

                Assert.IsNotNull(target);
                Assert.IsNull(context.Result);
            }
            public void NotBlocksConfiguredFastCallsWithoutClientId()
            {
                // we setup a request context that returns always the same session id
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost"), string.Empty, string.Empty);

                // we want to allow max 2 requests in one second
                var target = new FastRequestsProtectionAttribute {
                    RequestsPerSecondAndClient = 2
                };

                // calling 4 times without client id should not block
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);

                Assert.IsNotNull(target);
            }
            [ExcludeFromCodeCoverage]   // since we expect an exception, we cannot get 100% code coverage
            public void BlocksConfiguredFastCalls()
            {
                // we setup a request context that returns always the same session id
                var context = MvcTestBase.CreateRequestContext();

                // we want to allow max 2 requests in one second
                var target = new FastRequestsProtectionAttribute {
                    RequestsPerSecondAndClient = 2
                };

                // calling 3 times should block access
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);

                var contentResult = context.Result as ContentResult;

                Assert.IsNotNull(contentResult);
                Assert.AreEqual("client has been blocked...", contentResult.Content);
            }
            public void RedirectsConfiguredFastCalls()
            {
                // we setup a request context that returns always the same session id
                var context = MvcTestBase.CreateRequestContext();

                // we want to allow max 2 requests in one second
                var target = new FastRequestsProtectionAttribute {
                    RequestsPerSecondAndClient = 2, FaultAction = "Fault"
                };

                // first call should NOT redirect
                target.OnActionExecuting(context);
                Assert.IsNotInstanceOfType(context.Result, typeof(RedirectResult));

                target.OnActionExecuting(context);

                // calling 3 times should redirect
                target.OnActionExecuting(context);

                Assert.IsInstanceOfType(context.Result, typeof(RedirectResult));
            }
            public async Task NotBlocksMultipleLessThanConfiguredFastCalls()
            {
                // we setup a request context that returns always the same session id
                var context = MvcTestBase.CreateRequestContext();

                // we want to allow max 2 requests in one second
                var target = new FastRequestsProtectionAttribute
                {
                    RequestsPerSecondAndClient   = 2,
                    MaxRetentionTimeOfStatistics = 50,
                };

                // calling 2 times should NOT throw an exception
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);

                await Task.Delay(100);

                // calling 2 times should NOT throw an exception
                target.OnActionExecuting(context);
                target.OnActionExecuting(context);

                Assert.IsNotNull(target);
            }
            public void ImplementsActionFilterAttribute()
            {
                var target = new FastRequestsProtectionAttribute();

                Assert.IsInstanceOfType(target, typeof(ActionFilterAttribute));
            }
            public void InitializesWith30SecondsMaxRetentionTime()
            {
                var target = new FastRequestsProtectionAttribute();

                Assert.AreEqual(3000, target.MaxRetentionTimeOfStatistics);
            }