public async Task ExecuteResultAsync_FallsBackToThePhysicalFileProvider_IfNoFileProviderIsPresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new FilePathResult(path, "text/plain"); var appEnvironment = new Mock<IHostingEnvironment>(); appEnvironment.Setup(app => app.WebRootFileProvider) .Returns(new PhysicalFileProvider(Directory.GetCurrentDirectory())); var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); httpContext.RequestServices = new ServiceCollection() .AddInstance<IHostingEnvironment>(appEnvironment.Object) .BuildServiceProvider(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert Assert.NotNull(httpContext.Response.Body); var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents", contents); }
public void Constructor_SetsFileName() { // Arrange & Act var path = Path.GetFullPath("helllo.txt"); var result = new FilePathResult(path, "text/plain"); // Act & Assert Assert.Equal(path, result.FileName); }
public void GetFilePath_Resolves_RelativePaths(string path, string relativePathToFile) { // Arrange var fileSystem = new PhysicalFileSystem(Path.GetFullPath("./TestFiles")); var expectedPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePathToFile)); var filePathResult = new FilePathResult(path, "text/plain", fileSystem); // Act var result = filePathResult.ResolveFilePath(fileSystem); // Assert Assert.Equal(expectedPath, result); }
public void NormalizePath_DoesNotConvert_InvalidVirtualPathsIntoRelativePaths(string path) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of <IFileProvider>(), }; // Act var normalizedPath = fileResult.NormalizePath(path); // Assert Assert.Equal(path, normalizedPath); }
public void NormalizePath_ConvertsBackSlashes_IntoForwardSlashes(string path, string expectedPath) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of <IFileProvider>(), }; // Act var normalizedPath = fileResult.NormalizePath(path); // Assert Assert.Equal(expectedPath, normalizedPath); }
public void GetFilePath_FailsToResolve_InvalidVirtualPaths(string path, string relativePathToFile) { // Arrange var fileSystem = new PhysicalFileSystem(Path.GetFullPath("./TestFiles")); var expectedPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePathToFile)); var filePathResult = new FilePathResult(path, "text/plain", fileSystem); // Act var ex = Assert.Throws <FileNotFoundException>(() => filePathResult.ResolveFilePath(fileSystem)); // Assert Assert.Equal("Could not find file: " + path, ex.Message); Assert.Equal(path, ex.FileName); }
public void IsPathRooted_ReturnsFalse_ForRelativePaths(string path) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of <IFileProvider>(), }; // Act var isRooted = fileResult.IsPathRooted(path); // Assert Assert.False(isRooted); }
public void GetFilePath_ThrowsFileNotFound_IfItCanNotFindTheFile(string path) { // Arrange // Point the IFileSystem root to a different subfolder var fileSystem = new PhysicalFileSystem(Path.GetFullPath("./Utils")); var filePathResult = new FilePathResult(path, "text/plain", fileSystem); var expectedFileName = path.TrimStart('~').Replace('\\', '/'); var expectedMessage = "Could not find file: " + expectedFileName; // Act var ex = Assert.Throws <FileNotFoundException>(() => filePathResult.ResolveFilePath(fileSystem)); // Assert Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedFileName, ex.FileName); }
internal static bool TryGetCachedCompositeFileResult(FileSystemHelper fileSystemHelper, string filesetKey, CompressionType type, string mime, out FileResult result, out DateTime lastWriteTime) { result = null; lastWriteTime = DateTime.Now; var filesetPath = fileSystemHelper.GetCurrentCompositeFilePath(type, filesetKey); if (System.IO.File.Exists(filesetPath)) { lastWriteTime = System.IO.File.GetLastWriteTime(filesetPath); //FilePathResult uses IHttpSendFileFeature which is a native host option for sending static files result = new FilePathResult(filesetPath, mime); return true; } return false; }
public void GetFilePath_Resolves_RelativePaths(string path, string relativePathToFile) { // Arrange var expectedPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePathToFile)); var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./TestFiles")); var filePathResult = new FilePathResult(path, "text/plain") { FileProvider = fileProvider, }; // Act var resolveFilePathResult = filePathResult.ResolveFilePath(fileProvider); // Assert Assert.NotNull(resolveFilePathResult); Assert.NotNull(resolveFilePathResult.FileInfo); Assert.Equal(expectedPath, resolveFilePathResult.PhysicalFilePath); }
public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent() { // Arrange var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")); var result = new FilePathResult(path, "text/plain") { FileProvider = new PhysicalFileProvider(Path.GetFullPath(".")), }; var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert Assert.NotNull(httpContext.Response.Body); var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents", contents); }
public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent() { // Arrange var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")); var fileSystem = new PhysicalFileSystem(Path.GetFullPath(".")); var result = new FilePathResult(path, "text/plain", fileSystem); var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert Assert.NotNull(httpContext.Response.Body); var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents", contents); }
public void GetFilePath_FailsToResolve_InvalidVirtualPaths(string path, string relativePathToFile) { // Arrange var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./TestFiles")); var expectedPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePathToFile)); var filePathResult = new FilePathResult(path, "text/plain") { FileProvider = fileProvider, }; // Act var ex = Assert.Throws<FileNotFoundException>(() => filePathResult.ResolveFilePath(fileProvider)); // Assert Assert.Equal("Could not find file: " + path, ex.Message); Assert.Equal(path, ex.FileName); }
public void GetFilePath_Resolves_RelativePaths(string path, string relativePathToFile) { // Arrange var expectedPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePathToFile)); var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./TestFiles")); var filePathResult = new FilePathResult(path, "text/plain") { FileProvider = fileProvider, }; // Act var resolveFilePathResult = filePathResult.ResolveFilePath(fileProvider); // Assert Assert.NotNull(resolveFilePathResult); Assert.NotNull(resolveFilePathResult.FileInfo); Assert.Equal(expectedPath, resolveFilePathResult.PhysicalFilePath); }
public async Task ExecuteResultAsync_WorksWithNonDiskBasedFiles() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var expectedData = "This is an embedded resource"; var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(expectedData)); var nonDiskFileInfo = new Mock<IFileInfo>(); nonDiskFileInfo.SetupGet(fi => fi.Exists).Returns(true); nonDiskFileInfo.SetupGet(fi => fi.PhysicalPath).Returns(() => null); // set null to indicate non-disk file nonDiskFileInfo.Setup(fi => fi.CreateReadStream()).Returns(sourceStream); var nonDiskFileProvider = new Mock<IFileProvider>(); nonDiskFileProvider.Setup(fp => fp.GetFileInfo(It.IsAny<string>())).Returns(nonDiskFileInfo.Object); var filePathResult = new FilePathResult("/SampleEmbeddedFile.txt", "text/plain") { FileProvider = nonDiskFileProvider.Object }; // Act await filePathResult.ExecuteResultAsync(actionContext); // Assert httpContext.Response.Body.Position = 0; var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal(expectedData, contents); }
public void GetFilePath_ThrowsFileNotFound_IfItCanNotFindTheFile(string path) { // Arrange // Point the IFileProvider root to a different subfolder var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./Utils")); var filePathResult = new FilePathResult(path, "text/plain") { FileProvider = fileProvider, }; var expectedFileName = path.TrimStart('~').Replace('\\', '/'); var expectedMessage = "Could not find file: " + expectedFileName; // Act var ex = Assert.Throws<FileNotFoundException>(() => filePathResult.ResolveFilePath(fileProvider)); // Assert Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedFileName, ex.FileName); }
public void NormalizePath_DoesNotConvert_InvalidVirtualPathsIntoRelativePaths(string path) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of<IFileProvider>(), }; // Act var normalizedPath = fileResult.NormalizePath(path); // Assert Assert.Equal(path, normalizedPath); }
public void NormalizePath_ConvertsBackSlashes_IntoForwardSlashes(string path, string expectedPath) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of<IFileProvider>(), }; // Act var normalizedPath = fileResult.NormalizePath(path); // Assert Assert.Equal(expectedPath, normalizedPath); }
public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; // path will be C:/.../TestFiles/FilePathResultTestFile_ASCII.txt var path = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile_ASCII.txt")); path = path.Replace(@"\", "/"); // Point the FileProviderRoot to a subfolder var result = new FilePathResult(path, MediaTypeHeaderValue.Parse(expectedContentType)) { FileProvider = new PhysicalFileProvider(Path.GetFullPath("Utils")), }; var httpContext = new DefaultHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert var contents = Encoding.ASCII.GetString(memoryStream.ToArray()); Assert.Equal("FilePathResultTestFile contents ASCII encoded", contents); Assert.Equal(expectedContentType, httpContext.Response.ContentType); }
public void IsPathRooted_ReturnsFalse_ForRelativePaths(string path) { // Arrange var fileResult = new FilePathResult(path, "text/plain") { FileProvider = Mock.Of<IFileProvider>(), }; // Act var isRooted = fileResult.IsPathRooted(path); // Assert Assert.False(isRooted); }
public async Task ExecuteResultAsync_WorksWithAbsolutePaths_UsingBackSlash() { // Arrange // path will be C:\...\TestFiles\FilePathResultTestFile.txt var path = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile.txt")); // We want ot ensure that the path that we provide has backslashes to ensure they get normalized into // forward slashes. path = path.Replace('/', '\\'); // Point the FileProviderRoot to a subfolder var result = new FilePathResult(path, "text/plain") { FileProvider = new PhysicalFileProvider(Path.GetFullPath("Utils")), }; var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert Assert.NotNull(httpContext.Response.Body); var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents", contents); }
public async Task ExecuteResultAsync_CallsSendFileAsync_IfIHttpSendFilePresent() { // Arrange var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")); var result = new FilePathResult(path, "text/plain") { FileProvider = new PhysicalFileProvider(Path.GetFullPath(".")), }; var sendFileMock = new Mock<IHttpSendFileFeature>(); sendFileMock .Setup(s => s.SendFileAsync(path, 0, null, CancellationToken.None)) .Returns(Task.FromResult<int>(0)); var httpContext = new DefaultHttpContext(); httpContext.SetFeature(sendFileMock.Object); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert sendFileMock.Verify(); }
public async Task ExecuteResultAsync_WorksWithAbsolutePaths_UsingForwardSlash() { // Arrange // path will be C:/.../TestFiles/FilePathResultTestFile.txt var path = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile.txt")); path = path.Replace(@"\", "/"); // Point the FileProviderRoot to a subfolder var result = new FilePathResult(path, "text/plain") { FileProvider = new PhysicalFileProvider(Path.GetFullPath("Utils")), }; var httpContext = new DefaultHttpContext(); httpContext.Response.Body = new MemoryStream(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert Assert.NotNull(httpContext.Response.Body); var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents", contents); }