public async Task ExecuteResultAsync_WorksWithNonDiskBasedFiles()
        {
            // Arrange
            var httpContext = GetHttpContext(typeof(VirtualFileResultExecutor));

            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 VirtualFileResult("/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 async Task ExecuteResultAsync_ThrowsFileNotFound_IfFileProviderCanNotFindTheFile()
        {
            // Arrange
            var path     = "TestPath.txt";
            var fileInfo = new Mock <IFileInfo>();

            fileInfo.SetupGet(f => f.Exists).Returns(false);
            var fileProvider = new Mock <IFileProvider>();

            fileProvider.Setup(f => f.GetFileInfo(path)).Returns(fileInfo.Object);
            var filePathResult = new VirtualFileResult(path, "text/plain")
            {
                FileProvider = fileProvider.Object,
            };

            var expectedMessage = "Could not find file: " + path;
            var context         = new ActionContext(GetHttpContext(), 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);
        }
        protected override Task ExecuteAsync(HttpContext httpContext, string path, string contentType, DateTimeOffset?lastModified = null, EntityTagHeaderValue entityTag = null, bool enableRangeProcessing = false)
        {
            var webHostEnvironment = httpContext.RequestServices.GetRequiredService <IWebHostEnvironment>();

            httpContext.RequestServices = new ServiceCollection()
                                          .AddSingleton(webHostEnvironment)
                                          .AddTransient <IActionResultExecutor <VirtualFileResult>, VirtualFileResultExecutor>()
                                          .AddTransient <ILoggerFactory, NullLoggerFactory>()
                                          .BuildServiceProvider();

            var actionContext = new ActionContext(httpContext, new(), new());
            var result        = new VirtualFileResult(path, contentType)
            {
                LastModified          = lastModified,
                EntityTag             = entityTag,
                EnableRangeProcessing = enableRangeProcessing,
            };

            return(result.ExecuteResultAsync(actionContext));
        }
        public async Task ExecuteResultAsync_ThrowsFileNotFound_IfFileProviderCanNotFindTheFile()
        {
            // Arrange
            var path = "TestPath.txt";
            var fileInfo = new Mock<IFileInfo>();
            fileInfo.SetupGet(f => f.Exists).Returns(false);
            var fileProvider = new Mock<IFileProvider>();
            fileProvider.Setup(f => f.GetFileInfo(path)).Returns(fileInfo.Object);
            var filePathResult = new VirtualFileResult(path, "text/plain")
            {
                FileProvider = fileProvider.Object,
            };

            var expectedMessage = "Could not find file: " + path;
            var context = new ActionContext(GetHttpContext(), 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 = GetHttpContext();
            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 VirtualFileResult("/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);
        }