コード例 #1
0
        public void OnActionExecuting_CacheHit_ReturnsContentResult()
        {
            // Arrange
            Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>();

            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Never();
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary <string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock <ITestableCache> mockCache = new Mock <ITestableCache>();

            mockCache.Expect(o => o.GetCacheItem(It.IsAny <string>())).Returns("some cached content");

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object)
            {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;

            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsInstanceOfType(filterContext.Result, typeof(ContentResult));
            Assert.AreEqual("some cached content", ((ContentResult)filterContext.Result).Content);
        }
コード例 #2
0
        public void OnActionExecuting_CacheMiss_SetsFlagInItems() {
            // Arrange
            string expectedCacheKey = null;

            Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>();
            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable());
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary<string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache
                .Expect(o => o.GetCacheItem(It.IsAny<string>()))
                .Callback((string cacheKey) => { expectedCacheKey = cacheKey; })
                .Returns((object)null);

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;
            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsNull(filterContext.Result, "Result shouldn't have been set if cache miss.");
            Assert.AreEqual(expectedCacheKey, filterContext.HttpContext.Items.Values.Cast<object>().Single(), "Flag was not correctly set in HttpContext.Items.");
        }
コード例 #3
0
        public void OnActionExecuting_CacheHit_ReturnsContentResult() {
            // Arrange
            Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>();
            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Never();
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary<string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache.Expect(o => o.GetCacheItem(It.IsAny<string>())).Returns("some cached content");

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;
            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsInstanceOfType(filterContext.Result, typeof(ContentResult));
            Assert.AreEqual("some cached content", ((ContentResult)filterContext.Result).Content);
        }
コード例 #4
0
        public void OnResultExecuted_ItemsFlagSet_Exception_DumpsContent_NoAddToCache()
        {
            // Arrange
            TextWriter originalWriter = TextWriter.Null;

            ChildActionCacheAttribute.WrappedStringWriter wrappedWriter = new ChildActionCacheAttribute.WrappedStringWriter(originalWriter);
            wrappedWriter.Write("captured text");

            Mock <ResultExecutedContext> mockFilterContext = new Mock <ResultExecutedContext>();

            mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny <object>()]).Returns("theCacheKey");
            mockFilterContext.Expect(o => o.Exception).Returns(new Exception("Sample exception."));

            TextWriter currentResponseWriter = wrappedWriter;

            mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(() => currentResponseWriter);
            mockFilterContext
            .ExpectSet(o => o.HttpContext.Response.Output)
            .Callback(w => { currentResponseWriter = w; });
            mockFilterContext
            .Expect(o => o.HttpContext.Response.Write("captured text"))
            .Callback(() => { Assert.AreEqual(originalWriter, currentResponseWriter, "Response.Write() called while wrong writer was active."); })
            .AtMostOnce()
            .Verifiable();

            Mock <ITestableCache> mockCache = new Mock <ITestableCache>();

            mockCache.Expect(o => o.SetCacheItem(It.IsAny <string>(), It.IsAny <string>())).Never();

            // Act
            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object)
            {
                Duration = 10
            };

            // Act
            attr.OnResultExecuted(mockFilterContext.Object);

            // Assert
            mockFilterContext.Verify();
            mockCache.Verify();
            Assert.AreEqual(originalWriter, currentResponseWriter);
            Assert.IsFalse(mockFilterContext.Object.ExceptionHandled, "Exception shouldn't have been marked as handled.");
        }
コード例 #5
0
        public void OnActionExecuting_CacheMiss_SetsFlagInItems()
        {
            // Arrange
            string expectedCacheKey = null;

            Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>();

            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable());
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary <string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock <ITestableCache> mockCache = new Mock <ITestableCache>();

            mockCache
            .Expect(o => o.GetCacheItem(It.IsAny <string>()))
            .Callback((string cacheKey) => { expectedCacheKey = cacheKey; })
            .Returns((object)null);

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object)
            {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;

            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsNull(filterContext.Result, "Result shouldn't have been set if cache miss.");
            Assert.AreEqual(expectedCacheKey, filterContext.HttpContext.Items.Values.Cast <object>().Single(), "Flag was not correctly set in HttpContext.Items.");
        }
コード例 #6
0
        public void OnResultExecuted_ItemsFlagSet_NoException_DumpsContentAndAddsToCache() {
            // Arrange
            TextWriter originalWriter = TextWriter.Null;

            ChildActionCacheAttribute.WrappedStringWriter wrappedWriter = new ChildActionCacheAttribute.WrappedStringWriter(originalWriter);
            wrappedWriter.Write("captured text");

            Mock<ResultExecutedContext> mockFilterContext = new Mock<ResultExecutedContext>();
            mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny<object>()]).Returns("theCacheKey");
            mockFilterContext.Expect(o => o.Exception).Returns((Exception)null);

            TextWriter currentResponseWriter = wrappedWriter;
            mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(() => currentResponseWriter);
            mockFilterContext
                .ExpectSet(o => o.HttpContext.Response.Output)
                .Callback(w => { currentResponseWriter = w; });
            mockFilterContext
                .Expect(o => o.HttpContext.Response.Write("captured text"))
                .Callback(() => { Assert.AreEqual(originalWriter, currentResponseWriter, "Response.Write() called while wrong writer was active."); })
                .AtMostOnce()
                .Verifiable();

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache.Expect(o => o.SetCacheItem("theCacheKey", "captured text")).Verifiable();

            // Act
            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            attr.OnResultExecuted(mockFilterContext.Object);

            // Assert
            mockFilterContext.Verify();
            mockCache.Verify();
            Assert.AreEqual(originalWriter, currentResponseWriter);
        }