Example #1
0
        public void AddFileVersionToPath_CachesFoundResults(string path, bool pathStartsWithAppName)
        {
            // Arrange
            var fileProvider = GetMockFileProvider(
                "file.txt",
                pathStartsWithAppName);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());
            var mockFileProvider = Mock.Get(fileProvider);

            // Act 1
            var result = fileVersionProvider.AddFileVersionToPath(path);

            // Assert 1
            Assert.Equal($"{path}?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once());
            mockFileProvider.Verify(f => f.Watch(It.IsAny<string>()), Times.Once());

            // Act 2
            result = fileVersionProvider.AddFileVersionToPath(path);

            // Assert 2
            Assert.Equal($"{path}?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once());
            mockFileProvider.Verify(f => f.Watch(It.IsAny<string>()), Times.Once());
        }
Example #2
0
        public void AddFileVersionToPath_CachesNotFoundResults()
        {
            // Arrange
            var path = "/wwwroot/file.txt";
            var fileProvider = GetMockFileProvider(
                path,
                pathStartsWithAppName: false,
                fileDoesNotExist: true);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());
            var mockFileProvider = Mock.Get(fileProvider);

            // Act 1
            var result = fileVersionProvider.AddFileVersionToPath(path);

            // Assert 1
            Assert.Equal(path, result);
            mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once());
            mockFileProvider.Verify(f => f.Watch(It.IsAny<string>()), Times.Once());

            // Act 2
            result = fileVersionProvider.AddFileVersionToPath(path);

            // Assert 2
            Assert.Equal(path, result);
            mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once());
            mockFileProvider.Verify(f => f.Watch(It.IsAny<string>()), Times.Once());
        }
Example #3
0
        public void AddFileVersionToPath_WhenCacheIsAbsent(string filePath, string expected)
        {
            // Arrange
            var fileProvider        = GetMockFileProvider(filePath);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal(expected, result);
        }
Example #4
0
        public void DoesNotAddVersion_IfFileNotFound()
        {
            // Arrange
            var filePath            = "http://contoso.com/hello/world";
            var fileProvider        = GetMockFileProvider(filePath, false, true);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal("http://contoso.com/hello/world", result);
        }
Example #5
0
        public void AddFileVersionToPath_PathContainingAppName(
            string filePath,
            bool pathStartsWithAppBase,
            string requestPathBase)
        {
            // Arrange
            var fileProvider        = GetMockFileProvider(filePath, pathStartsWithAppBase);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase(requestPathBase));

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
Example #6
0
        public void ReturnsValueFromCache()
        {
            // Arrange
            var filePath     = "/hello/world";
            var fileProvider = GetMockFileProvider(filePath);
            var memoryCache  = new MemoryCache(new MemoryCacheOptions());

            memoryCache.Set(filePath, "FromCache");
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                memoryCache,
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal("FromCache", result);
        }
Example #7
0
        public void AddFileVersionToPath_UpdatesEntryWhenCacheExpires_ForNonExistingFile()
        {
            // Arrange
            var fileProvider        = new TestFileProvider();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act 1 - File does not exist
            var result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 1
            Assert.Equal("file.txt", result);

            // Act 2 - File gets added
            fileProvider.AddFile("file.txt", "Hello World!");
            fileProvider.GetChangeToken("file.txt").HasChanged = true;
            result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 2
            Assert.Equal("file.txt?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
Example #8
0
 private void EnsureFileVersionProvider()
 {
     if (_fileVersionProvider == null)
     {
         _fileVersionProvider = new FileVersionProvider(
             HostingEnvironment.WebRootFileProvider,
             Cache,
             ViewContext.HttpContext.Request.PathBase);
     }
 }
Example #9
0
        public void AddFileVersionToPath_DoesNotLockFileAfterReading()
        {
            // Arrange
            var stream = new TestableMemoryStream(Encoding.UTF8.GetBytes("Hello World!"));
            var mockFile = new Mock<IFileInfo>();
            mockFile.SetupGet(f => f.Exists).Returns(true);
            mockFile
                .Setup(m => m.CreateReadStream())
                .Returns(stream);

            var fileProvider = new TestFileProvider();
            fileProvider.AddFile("/hello/world", mockFile.Object);

            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath("/hello/world");

            // Assert
            Assert.True(stream.Disposed);
        }
Example #10
0
        public void SetsValueInCache(string filePath, string watchPath, string requestPathBase)
        {
            // Arrange
            var changeToken = new Mock<IChangeToken>();
            var fileProvider = GetMockFileProvider(filePath, requestPathBase != null);
            Mock.Get(fileProvider)
                .Setup(f => f.Watch(watchPath)).Returns(changeToken.Object);

            object cacheValue = null;
            var value = new Mock<ICacheEntry>();
            value.Setup(c => c.Value).Returns(cacheValue);
            value.Setup(c => c.ExpirationTokens).Returns(new List<IChangeToken>());
            var cache = new Mock<IMemoryCache>();
            cache.CallBase = true;
            cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheValue))
                .Returns(cacheValue != null);
            cache.Setup(c => c.CreateEntry(
                /*key*/ filePath))
                .Returns((object key) => value.Object)
                .Verifiable();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                cache.Object,
                GetRequestPathBase(requestPathBase));

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            cache.VerifyAll();
        }
Example #11
0
        public void ReturnsValueFromCache()
        {
            // Arrange
            var filePath = "/hello/world";
            var fileProvider = GetMockFileProvider(filePath);
            var memoryCache = new MemoryCache(new MemoryCacheOptions());
            memoryCache.Set(filePath, "FromCache");
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                memoryCache,
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal("FromCache", result);
        }
Example #12
0
        public void DoesNotAddVersion_IfFileNotFound()
        {
            // Arrange
            var filePath = "http://contoso.com/hello/world";
            var fileProvider = GetMockFileProvider(filePath, false, true);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal("http://contoso.com/hello/world", result);
        }
Example #13
0
        public void AddFileVersionToPath_WhenCacheIsAbsent(string filePath, string expected)
        {
            // Arrange
            var fileProvider = GetMockFileProvider(filePath);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal(expected, result);
        }
Example #14
0
        public void AddFileVersionToPath_UpdatesEntryWhenCacheExpires_ForNonExistingFile()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act 1 - File does not exist
            var result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 1
            Assert.Equal("file.txt", result);

            // Act 2 - File gets added
            fileProvider.AddFile("file.txt", "Hello World!");
            fileProvider.GetChangeToken("file.txt").HasChanged = true;
            result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 2
            Assert.Equal("file.txt?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
Example #15
0
        public void AddFileVersionToPath_PathContainingAppName(
            string filePath,
            bool pathStartsWithAppBase,
            string requestPathBase)
        {
            // Arrange
            var fileProvider = GetMockFileProvider(filePath, pathStartsWithAppBase);
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase(requestPathBase));

            // Act
            var result = fileVersionProvider.AddFileVersionToPath(filePath);

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }