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 VirtualFileProviderResult("/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 Constructor_SetsFileName()
        {
            // Arrange
            var path = Path.GetFullPath("helllo.txt");

            // Act
            var result = new VirtualFileProviderResult(path, "text/plain");

            // Assert
            Assert.Equal(path, result.FileName);
        }
        public void Constructor_SetsFileName()
        {
            // Arrange
            var path = Path.GetFullPath("helllo.txt");

            // Act
            var result = new VirtualFileProviderResult(path, "text/plain");

            // Assert
            Assert.Equal(path, result.FileName);
        }
        public void ExecuteResultAsync_ThrowsDirectoryNotFound_IfFileProviderCanNotFindTheDirectory(string path)
        {
            // Arrange
            // Point the IFileProvider root to a different subfolder
            var fileProvider   = new PhysicalFileProvider(Path.GetFullPath("./Properties"));
            var filePathResult = new VirtualFileProviderResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync <DirectoryNotFoundException>(() => filePathResult.ExecuteResultAsync(context));
        }
        public async Task ExecuteResultAsync_ThrowsFileNotFound_IfFileProviderCanNotFindTheFile(string path)
        {
            // Arrange
            // Point the IFileProvider root to a different subfolder
            var fileProvider   = new PhysicalFileProvider(Path.GetFullPath("./Properties"));
            var filePathResult = new VirtualFileProviderResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

            var expectedMessage = "Could not find file: " + path;
            var context         = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act
            var ex = await Assert.ThrowsAsync <FileNotFoundException>(() => filePathResult.ExecuteResultAsync(context));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
            Assert.Equal(path, ex.FileName);
        }
        public void ExecuteResultAsync_ThrowsDirectoryNotFound_IfFileProviderCanNotFindTheDirectory(string path)
        {
            // Arrange
            // Point the IFileProvider root to a different subfolder
            var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./Properties"));
            var filePathResult = new VirtualFileProviderResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync<DirectoryNotFoundException>(() => filePathResult.ExecuteResultAsync(context));
        }
        public async Task ExecuteResultAsync_ThrowsFileNotFound_IfFileProviderCanNotFindTheFile(string path)
        {
            // Arrange
            // Point the IFileProvider root to a different subfolder
            var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./Properties"));
            var filePathResult = new VirtualFileProviderResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

            var expectedMessage = "Could not find file: " + path;
            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act
            var ex = await Assert.ThrowsAsync<FileNotFoundException>(() => filePathResult.ExecuteResultAsync(context));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
            Assert.Equal(path, ex.FileName);
        }
        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 VirtualFileProviderResult("/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);
        }