public void AbsolutePathNotAllowed() { var provider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "sub")); var applicationBase = Directory.GetCurrentDirectory(); var file1 = Path.Combine(applicationBase, "File.txt"); var info = provider.GetFileInfo(file1); Assert.NotNull(info); Assert.False(info.Exists); var file2 = Path.Combine(applicationBase, "sub", "File2.txt"); info = provider.GetFileInfo(file2); Assert.NotNull(info); Assert.False(info.Exists); var directory1 = Path.Combine(applicationBase, "sub"); var directoryContents = provider.GetDirectoryContents(directory1); Assert.NotNull(info); Assert.False(info.Exists); var directory2 = Path.Combine(applicationBase, "Does_Not_Exists"); directoryContents = provider.GetDirectoryContents(directory2); Assert.NotNull(info); Assert.False(info.Exists); }
public void ExistingFilesReturnTrue() { var provider = new PhysicalFileProvider(Environment.CurrentDirectory); var info = provider.GetFileInfo("File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); info = provider.GetFileInfo("/File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); }
public void ExistingFilesReturnTrue() { var provider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); var info = provider.GetFileInfo("File.txt"); Assert.NotNull(info); Assert.True(info.Exists); info = provider.GetFileInfo("/File.txt"); Assert.NotNull(info); Assert.True(info.Exists); }
private static void UseDocumentation(IApplicationBuilder app, IApplicationEnvironment appEnv) { var documentationFilesProvider = new PhysicalFileProvider(appEnv.ApplicationBasePath); app.UseDocumentation(new DocumentationOptions() { DefaultFileName = "index", RequestPath = "/docs", NotFoundHtmlFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\NotFound.html"), LayoutFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\Layout.html") }); }
public void SubPathActsAsRoot() { var provider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "sub")); var info = provider.GetFileInfo("File2.txt"); Assert.NotNull(info); Assert.True(info.Exists); }
public void GetFileInfoReturnsNotFoundFileInfoForRelativePathAboveRootPath() { using (var provider = new PhysicalFileProvider(Path.GetTempPath())) { var info = provider.GetFileInfo(Path.Combine("..", Guid.NewGuid().ToString())); Assert.IsType(typeof(NotFoundFileInfo), info); } }
public void GetFileInfoReturnsNotFoundFileInfoForEmptyPath() { using (var provider = new PhysicalFileProvider(Path.GetTempPath())) { var info = provider.GetFileInfo(string.Empty); Assert.IsType(typeof(NotFoundFileInfo), info); } }
public void RelativePathPastRootNotAllowed() { var provider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "sub")); var info = provider.GetFileInfo("..\\File.txt"); Assert.NotNull(info); Assert.False(info.Exists); info = provider.GetFileInfo(".\\..\\File.txt"); Assert.NotNull(info); Assert.False(info.Exists); info = provider.GetFileInfo("File2.txt"); Assert.NotNull(info); Assert.True(info.Exists); Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), "sub", "File2.txt"), info.PhysicalPath); }
public void Exists_WithNonExistingFile_ReturnsFalse() { // Set stuff up on disk (nothing to set up here because we're testing a non-existing file) var root = Path.GetTempPath(); var nonExistingFileName = Guid.NewGuid().ToString(); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(nonExistingFileName); Assert.False(file.Exists); Assert.Throws <FileNotFoundException>(() => file.CreateReadStream()); }
public void Exists_WithFileStartingWithPeriod_ReturnsFalse() { // Set stuff up on disk var root = Path.GetTempPath(); var fileNameStartingWithPeriod = "." + Guid.NewGuid().ToString(); var physicalFileNameStartingWithPeriod = Path.Combine(root, fileNameStartingWithPeriod); File.WriteAllText(physicalFileNameStartingWithPeriod, "Content"); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(fileNameStartingWithPeriod); Assert.False(file.Exists); Assert.Throws <FileNotFoundException>(() => file.CreateReadStream()); }
public void GetFileInfoReturnsNotFoundFileInfoForHiddenFile() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileName = Guid.NewGuid().ToString(); var filePath = Path.Combine(root.RootPath, fileName); File.Create(filePath); var fileInfo = new FileInfo(filePath); File.SetAttributes(filePath, fileInfo.Attributes | FileAttributes.Hidden); var info = provider.GetFileInfo(fileName); Assert.IsType(typeof(NotFoundFileInfo), info); } } }
public void Exists_WithHiddenFile_ReturnsFalse() { // Set stuff up on disk var root = Path.GetTempPath(); var tempFileName = Guid.NewGuid().ToString(); var physicalHiddenFileName = Path.Combine(root, tempFileName); File.WriteAllText(physicalHiddenFileName, "Content"); var fileInfo = new FileInfo(physicalHiddenFileName); File.SetAttributes(physicalHiddenFileName, fileInfo.Attributes | FileAttributes.Hidden); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(tempFileName); Assert.False(file.Exists); Assert.Throws <FileNotFoundException>(() => file.CreateReadStream()); }
private static JSPool.IJsPool CreateJSEngine(IServiceProvider provider) { var ieConfig = new JavaScriptEngineSwitcher.Msie.Configuration.MsieConfiguration { EngineMode = JavaScriptEngineSwitcher.Msie.JsEngineMode.ChakraEdgeJsRt }; var appEnv = provider.GetRequiredService<IApplicationEnvironment>(); var fileProvider = new PhysicalFileProvider(appEnv.ApplicationBasePath); var jsPath = fileProvider.GetFileInfo("wwwroot/js/server.js").PhysicalPath; var poolConfig = new JSPool.JsPoolConfig(); poolConfig.MaxUsagesPerEngine = 20; poolConfig.StartEngines = 2; poolConfig.EngineFactory = () => new JavaScriptEngineSwitcher.Msie.MsieJsEngine(ieConfig); poolConfig.Initializer = engine => InitialiseJSRuntime(jsPath, engine); poolConfig.WatchFiles = new[] { jsPath }; return new JSPool.JsPool(poolConfig); }
public void SubPathActsAsRoot() { var provider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, "sub")); var info = provider.GetFileInfo("File2.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); }
public void Missing_Hidden_And_FilesStartingWithPeriod_ReturnFalse() { var root = Path.GetTempPath(); var hiddenFileName = Path.Combine(root, Guid.NewGuid().ToString()); File.WriteAllText(hiddenFileName, "Content"); var fileNameStartingWithPeriod = Path.Combine(root, ".", Guid.NewGuid().ToString()); File.WriteAllText(fileNameStartingWithPeriod, "Content"); var fileInfo = new FileInfo(hiddenFileName); File.SetAttributes(hiddenFileName, fileInfo.Attributes | FileAttributes.Hidden); var provider = new PhysicalFileProvider(Path.GetTempPath()); provider.GetFileInfo(Guid.NewGuid().ToString()).Exists.ShouldBe(false); provider.GetFileInfo(hiddenFileName).Exists.ShouldBe(false); provider.GetFileInfo(fileNameStartingWithPeriod).Exists.ShouldBe(false); }
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_RegisteredTokens() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "OldContent"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var fileInfo = provider.GetFileInfo(fileName); Assert.Equal(new FileInfo(fileInfo.PhysicalPath).Length, fileInfo.Length); Assert.True(fileInfo.Exists); var token1 = provider.Watch(fileName); var token2 = provider.Watch(fileName); // Valid token1 created. Assert.NotNull(token1); Assert.False(token1.HasChanged); Assert.True(token1.ActiveChangeCallbacks); // Valid token2 created. Assert.NotNull(token2); Assert.False(token2.HasChanged); Assert.True(token2.ActiveChangeCallbacks); // token is the same for a specific file. Assert.Equal(token2, token1); IChangeToken token3 = null; IChangeToken token4 = null; token1.RegisterChangeCallback(state => { var infoFromState = state as IFileInfo; token3 = provider.Watch(infoFromState.Name); Assert.NotNull(token3); token3.RegisterChangeCallback(_ => { }, null); Assert.False(token3.HasChanged); }, state: fileInfo); token2.RegisterChangeCallback(state => { var infoFromState = state as IFileInfo; token4 = provider.Watch(infoFromState.Name); Assert.NotNull(token4); token4.RegisterChangeCallback(_ => { }, null); Assert.False(token4.HasChanged); }, state: fileInfo); // Write new content. File.WriteAllText(fileLocation, "OldContent + NewContent"); Assert.True(fileInfo.Exists); // Wait for callbacks to be fired. await Task.Delay(WaitTimeForTokenToFire); Assert.True(token1.HasChanged); Assert.True(token2.HasChanged); // token is the same for a specific file. Assert.Same(token4, token3); // A new token is created. Assert.NotEqual(token1, token3); // Delete the file and verify file info is updated. File.Delete(fileLocation); fileInfo = provider.GetFileInfo(fileName); Assert.False(fileInfo.Exists); Assert.False(new FileInfo(fileLocation).Exists); // Wait for callbacks to be fired. await Task.Delay(WaitTimeForTokenToFire); Assert.True(token3.HasChanged); Assert.True(token4.HasChanged); }
public void GetFileInfoReturnsNotFoundFileInfoForFileNameStartingWithPeriod() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileName = "." + Guid.NewGuid().ToString(); var filePath = Path.Combine(root.RootPath, fileName); var info = provider.GetFileInfo(fileName); Assert.IsType(typeof(NotFoundFileInfo), info); } } }
public void Configure(IApplicationBuilder app, IApplicationEnvironment appEnv, IHostingEnvironment hostEnv, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(LogginConfiguration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseIISPlatformHandler(); app.UseApiErrorHandler(); app.UseMvc(); app.UseSwaggerGen("swagger/{apiVersion}/swagger.json"); app.UseSwaggerUi("swagger/ui"); var documentationFilesProvider = new PhysicalFileProvider(appEnv.ApplicationBasePath); app.UseDocumentation(new DocumentationOptions() { DefaultFileName = "index", RequestPath = "/docs", NotFoundHtmlFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\NotFound.html"), LayoutFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\Layout.html") }); app.UseNotFoundHandler(); }
public void InvalidPath_DoesNotThrowGeneric_GetFileInfo(string path) { using (var provider = new PhysicalFileProvider(Directory.GetCurrentDirectory())) { var info = provider.GetFileInfo(path); Assert.NotNull(info); Assert.IsType<NotFoundFileInfo>(info); } }
public void Exists_WithFileStartingWithPeriod_ReturnsFalse() { // Set stuff up on disk var root = Path.GetTempPath(); var fileNameStartingWithPeriod = "." + Guid.NewGuid().ToString(); var physicalFileNameStartingWithPeriod = Path.Combine(root, fileNameStartingWithPeriod); File.WriteAllText(physicalFileNameStartingWithPeriod, "Content"); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(fileNameStartingWithPeriod); Assert.False(file.Exists); Assert.Throws<FileNotFoundException>(() => file.CreateReadStream()); }
public void RelativePathPastRootNotAllowed() { var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider; var appEnvironment = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment)); var provider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, "sub")); var info = provider.GetFileInfo("..\\File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(false); info = provider.GetFileInfo(".\\..\\File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(false); info = provider.GetFileInfo("File2.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); info.PhysicalPath.ShouldBe(Path.Combine(appEnvironment.ApplicationBasePath, "sub", "File2.txt")); }
public void Exists_WithHiddenFile_ReturnsFalse() { // Set stuff up on disk var root = Path.GetTempPath(); var tempFileName = Guid.NewGuid().ToString(); var physicalHiddenFileName = Path.Combine(root, tempFileName); File.WriteAllText(physicalHiddenFileName, "Content"); var fileInfo = new FileInfo(physicalHiddenFileName); File.SetAttributes(physicalHiddenFileName, fileInfo.Attributes | FileAttributes.Hidden); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(tempFileName); Assert.False(file.Exists); Assert.Throws<FileNotFoundException>(() => file.CreateReadStream()); }
public void AbsolutePathNotAllowed() { var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider; var appEnvironment = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment)); var provider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, "sub")); var applicationBase = appEnvironment.ApplicationBasePath; var file1 = Path.Combine(applicationBase, "File.txt"); var info = provider.GetFileInfo(file1); info.ShouldNotBe(null); info.Exists.ShouldBe(false); var file2 = Path.Combine(applicationBase, "sub", "File2.txt"); info = provider.GetFileInfo(file2); info.ShouldNotBe(null); info.Exists.ShouldBe(false); var directory1 = Path.Combine(applicationBase, "sub"); var directoryContents = provider.GetDirectoryContents(directory1); info.ShouldNotBe(null); info.Exists.ShouldBe(false); var directory2 = Path.Combine(applicationBase, "Does_Not_Exists"); directoryContents = provider.GetDirectoryContents(directory2); info.ShouldNotBe(null); info.Exists.ShouldBe(false); }
public void Exists_WithNonExistingFile_ReturnsFalse() { // Set stuff up on disk (nothing to set up here because we're testing a non-existing file) var root = Path.GetTempPath(); var nonExistingFileName = Guid.NewGuid().ToString(); // Use the file provider to try to read the file info back var provider = new PhysicalFileProvider(root); var file = provider.GetFileInfo(nonExistingFileName); Assert.False(file.Exists); Assert.Throws<FileNotFoundException>(() => file.CreateReadStream()); }
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "OldContent"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var fileInfo = provider.GetFileInfo(fileName); fileInfo.Length.ShouldBe(new FileInfo(fileInfo.PhysicalPath).Length); fileInfo.Exists.ShouldBe(true); IExpirationTrigger trigger3 = null, trigger4 = null; var trigger1 = provider.Watch(fileName); var trigger2 = provider.Watch(fileName); // Valid trigger1 created. trigger1.ShouldNotBe(null); trigger1.IsExpired.ShouldBe(false); trigger1.ActiveExpirationCallbacks.ShouldBe(true); // Valid trigger2 created. trigger2.ShouldNotBe(null); trigger2.IsExpired.ShouldBe(false); trigger2.ActiveExpirationCallbacks.ShouldBe(true); // Trigger is the same for a specific file. trigger1.ShouldBe(trigger2); trigger1.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger3 = provider.Watch(infoFromState.Name); trigger3.ShouldNotBe(null); trigger3.RegisterExpirationCallback(_ => { }, null); trigger3.IsExpired.ShouldBe(false); }, state: fileInfo); trigger2.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger4 = provider.Watch(infoFromState.Name); trigger4.ShouldNotBe(null); trigger4.RegisterExpirationCallback(_ => { }, null); trigger4.IsExpired.ShouldBe(false); }, state: fileInfo); // Write new content. File.WriteAllText(fileLocation, "OldContent + NewContent"); fileInfo.Exists.ShouldBe(true); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); trigger1.IsExpired.ShouldBe(true); trigger2.IsExpired.ShouldBe(true); // Trigger is the same for a specific file. trigger3.ShouldBe(trigger4); // A new trigger is created. trigger3.ShouldNotBe(trigger1); // Delete the file and verify file info is updated. File.Delete(fileLocation); fileInfo = provider.GetFileInfo(fileName); fileInfo.Exists.ShouldBe(false); new FileInfo(fileLocation).Exists.ShouldBe(false); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); trigger3.IsExpired.ShouldBe(true); trigger4.IsExpired.ShouldBe(true); }
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "OldContent"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var fileInfo = provider.GetFileInfo(fileName); Assert.Equal(new FileInfo(fileInfo.PhysicalPath).Length, fileInfo.Length); Assert.True(fileInfo.Exists); IExpirationTrigger trigger3 = null, trigger4 = null; var trigger1 = provider.Watch(fileName); var trigger2 = provider.Watch(fileName); // Valid trigger1 created. Assert.NotNull(trigger1); Assert.False(trigger1.IsExpired); Assert.True(trigger1.ActiveExpirationCallbacks); // Valid trigger2 created. Assert.NotNull(trigger2); Assert.False(trigger2.IsExpired); Assert.True(trigger2.ActiveExpirationCallbacks); // Trigger is the same for a specific file. Assert.Equal(trigger2, trigger1); trigger1.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger3 = provider.Watch(infoFromState.Name); Assert.NotNull(trigger3); trigger3.RegisterExpirationCallback(_ => { }, null); Assert.False(trigger3.IsExpired); }, state: fileInfo); trigger2.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger4 = provider.Watch(infoFromState.Name); Assert.NotNull(trigger4); trigger4.RegisterExpirationCallback(_ => { }, null); Assert.False(trigger4.IsExpired); }, state: fileInfo); // Write new content. File.WriteAllText(fileLocation, "OldContent + NewContent"); Assert.True(fileInfo.Exists); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); Assert.True(trigger1.IsExpired); Assert.True(trigger2.IsExpired); // Trigger is the same for a specific file. Assert.Equal(trigger4, trigger3); // A new trigger is created. Assert.NotEqual(trigger1, trigger3); // Delete the file and verify file info is updated. File.Delete(fileLocation); fileInfo = provider.GetFileInfo(fileName); Assert.False(fileInfo.Exists); Assert.False(new FileInfo(fileLocation).Exists); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); Assert.True(trigger3.IsExpired); Assert.True(trigger4.IsExpired); }
public void TokenIsSameForSamePath() { using (var root = new DisposableFileSystem()) { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(root.RootPath, fileName); using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileInfo = provider.GetFileInfo(fileName); var token1 = provider.Watch(fileName); var token2 = provider.Watch(fileName); Assert.NotNull(token1); Assert.NotNull(token2); Assert.Equal(token2, token1); } } }