Ejemplo n.º 1
0
        public void GetHashCode_VariesByUniqueId()
        {
            // Arrange
            var tagHelperContext1 = GetTagHelperContext("some-id");
            var cacheTagHelper1   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            var tagHelperContext2 = GetTagHelperContext("some-other-id");
            var cacheTagHelper2   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            var cacheKey1 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper1, tagHelperContext1);
            var cacheKey2 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper2, tagHelperContext2);

            // Act
            var hashcode1 = cacheKey1.GetHashCode();
            var hashcode2 = cacheKey2.GetHashCode();

            // Assert
            Assert.NotEqual(hashcode1, hashcode2);
        }
Ejemplo n.º 2
0
        public void GenerateKey_WithMultipleVaryByOptions_CreatesCombinedKey()
        {
            // Arrange
            var expected = "CacheTagHelper||testid||VaryBy||custom-value||" +
                           "VaryByHeader(content-type||text/html)||VaryByUser||someuser";
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext  = GetViewContext(),
                VaryByUser   = true,
                VaryByHeader = "content-type",
                VaryBy       = "custom-value"
            };

            cacheTagHelper.ViewContext.HttpContext.Request.Headers["Content-Type"] = "text/html";
            var identity = new ClaimsIdentity(new[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "someuser") });

            cacheTagHelper.ViewContext.HttpContext.User = new ClaimsPrincipal(identity);

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 3
0
        public void GenerateKey_ReturnsKeyBasedOnTagHelperUniqueId()
        {
            // Arrange
            var id = Guid.NewGuid().ToString();
            var tagHelperContext = GetTagHelperContext(id);
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };
            var expected = "CacheTagHelper||" + id;

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 4
0
        public void GenerateKey_UsesVaryByUser_WhenUserIsNotAuthenticated()
        {
            // Arrange
            var expected         = "CacheTagHelper||testid||VaryByUser||";
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                VaryByUser  = true
            };

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 5
0
        public void GenerateKey_UsesVaryByPropertyToGenerateKey(string varyBy)
        {
            // Arrange
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                VaryBy      = varyBy
            };
            var expected = "CacheTagHelper||testid||VaryBy||" + varyBy;

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 6
0
        public void GenerateKey_UsesVaryByRoute(string varyByRoute, string expected)
        {
            // Arrange
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                VaryByRoute = varyByRoute
            };

            cacheTagHelper.ViewContext.RouteData.Values["id"]       = 4;
            cacheTagHelper.ViewContext.RouteData.Values["category"] = "MyCategory";

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 7
0
        public void GenerateKey_UsesVaryByQuery(string varyByQuery, string expected)
        {
            // Arrange
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                VaryByQuery = varyByQuery
            };

            cacheTagHelper.ViewContext.HttpContext.Request.QueryString =
                new QueryString("?sortoption=Adorability&Category=cats&sortOrder=");

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 8
0
        public void GenerateKey_UsesVaryByCookieName(string varyByCookie, string expected)
        {
            // Arrange
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext  = GetViewContext(),
                VaryByCookie = varyByCookie
            };

            cacheTagHelper.ViewContext.HttpContext.Request.Headers["Cookie"] =
                "Cookie0=Cookie0Value;Cookie1=Cookie1Value";

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 9
0
        public void GenerateKey_UsesVaryByUserAndAuthenticatedUserName()
        {
            // Arrange
            var expected         = "CacheTagHelper||testid||VaryByUser||test_name";
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext(),
                VaryByUser  = true
            };
            var identity = new ClaimsIdentity(new[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "test_name") });

            cacheTagHelper.ViewContext.HttpContext.User = new ClaimsPrincipal(identity);

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 10
0
        public void Equals_ReturnsFalseOnDifferentKey()
        {
            // Arrange
            var tagHelperContext1 = GetTagHelperContext("some-id");
            var cacheTagHelper1   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            var tagHelperContext2 = GetTagHelperContext("some-other-id");
            var cacheTagHelper2   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            // Act
            var cacheTagKey1 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper1, tagHelperContext1);
            var cacheTagKey2 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper2, tagHelperContext2);

            // Assert
            Assert.NotEqual(cacheTagKey1, cacheTagKey2);
        }
Ejemplo n.º 11
0
        public void GenerateKey_UsesVaryByHeader(string varyByHeader, string expected)
        {
            // Arrange
            var tagHelperContext = GetTagHelperContext();
            var cacheTagHelper   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext  = GetViewContext(),
                VaryByHeader = varyByHeader
            };
            var headers = cacheTagHelper.ViewContext.HttpContext.Request.Headers;

            headers["Accept-Language"] = "en-us;charset=utf8";
            headers["Accept-Encoding"] = "utf8";
            headers["X-CustomHeader"]  = "Header-Value";

            // Act
            var cacheTagKey = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper, tagHelperContext);
            var key         = cacheTagKey.GenerateKey();

            // Assert
            Assert.Equal(expected, key);
        }
Ejemplo n.º 12
0
        public void Equals_ReturnsTrueOnSameKey()
        {
            // Arrange
            var id = Guid.NewGuid().ToString();
            var tagHelperContext1 = GetTagHelperContext(id);
            var cacheTagHelper1   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            var tagHelperContext2 = GetTagHelperContext(id);
            var cacheTagHelper2   = new Pavalisoft.Caching.TagHelpers.CacheTagHelper(Mock.Of <ICacheManager>(), new HtmlTestEncoder())
            {
                ViewContext = GetViewContext()
            };

            // Act
            var cacheTagKey1 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper1, tagHelperContext1);
            var cacheTagKey2 = new Pavalisoft.Caching.TagHelpers.CacheTagKey(cacheTagHelper2, tagHelperContext2);

            // Assert
            Assert.Equal(cacheTagKey1, cacheTagKey2);
        }