public async Task When_FilterCalledWithoutCached_Then_SetObjectCached_Test(string method, string path, string queryString)
        {
            Configuration
            .SetupGet(d => d.CurrentValue).Returns(GetConfiguration("Default".Split(",")));

            CachedByConfigurationAttribute = new CachedByConfigurationAttribute(
                ResponseCacheServiceMock.Object,
                Configuration.Object
                );

            var expectedValue = new { prop1 = "teste" };

            ActionExecutionDelegateMock
            .Setup(d => d.Invoke())
            .ReturnsAsync(
                SetActionExecutedContext(expectedValue)
                );

            HttpContext.Request.Method      = method;
            HttpContext.Request.Path        = path;
            HttpContext.Request.QueryString = new QueryString(queryString);

            //Act
            await CachedByConfigurationAttribute.OnActionExecutionAsync(Context, ActionExecutionDelegateMock.Object);

            ResponseCacheServiceMock.Verify_GetCachedResponseAsStringAsync(Times.Once());
            ResponseCacheServiceMock.Verify_SetCacheResponseAsync(Times.Once());
            var storedValue = await ResponseCacheServiceMock.Object.GetCachedResponseAsStringAsync($"{path}|{BuildValuesFromQueryString(HttpContext)}");

            Assert.Equal(JsonSerializer.Serialize(expectedValue), storedValue);
        }
        public async Task When_FilterCalledAndNotGetMethod_Then_NotProcess_Test(string method, string path)
        {
            Configuration
            .SetupGet(d => d.CurrentValue).Returns(GetConfiguration("Default".Split(",")));

            CachedByConfigurationAttribute = new CachedByConfigurationAttribute(
                ResponseCacheServiceMock.Object,
                Configuration.Object
                );
            var expectedValue = new { prop1 = "teste" };

            ActionExecutionDelegateMock
            .Setup(d => d.Invoke())
            .ReturnsAsync(
                SetActionExecutedContext(expectedValue)
                );

            HttpContext.Request.Method = method;
            HttpContext.Request.Path   = path;

            //Act
            await CachedByConfigurationAttribute.OnActionExecutionAsync(Context, ActionExecutionDelegateMock.Object);

            ResponseCacheServiceMock.Verify_GetCachedResponseAsStringAsync(Times.Never());

            ResponseCacheServiceMock.Verify_SetCacheResponseAsync(Times.Never());
        }
        public async Task When_FilterCalledWithoutCached2_Then_SetObjectCached_Test(string method, string path, string queryString)
        {
            Configuration
            .SetupGet(d => d.CurrentValue).Returns(GetConfiguration("Default".Split(",")));

            CachedByConfigurationAttribute = new CachedByConfigurationAttribute(
                ResponseCacheServiceMock.Object,
                Configuration.Object
                );

            object expectedValue = null;

            ActionExecutionDelegateMock
            .Setup(d => d.Invoke())
            .ReturnsAsync(
                SetActionExecutedContext(expectedValue)
                );

            HttpContext.Request.Method      = method;
            HttpContext.Request.Path        = path;
            HttpContext.Request.QueryString = new QueryString(queryString);

            //Act
            await CachedByConfigurationAttribute.OnActionExecutionAsync(Context, ActionExecutionDelegateMock.Object);

            ResponseCacheServiceMock.Verify_GetCachedResponseAsStringAsync(Times.Once());
            ResponseCacheServiceMock.Verify_SetCacheResponseAsync(Times.Never());
        }
Пример #4
0
        public async Task When_FilterCalled_Then_RemoveAllCachedPattern_Test(string method, string path, string patternRemove)
        {
            Configuration
            .SetupGet(d => d.CurrentValue)
            .Returns(GetConfiguration(new Dictionary <string, string>()
            {
                { path, patternRemove }
            }));

            CachedRemoveByConfigurationAttribute = new CachedRemoveByConfigurationAttribute(
                ResponseCacheServiceMock.Object,
                Configuration.Object
                );

            var expectedValue = new { prop1 = "teste" };

            ActionExecutionDelegateMock
            .Setup(d => d.Invoke())
            .ReturnsAsync(
                SetActionExecutedContext(expectedValue)
                );

            HttpContext.Request.Method = method;
            HttpContext.Request.Path   = path;

            //Act
            await CachedRemoveByConfigurationAttribute.OnActionExecutionAsync(Context, ActionExecutionDelegateMock.Object);

            ResponseCacheServiceMock.Verify_GetKeysByPattern(Times.Once(), patternRemove);
            ResponseCacheServiceMock.Set_RemoveCachedResponseByNameAsync(Times.Once());
        }
Пример #5
0
        public CacheAttributeTest()
        {
            HttpContext = new DefaultHttpContext();
            Context     = new ActionExecutingContext(
                new ActionContext(HttpContext, new RouteData(), new ActionDescriptor()),
                new List <IFilterMetadata>(),
                new Dictionary <string, object>(),
                new Mock <ControllerBase>().Object);

            ResponseCacheServiceMock = new ResponseCacheServiceMock();
            CachedAttribute          = new CachedAttribute(ResponseCacheServiceMock.Object);

            ActionExecutionDelegateMock = new Mock <ActionExecutionDelegate>();
        }
        public CachedByConfigurationAttributeTest()
        {
            Configuration = new Mock <IOptionsMonitor <FilterCachedConfiguration> >();

            HttpContext = new DefaultHttpContext();
            Context     = new ActionExecutingContext(
                new ActionContext(HttpContext, new RouteData(), new ActionDescriptor()),
                new List <IFilterMetadata>(),
                new Dictionary <string, object>(),
                new Mock <ControllerBase>().Object);

            ResponseCacheServiceMock = new ResponseCacheServiceMock();

            ActionExecutionDelegateMock = new Mock <ActionExecutionDelegate>();
        }
Пример #7
0
        public async Task When_FilterCalledWithCached_Then_GetObjectCached_Test(string method, string path, string queryString)
        {
            var expectedValue = new { prop1 = "teste" };

            ActionExecutionDelegateMock
            .Setup(d => d.Invoke())
            .ReturnsAsync(
                SetActionExecutedContext(expectedValue)
                );

            HttpContext.Request.Method      = method;
            HttpContext.Request.Path        = path;
            HttpContext.Request.QueryString = new QueryString(queryString);

            await ResponseCacheServiceMock.Object.SetCacheResponseAsync($"{path}|{BuildValuesFromQueryString(HttpContext)}", JsonSerializer.Serialize(expectedValue), null);

            //Act
            await CachedAttribute.OnActionExecutionAsync(Context, ActionExecutionDelegateMock.Object);

            ResponseCacheServiceMock.Verify_GetCachedResponseAsStringAsync(Times.Once());

            ResponseCacheServiceMock.Verify_SetCacheResponseAsync(Times.Once());
        }