public void AddsVersionToFiles_DoesNotLockFileAfterReading()
        {
            // Arrange
            var stream = new MemoryStream(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 mockFileProvider = new Mock<IFileProvider>();
            mockFileProvider.Setup(fp => fp.GetFileInfo(It.IsAny<string>()))
                .Returns(mockFile.Object);

            var hostingEnvironment = new Mock<IHostingEnvironment>();
            hostingEnvironment.Setup(h => h.WebRootFileProvider).Returns(mockFileProvider.Object);

            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.Object.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase());

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

            // Assert
            Assert.False(stream.CanRead);
            Assert.Throws<ObjectDisposedException>(() => fileVersionProvider.AddFileVersionToPath("/hello/world"));
        }
        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());
        }
        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());
        }
Esempio n. 4
0
 private void EnsureFileVersionProvider()
 {
     if (_fileVersionProvider == null)
     {
         _fileVersionProvider = new FileVersionProvider(
             HostingEnvironment.WebRootFileProvider,
             Cache,
             ViewContext.HttpContext.Request.PathBase);
     }
 }
        public void AddsVersionToFiles_WhenCacheIsAbsent(string filePath, string expected)
        {
            // Arrange
            var hostingEnvironment  = GetMockHostingEnvironment(filePath);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal(expected, result);
        }
        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);
        }
        public void AddsVersionToFiles_WhenCacheIsAbsent(string filePath, string expected)
        {
            // Arrange
            var hostingEnvironment = GetMockHostingEnvironment(filePath);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal(expected, result);
        }
        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);
        }
        public void DoesNotAddVersion_IfFileNotFound()
        {
            // Arrange
            var filePath            = "http://contoso.com/hello/world";
            var hostingEnvironment  = GetMockHostingEnvironment(filePath, false, true);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal("http://contoso.com/hello/world", result);
        }
        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);
        }
Esempio n. 11
0
        public void ReturnsValueFromCache()
        {
            // Arrange
            var filePath            = "/hello/world";
            var hostingEnvironment  = GetMockHostingEnvironment(filePath);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache("FromCache"),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal("FromCache", result);
        }
        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);
        }
Esempio n. 13
0
        public void AddsVersionToFiles_PathContainingAppName(
            string filePath,
            bool pathStartsWithAppBase,
            string requestPathBase)
        {
            // Arrange
            var hostingEnvironment  = GetMockHostingEnvironment(filePath, pathStartsWithAppBase);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase(requestPathBase));

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

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
        public void AddsVersionToFiles_PathContainingAppName(
            string filePath,
            bool pathStartsWithAppBase,
            string requestPathBase)
        {
            // Arrange
            var hostingEnvironment = GetMockHostingEnvironment(filePath, pathStartsWithAppBase);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase(requestPathBase));

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

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
Esempio n. 15
0
        public void SetsValueInCache(string filePath, string watchPath, string requestPathBase)
        {
            // Arrange
            var trigger            = new Mock <IExpirationTrigger>();
            var hostingEnvironment = GetMockHostingEnvironment(filePath, requestPathBase != null);

            Mock.Get(hostingEnvironment.WebRootFileProvider)
            .Setup(f => f.Watch(watchPath)).Returns(trigger.Object);

            object cacheValue = null;
            var    cache      = new Mock <IMemoryCache>();

            cache.CallBase = true;
            cache.Setup(c => c.TryGetValue(It.IsAny <string>(), It.IsAny <IEntryLink>(), out cacheValue))
            .Returns(cacheValue != null);
            var cacheSetContext = new Mock <ICacheSetContext>();

            cacheSetContext.Setup(c => c.AddExpirationTrigger(trigger.Object)).Verifiable();
            cache.Setup(c => c.Set(
                            /*key*/ filePath,
                            /*link*/ It.IsAny <IEntryLink>(),
                            /*state*/ It.IsAny <object>(),
                            /*create*/ It.IsAny <Func <ICacheSetContext, object> >()))
            .Returns <string, IEntryLink, object, Func <ICacheSetContext, object> >(
                (key, link, state, create) =>
            {
                cacheSetContext.Setup(c => c.State).Returns(state);
                return(create(cacheSetContext.Object));
            })
            .Verifiable();
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                cache.Object,
                GetRequestPathBase(requestPathBase));

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

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            cacheSetContext.VerifyAll();
            cache.VerifyAll();
        }
        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);
        }
        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);
        }
        public void AddFileVersionToPath_UpdatesEntryWhenCacheExpires_ForExistingFile_WithRequestPathBase()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase("/wwwroot/"));
            fileProvider.AddFile("file.txt", "Hello World!");

            // Act 1 - File exists
            var result = fileVersionProvider.AddFileVersionToPath("/wwwroot/file.txt");

            // Assert 1
            Assert.Equal("/wwwroot/file.txt?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);

            // Act 2
            fileProvider.DeleteFile("file.txt");
            fileProvider.GetChangeToken("file.txt").HasChanged = true;
            result = fileVersionProvider.AddFileVersionToPath("/wwwroot/file.txt");

            // Assert 2
            Assert.Equal("/wwwroot/file.txt", result);
        }
        public void DoesNotAddVersion_IfFileNotFound()
        {
            // Arrange
            var filePath = "http://contoso.com/hello/world";
            var hostingEnvironment = GetMockHostingEnvironment(filePath, false, true);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache(),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal("http://contoso.com/hello/world", result);
        }
        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);
        }
        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);
        }
        public void SetsValueInCache(string filePath, string watchPath, string requestPathBase)
        {
            // Arrange
            var trigger = new Mock<IExpirationTrigger>();
            var hostingEnvironment = GetMockHostingEnvironment(filePath, requestPathBase != null);
            Mock.Get(hostingEnvironment.WebRootFileProvider)
                .Setup(f => f.Watch(watchPath)).Returns(trigger.Object);

            object cacheValue = null;
            var cache = new Mock<IMemoryCache>();
            cache.CallBase = true;
            cache.Setup(c => c.TryGetValue(It.IsAny<string>(), It.IsAny<IEntryLink>(), out cacheValue))
                .Returns(cacheValue != null);
            var cacheSetContext = new Mock<ICacheSetContext>();
            cacheSetContext.Setup(c => c.AddExpirationTrigger(trigger.Object)).Verifiable();
            cache.Setup(c => c.Set(
                /*key*/ filePath,
                /*link*/ It.IsAny<IEntryLink>(),
                /*state*/ It.IsAny<object>(),
                /*create*/ It.IsAny<Func<ICacheSetContext, object>>()))
                .Returns<string, IEntryLink, object, Func<ICacheSetContext, object>>(
                    (key, link, state, create) =>
                    {
                        cacheSetContext.Setup(c => c.State).Returns(state);
                        return create(cacheSetContext.Object);
                    })
                .Verifiable();
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                cache.Object,
                GetRequestPathBase(requestPathBase));

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

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            cacheSetContext.VerifyAll();
            cache.VerifyAll();
        }
        public void ReturnsValueFromCache()
        {
            // Arrange
            var filePath = "/hello/world";
            var hostingEnvironment = GetMockHostingEnvironment(filePath);
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                GetMockCache("FromCache"),
                GetRequestPathBase());

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

            // Assert
            Assert.Equal("FromCache", result);
        }
Esempio n. 24
0
        public void SetsValueInCache(string filePath, string watchPath, string requestPathBase)
        {
            // Arrange
            var changeToken = new Mock<IChangeToken>();
            var hostingEnvironment = GetMockHostingEnvironment(filePath, requestPathBase != null);
            Mock.Get(hostingEnvironment.WebRootFileProvider)
                .Setup(f => f.Watch(watchPath)).Returns(changeToken.Object);

            object cacheValue = null;
            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.Set(
                /*key*/ filePath,
                /*value*/ It.IsAny<object>(),
                /*options*/ It.IsAny<MemoryCacheEntryOptions>()))
                .Returns(new object())
                .Verifiable();
            var fileVersionProvider = new FileVersionProvider(
                hostingEnvironment.WebRootFileProvider,
                cache.Object,
                GetRequestPathBase(requestPathBase));

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

            // Assert
            Assert.Equal(filePath + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
            cache.VerifyAll();
        }
        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);
        }
        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);
        }