public void OnActionExecutingShouldDecrementRequests()
        {
            ThrottleAttribute target = new ThrottleAttribute(m_dtWrapperMock.Object, m_cacheMock.Object) { Requests = 10, Seconds = 20, Name = "TestAction" }; // TODO: Initialize to an appropriate value

            var tci = new ThrottleCacheItem { RequestsRemaining = 10 };
            m_cacheMock.Setup(c => c["TestAction-ipaddress"]).Returns(tci);

            target.OnActionExecuting(m_ActionContext);

            Assert.AreEqual(9, tci.RequestsRemaining);
        }
        public void OnActionExecutingShouldAddCacheItemIfNotExists()
        {
            ThrottleAttribute target = new ThrottleAttribute(m_dtWrapperMock.Object, m_cacheMock.Object) { Requests = 10, Seconds = 20, Name="TestAction" }; // TODO: Initialize to an appropriate value

            m_cacheMock.Setup(c=>c["TestAction-ipaddress"]).Returns(null);
            m_cacheMock.Setup(c =>
                c.Add("TestAction-ipaddress",
                    It.Is<ThrottleCacheItem>(tci=>tci.RequestsRemaining == 10),
                    new DateTime(2010, 06, 23,0,0,20),
                    Cache.NoSlidingExpiration)
                ).Returns(null);

            target.OnActionExecuting(m_ActionContext);

            m_cacheMock.VerifyAll();
        }
        public void OnActionExecutingShouldDoJsonResultWhenJson()
        {
            ThrottleAttribute target = new ThrottleAttribute(m_dtWrapperMock.Object, m_cacheMock.Object) {
                Requests = 10,
                Seconds = 20,
                Name = "TestAction",
                Message = "Message {n} {s}",
                Json = true
            }; // TODO: Initialize to an appropriate value

            var tci = new ThrottleCacheItem { RequestsRemaining = 0 };
            m_cacheMock.Setup(c => c["TestAction-ipaddress"]).Returns(tci);
            m_ResponseMock.SetupSet(r => { r.StatusCode = (int)HttpStatusCode.Conflict; });
            target.OnActionExecuting(m_ActionContext);

            Assert.AreEqual("Message 10 20", ((DataResult)((JsonResult)m_ActionContext.Result).Data).message);
            Assert.IsFalse(((DataResult)((JsonResult)m_ActionContext.Result).Data).success);
            Assert.AreEqual(JsonRequestBehavior.AllowGet, ((JsonResult)m_ActionContext.Result).JsonRequestBehavior);
        }
        public void OnActionExecutingShouldFormatString()
        {
            ThrottleAttribute target = new ThrottleAttribute(m_dtWrapperMock.Object, m_cacheMock.Object) {
                Requests = 10,
                Seconds = 20,
                Name = "TestAction",
                Message = "Message {n} {s}"
            }; // TODO: Initialize to an appropriate value

            var tci = new ThrottleCacheItem { RequestsRemaining = 0 };
            m_cacheMock.Setup(c => c["TestAction-ipaddress"]).Returns(tci);
            m_ResponseMock.SetupSet(r => { r.StatusCode = (int)HttpStatusCode.Conflict; });
            target.OnActionExecuting(m_ActionContext);

            Assert.AreEqual("Message 10 20",((ContentResult)m_ActionContext.Result).Content);
        }
        public void OnActionExecutingShouldRejectRequestWhenNotRequestsRemaining()
        {
            ThrottleAttribute target = new ThrottleAttribute(m_dtWrapperMock.Object, m_cacheMock.Object) { Requests = 10, Seconds = 20, Name = "TestAction" }; // TODO: Initialize to an appropriate value

            var tci = new ThrottleCacheItem { RequestsRemaining = 0 };
            m_cacheMock.Setup(c => c["TestAction-ipaddress"]).Returns(tci);
            m_ResponseMock.SetupSet(r => { r.StatusCode = (int)HttpStatusCode.Conflict; });
            target.OnActionExecuting(m_ActionContext);

            m_ResponseMock.VerifyAll();
            Assert.IsNotNull(m_ActionContext.Result);
        }