예제 #1
0
        public async Task ProcessAsync_RecalculatesValueIfCacheKeyChanges()
        {
            // Arrange - 1
            var id                = "unique-id";
            var childContent1     = "original-child-content";
            var cache             = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1  = new TagHelperOutput("cache", new Dictionary <string, string> {
                { "attr", "value" }
            })
            {
                PreContent  = "<cache>",
                PostContent = "</cache>"
            };
            var cacheTagHelper1 = new CacheTagHelper
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext  = GetViewContext(),
                MemoryCache  = cache
            };

            cacheTagHelper1.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=value2";

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Null(tagHelperOutput1.PreContent);
            Assert.Null(tagHelperOutput1.PostContent);
            Assert.True(tagHelperOutput1.ContentSet);
            Assert.Equal(childContent1, tagHelperOutput1.Content);

            // Arrange - 2
            var childContent2     = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2  = new TagHelperOutput("cache", new Dictionary <string, string> {
                { "attr", "value" }
            })
            {
                PreContent  = "<cache>",
                PostContent = "</cache>"
            };
            var cacheTagHelper2 = new CacheTagHelper
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext  = GetViewContext(),
                MemoryCache  = cache
            };

            cacheTagHelper2.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=not-value2";

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Null(tagHelperOutput2.PreContent);
            Assert.Null(tagHelperOutput2.PostContent);
            Assert.True(tagHelperOutput2.ContentSet);
            Assert.Equal(childContent2, tagHelperOutput2.Content);
        }
예제 #2
0
        public async Task ProcessAsync_UsesExpiresSliding_ToExpireCacheEntryWithSlidingExpiration()
        {
            // Arrange - 1
            var currentTime   = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
            var id            = "unique-id";
            var childContent1 = "original-child-content";
            var clock         = new Mock <ISystemClock>();

            clock.SetupGet(p => p.UtcNow)
            .Returns(() => currentTime);
            var cache = new MemoryCache(new MemoryCacheOptions {
                Clock = clock.Object
            });
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1  = new TagHelperOutput("cache", new TagHelperAttributeList {
                { "attr", "value" }
            });

            tagHelperOutput1.PreContent.SetContent("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                ViewContext    = GetViewContext(),
                ExpiresSliding = TimeSpan.FromSeconds(30)
            };

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            currentTime = currentTime.AddSeconds(35);
            var childContent2     = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2  = new TagHelperOutput("cache", new TagHelperAttributeList {
                { "attr", "value" }
            });

            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                ViewContext    = GetViewContext(),
                ExpiresSliding = TimeSpan.FromSeconds(30)
            };

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #3
0
        public async Task ProcessAsync_FlowsEntryLinkThatAllowsAddingTriggersToAddedEntry()
        {
            // Arrange
            var id = "some-id";
            var expectedContent = new DefaultTagHelperContent();

            expectedContent.SetContent("some-content");
            var tokenSource       = new CancellationTokenSource();
            var cache             = new MemoryCache(new MemoryCacheOptions());
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .AddExpirationToken(new CancellationChangeToken(tokenSource.Token));
            var tagHelperContext = new TagHelperContext(
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: id);
            var tagHelperOutput = new TagHelperOutput(
                "cache",
                new TagHelperAttributeList {
                { "attr", "value" }
            },
                getChildContentAsync: useCachedResult =>
            {
                TagHelperContent tagHelperContent;
                if (!cache.TryGetValue("key1", out tagHelperContent))
                {
                    tagHelperContent = expectedContent;
                    cache.Set("key1", tagHelperContent, cacheEntryOptions);
                }

                return(Task.FromResult(tagHelperContent));
            });

            tagHelperOutput.PreContent.SetContent("<cache>");
            tagHelperOutput.PostContent.SetContent("</cache>");
            var cacheTagHelper = new CacheTagHelper(cache)
            {
                ViewContext = GetViewContext(),
            };
            var key = cacheTagHelper.GenerateKey(tagHelperContext);

            // Act - 1
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            TagHelperContent cachedValue;
            var result = cache.TryGetValue(key, out cachedValue);

            // Assert - 1
            Assert.Equal(expectedContent.GetContent(), tagHelperOutput.Content.GetContent());
            Assert.True(result);
            Assert.Equal(expectedContent, cachedValue);

            // Act - 2
            tokenSource.Cancel();
            result = cache.TryGetValue(key, out cachedValue);

            // Assert - 2
            Assert.False(result);
            Assert.Null(cachedValue);
        }
예제 #4
0
        public async Task ProcessAsync_UsesExpiresAfter_ToExpireCacheEntry()
        {
            // Arrange - 1
            var currentTime   = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
            var id            = "unique-id";
            var childContent1 = "original-child-content";
            var clock         = new Mock <ISystemClock>();

            clock.SetupGet(p => p.UtcNow)
            .Returns(() => currentTime);
            var cache = new MemoryCache(new MemoryCacheOptions {
                Clock = clock.Object
            });
            var tagHelperContext1 = GetTagHelperContext(id);
            var tagHelperOutput1  = GetTagHelperOutput(childContent: childContent1);

            tagHelperOutput1.PreContent.SetContent("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                ViewContext  = GetViewContext(),
                ExpiresAfter = TimeSpan.FromMinutes(10)
            };

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            var childContent2     = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id);
            var tagHelperOutput2  = GetTagHelperOutput(childContent: childContent2);

            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                ViewContext  = GetViewContext(),
                ExpiresAfter = TimeSpan.FromMinutes(10)
            };

            currentTime = currentTime.AddMinutes(11);

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #5
0
        public async Task ProcessAsync_FlowsEntryLinkThatAllowsAddingTriggersToAddedEntry()
        {
            // Arrange
            var id = "some-id";
            var expectedContent = new DefaultTagHelperContent();

            expectedContent.SetContent("some-content");
            var tokenSource      = new CancellationTokenSource();
            var cache            = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext = new TagHelperContext(
                allAttributes: new Dictionary <string, object>(),
                items: new Dictionary <object, object>(),
                uniqueId: id,
                getChildContentAsync: () =>
            {
                var entryLink = EntryLinkHelpers.ContextLink;
                Assert.NotNull(entryLink);
                entryLink.AddExpirationTriggers(new[]
                {
                    new CancellationTokenTrigger(tokenSource.Token)
                });
                return(Task.FromResult <TagHelperContent>(expectedContent));
            });
            var tagHelperOutput = new TagHelperOutput("cache",
                                                      new Dictionary <string, object> {
                { "attr", "value" }
            });

            tagHelperOutput.PreContent.SetContent("<cache>");
            tagHelperOutput.PostContent.SetContent("</cache>");
            var cacheTagHelper = new CacheTagHelper
            {
                ViewContext = GetViewContext(),
                MemoryCache = cache,
            };
            var key = cacheTagHelper.GenerateKey(tagHelperContext);

            // Act - 1
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            TagHelperContent cachedValue;
            var result = cache.TryGetValue(key, out cachedValue);

            // Assert - 1
            Assert.Equal(expectedContent.GetContent(), tagHelperOutput.Content.GetContent());
            Assert.True(result);
            Assert.Equal(expectedContent, cachedValue);

            // Act - 2
            tokenSource.Cancel();
            result = cache.TryGetValue(key, out cachedValue);

            // Assert - 2
            Assert.False(result);
            Assert.Null(cachedValue);
        }
예제 #6
0
        public async Task ProcessAsync_RecalculatesValueIfCacheKeyChanges()
        {
            // Arrange - 1
            var id                = "unique-id";
            var childContent1     = "original-child-content";
            var cache             = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1  = new TagHelperOutput("cache", new TagHelperAttributeList {
                { "attr", "value" }
            });

            tagHelperOutput1.PreContent.Append("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext  = GetViewContext(),
            };

            cacheTagHelper1.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=value2";

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            var childContent2     = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2  = new TagHelperOutput("cache", new TagHelperAttributeList {
                { "attr", "value" }
            });

            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext  = GetViewContext(),
            };

            cacheTagHelper2.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=not-value2";

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #7
0
        public async Task ProcessAsync_ReturnsCachedValue_IfVaryByParamIsUnchanged()
        {
            // Arrange - 1
            var id                = "unique-id";
            var childContent      = "original-child-content";
            var cache             = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id);
            var tagHelperOutput1  = GetTagHelperOutput(
                attributes: new TagHelperAttributeList(),
                childContent: childContent);
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
            };

            cacheTagHelper1.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            var tagHelperContext2 = GetTagHelperContext(id);
            var tagHelperOutput2  = GetTagHelperOutput(
                attributes: new TagHelperAttributeList(),
                childContent: "different-content");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
            };

            cacheTagHelper2.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput2.Content.GetContent());
        }
예제 #8
0
        public async Task ProcessAsync_FlowsEntryLinkThatAllowsAddingTriggersToAddedEntry()
        {
            // Arrange
            var id = "some-id";
            var expectedContent  = "some-content";
            var tokenSource      = new CancellationTokenSource();
            var cache            = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext = new TagHelperContext(new Dictionary <string, object>(),
                                                        id,
                                                        () =>
            {
                var entryLink = EntryLinkHelpers.ContextLink;
                Assert.NotNull(entryLink);
                entryLink.AddExpirationTriggers(new[]
                {
                    new CancellationTokenTrigger(tokenSource.Token)
                });
                return(Task.FromResult(expectedContent));
            });
            var tagHelperOutput = new TagHelperOutput("cache", new Dictionary <string, string>())
            {
                PreContent  = "<cache>",
                PostContent = "</cache>"
            };
            var cacheTagHelper = new CacheTagHelper
            {
                ViewContext = GetViewContext(),
                MemoryCache = cache,
            };
            var key = cacheTagHelper.GenerateKey(tagHelperContext);

            // Act - 1
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            string cachedValue;
            var    result = cache.TryGetValue(key, out cachedValue);

            // Assert - 1
            Assert.Equal(expectedContent, tagHelperOutput.Content);
            Assert.True(result);
            Assert.Equal(expectedContent, cachedValue);

            // Act - 2
            tokenSource.Cancel();
            result = cache.TryGetValue(key, out cachedValue);

            // Assert - 2
            Assert.False(result);
            Assert.Null(cachedValue);
        }
예제 #9
0
        public async Task ProcessAsync_ReturnsCachedValue_IfVaryByParamIsUnchanged()
        {
            // Arrange - 1
            var id                = "unique-id";
            var childContent      = "original-child-content";
            var cache             = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id, childContent);
            var tagHelperOutput1  = new TagHelperOutput("cache", new Dictionary <string, string>());
            var cacheTagHelper1   = new CacheTagHelper
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
                MemoryCache = cache
            };

            cacheTagHelper1.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Null(tagHelperOutput1.PreContent);
            Assert.Null(tagHelperOutput1.PostContent);
            Assert.True(tagHelperOutput1.ContentSet);
            Assert.Equal(childContent, tagHelperOutput1.Content);

            // Arrange - 2
            var tagHelperContext2 = GetTagHelperContext(id, "different-content");
            var tagHelperOutput2  = new TagHelperOutput("cache", new Dictionary <string, string>());
            var cacheTagHelper2   = new CacheTagHelper
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
                MemoryCache = cache
            };

            cacheTagHelper2.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Null(tagHelperOutput2.PreContent);
            Assert.Null(tagHelperOutput2.PostContent);
            Assert.True(tagHelperOutput2.ContentSet);
            Assert.Equal(childContent, tagHelperOutput2.Content);
        }
예제 #10
0
        public async Task ProcessAsync_ReturnsCachedValue_IfEnabled()
        {
            // Arrange
            var id           = "unique-id";
            var childContent = "original-child-content";
            var cache        = new Mock <IMemoryCache>();

            cache.CallBase = true;
            var value = new DefaultTagHelperContent().SetContent("ok");

            cache.Setup(c => c.CreateLinkingScope()).Returns(new Mock <IEntryLink>().Object);
            cache.Setup(c => c.Set(
                            /*key*/ It.IsAny <string>(),
                            /*value*/ It.IsAny <object>(),
                            /*options*/ It.IsAny <MemoryCacheEntryOptions>()))
            .Returns(value)
            .Verifiable();
            object cacheResult;

            cache.Setup(c => c.TryGetValue(It.IsAny <string>(), out cacheResult))
            .Returns(false);
            var tagHelperContext = GetTagHelperContext(id);
            var tagHelperOutput  = GetTagHelperOutput(
                attributes: new TagHelperAttributeList(),
                childContent: childContent);
            var cacheTagHelper = new CacheTagHelper(cache.Object)
            {
                ViewContext = GetViewContext(),
                Enabled     = true
            };

            // Act
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            // Assert
            Assert.Empty(tagHelperOutput.PreContent.GetContent());
            Assert.Empty(tagHelperOutput.PostContent.GetContent());
            Assert.True(tagHelperOutput.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
            cache.Verify(c => c.Set(
                             /*key*/ It.IsAny <string>(),
                             /*value*/ It.IsAny <object>(),
                             /*options*/ It.IsAny <MemoryCacheEntryOptions>()),
                         Times.Once);
        }
예제 #11
0
        public async Task ProcessAsync_DoesNotCache_IfDisabled()
        {
            // Arrange
            var id           = "unique-id";
            var childContent = "original-child-content";
            var cache        = new Mock <IMemoryCache>();

            cache.CallBase = true;
            var value = new DefaultTagHelperContent().SetContent("ok");

            cache.Setup(c => c.Set(
                            /*key*/ It.IsAny <string>(),
                            /*value*/ value,
                            /*optons*/ It.IsAny <MemoryCacheEntryOptions>()))
            .Returns(value)
            .Verifiable();
            object cacheResult;

            cache.Setup(c => c.TryGetValue(It.IsAny <string>(), out cacheResult))
            .Returns(false);
            var tagHelperContext = GetTagHelperContext(id);
            var tagHelperOutput  = GetTagHelperOutput(
                attributes: new TagHelperAttributeList(),
                childContent: childContent);
            var cacheTagHelper = new CacheTagHelper(cache.Object, new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                Enabled     = false
            };

            // Act
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            // Assert
            Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
            cache.Verify(c => c.Set(
                             /*key*/ It.IsAny <string>(),
                             /*value*/ It.IsAny <object>(),
                             /*options*/ It.IsAny <MemoryCacheEntryOptions>()),
                         Times.Never);
        }
예제 #12
0
        public async Task ProcessAsync_FlowsEntryLinkThatAllowsAddingTriggersToAddedEntry()
        {
            // Arrange
            var id = "some-id";
            var expectedContent = new DefaultTagHelperContent();
            expectedContent.SetContent("some-content");
            var tokenSource = new CancellationTokenSource();
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                .AddExpirationTrigger(new CancellationTokenTrigger(tokenSource.Token));
            var tagHelperContext = new TagHelperContext(
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary<object, object>(),
                uniqueId: id,
                getChildContentAsync: () =>
                {
                    TagHelperContent tagHelperContent;
                    if(!cache.TryGetValue("key1", out tagHelperContent))
                    {
                        tagHelperContent = expectedContent;
                        cache.Set("key1", tagHelperContent, cacheEntryOptions);
                    }

                    return Task.FromResult(tagHelperContent);
                });
            var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
            tagHelperOutput.PreContent.SetContent("<cache>");
            tagHelperOutput.PostContent.SetContent("</cache>");
            var cacheTagHelper = new CacheTagHelper(cache)
            {
                ViewContext = GetViewContext(),
            };
            var key = cacheTagHelper.GenerateKey(tagHelperContext);

            // Act - 1
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);
            TagHelperContent cachedValue;
            var result = cache.TryGetValue(key, out cachedValue);

            // Assert - 1
            Assert.Equal(expectedContent.GetContent(), tagHelperOutput.Content.GetContent());
            Assert.True(result);
            Assert.Equal(expectedContent, cachedValue);

            // Act - 2
            tokenSource.Cancel();
            result = cache.TryGetValue(key, out cachedValue);

            // Assert - 2
            Assert.False(result);
            Assert.Null(cachedValue);
        }
예제 #13
0
        public async Task ProcessAsync_UsesExpiresSliding_ToExpireCacheEntryWithSlidingExpiration()
        {
            // Arrange - 1
            var currentTime = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
            var id = "unique-id";
            var childContent1 = "original-child-content";
            var clock = new Mock<ISystemClock>();
            clock.SetupGet(p => p.UtcNow)
                 .Returns(() => currentTime);
            var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
            tagHelperOutput1.PreContent.SetContent("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                ViewContext = GetViewContext(),
                ExpiresSliding = TimeSpan.FromSeconds(30)
            };

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            currentTime = currentTime.AddSeconds(35);
            var childContent2 = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                ViewContext = GetViewContext(),
                ExpiresSliding = TimeSpan.FromSeconds(30)
            };

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #14
0
        public async Task ProcessAsync_RecalculatesValueIfCacheKeyChanges()
        {
            // Arrange - 1
            var id = "unique-id";
            var childContent1 = "original-child-content";
            var cache = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
            tagHelperOutput1.PreContent.Append("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext = GetViewContext(),
            };
            cacheTagHelper1.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=value2";

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            var childContent2 = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                VaryByCookie = "cookie1,cookie2",
                ViewContext = GetViewContext(),
            };
            cacheTagHelper2.ViewContext.HttpContext.Request.Headers["Cookie"] = "cookie1=value1;cookie2=not-value2";

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #15
0
        public async Task ProcessAsync_ReturnsCachedValue_IfVaryByParamIsUnchanged()
        {
            // Arrange - 1
            var id = "unique-id";
            var childContent = "original-child-content";
            var cache = new MemoryCache(new MemoryCacheOptions());
            var tagHelperContext1 = GetTagHelperContext(id, childContent);
            var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList());
            var cacheTagHelper1 = new CacheTagHelper(cache)
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
            };
            cacheTagHelper1.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            var tagHelperContext2 = GetTagHelperContext(id, "different-content");
            var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList());
            var cacheTagHelper2 = new CacheTagHelper(cache)
            {
                VaryByQuery = "key1,key2",
                ViewContext = GetViewContext(),
            };
            cacheTagHelper2.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
                "?key1=value1&key2=value2");

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput2.Content.GetContent());
        }
예제 #16
0
        public async Task ProcessAsync_ReturnsCachedValue_IfEnabled()
        {
            // Arrange
            var id = "unique-id";
            var childContent = "original-child-content";
            var cache = new Mock<IMemoryCache>();
            cache.CallBase = true;
            var value = new DefaultTagHelperContent().SetContent("ok");
            cache.Setup(c => c.CreateLinkingScope()).Returns(new Mock<IEntryLink>().Object);
            cache.Setup(c => c.Set(
                /*key*/ It.IsAny<string>(),
                /*value*/ It.IsAny<object>(),
                /*options*/ It.IsAny<MemoryCacheEntryOptions>()))
                .Returns(value)
                .Verifiable();
            object cacheResult;
            cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
                .Returns(false);
            var tagHelperContext = GetTagHelperContext(id, childContent);
            var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList());
            var cacheTagHelper = new CacheTagHelper(cache.Object)
            {
                ViewContext = GetViewContext(),
                Enabled = true
            };

            // Act
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            // Assert
            Assert.Empty(tagHelperOutput.PreContent.GetContent());
            Assert.Empty(tagHelperOutput.PostContent.GetContent());
            Assert.True(tagHelperOutput.IsContentModified);
            Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
            cache.Verify(c => c.Set(
                /*key*/ It.IsAny<string>(),
                /*value*/ It.IsAny<object>(),
                /*options*/ It.IsAny<MemoryCacheEntryOptions>()),
                Times.Once);
        }
예제 #17
0
        public async Task ProcessAsync_UsesExpiresOn_ToExpireCacheEntry()
        {
            // Arrange - 1
            var currentTime = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
            var id = "unique-id";
            var childContent1 = "original-child-content";
            var clock = new Mock<ISystemClock>();
            clock.SetupGet(p => p.UtcNow)
                 .Returns(() => currentTime);
            var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
            var tagHelperContext1 = GetTagHelperContext(id);
            var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
            tagHelperOutput1.PreContent.SetContent("<cache>");
            tagHelperOutput1.PostContent.SetContent("</cache>");
            var cacheTagHelper1 = new CacheTagHelper(cache, new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                ExpiresOn = currentTime.AddMinutes(5)
            };

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Empty(tagHelperOutput1.PreContent.GetContent());
            Assert.Empty(tagHelperOutput1.PostContent.GetContent());
            Assert.True(tagHelperOutput1.IsContentModified);
            Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());

            // Arrange - 2
            currentTime = currentTime.AddMinutes(5).AddSeconds(2);
            var childContent2 = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id);
            var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
            tagHelperOutput2.PreContent.SetContent("<cache>");
            tagHelperOutput2.PostContent.SetContent("</cache>");
            var cacheTagHelper2 = new CacheTagHelper(cache, new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                ExpiresOn = currentTime.AddMinutes(5)
            };

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Empty(tagHelperOutput2.PreContent.GetContent());
            Assert.Empty(tagHelperOutput2.PostContent.GetContent());
            Assert.True(tagHelperOutput2.IsContentModified);
            Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
        }
예제 #18
0
        public async Task ProcessAsync_DoesNotCache_IfDisabled()
        {
            // Arrange
            var id = "unique-id";
            var childContent = "original-child-content";
            var cache = new Mock<IMemoryCache>();
            cache.CallBase = true;
            var value = new DefaultTagHelperContent().SetContent("ok");
            cache.Setup(c => c.Set(
                /*key*/ It.IsAny<string>(),
                /*value*/ value,
                /*optons*/ It.IsAny<MemoryCacheEntryOptions>()))
                .Returns(value)
                .Verifiable();
            object cacheResult;
            cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
                .Returns(false);
            var tagHelperContext = GetTagHelperContext(id);
            var tagHelperOutput = GetTagHelperOutput(
                attributes: new TagHelperAttributeList(),
                childContent: childContent);
            var cacheTagHelper = new CacheTagHelper(cache.Object, new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                Enabled = false
            };

            // Act
            await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);

            // Assert
            Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
            cache.Verify(c => c.Set(
                /*key*/ It.IsAny<string>(),
                /*value*/ It.IsAny<object>(),
                /*options*/ It.IsAny<MemoryCacheEntryOptions>()),
                Times.Never);
        }
예제 #19
0
        public async Task ProcessAsync_UsesExpiresOn_ToExpireCacheEntry()
        {
            // Arrange - 1
            var currentTime   = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
            var id            = "unique-id";
            var childContent1 = "original-child-content";
            var clock         = new Mock <ISystemClock>();

            clock.SetupGet(p => p.UtcNow)
            .Returns(() => currentTime);
            var cache = new MemoryCache(new MemoryCacheOptions {
                Clock = clock.Object
            });
            var tagHelperContext1 = GetTagHelperContext(id, childContent1);
            var tagHelperOutput1  = new TagHelperOutput("cache", new Dictionary <string, string> {
                { "attr", "value" }
            })
            {
                PreContent  = "<cache>",
                PostContent = "</cache>"
            };
            var cacheTagHelper1 = new CacheTagHelper
            {
                ViewContext = GetViewContext(),
                MemoryCache = cache,
                ExpiresOn   = currentTime.AddMinutes(5)
            };

            // Act - 1
            await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);

            // Assert - 1
            Assert.Null(tagHelperOutput1.PreContent);
            Assert.Null(tagHelperOutput1.PostContent);
            Assert.True(tagHelperOutput1.ContentSet);
            Assert.Equal(childContent1, tagHelperOutput1.Content);

            // Arrange - 2
            currentTime = currentTime.AddMinutes(5).AddSeconds(2);
            var childContent2     = "different-content";
            var tagHelperContext2 = GetTagHelperContext(id, childContent2);
            var tagHelperOutput2  = new TagHelperOutput("cache", new Dictionary <string, string> {
                { "attr", "value" }
            })
            {
                PreContent  = "<cache>",
                PostContent = "</cache>"
            };
            var cacheTagHelper2 = new CacheTagHelper
            {
                ViewContext = GetViewContext(),
                MemoryCache = cache,
                ExpiresOn   = currentTime.AddMinutes(5)
            };

            // Act - 2
            await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);

            // Assert - 2
            Assert.Null(tagHelperOutput2.PreContent);
            Assert.Null(tagHelperOutput2.PostContent);
            Assert.True(tagHelperOutput2.ContentSet);
            Assert.Equal(childContent2, tagHelperOutput2.Content);
        }