예제 #1
0
        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);
        }
예제 #3
0
        public void Constructor_SetsFileName()
        {
            // Arrange
            var path = Path.GetFullPath("helllo.txt");

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

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

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

            // Assert
            Assert.Equal(path, result.FileName);
        }
        public void Constructor_SetsContentTypeAndParameters()
        {
            // Arrange
            var path              = Path.GetFullPath("helllo.txt");
            var contentType       = "text/plain; charset=us-ascii; p1=p1-value";
            var expectedMediaType = contentType;

            // Act
            var result = new VirtualFileResult(path, contentType);

            // Assert
            Assert.Equal(path, result.FileName);
            MediaTypeAssert.Equal(expectedMediaType, result.ContentType);
        }
        public void GetFileProvider_ReturnsFileProviderFromWebHostEnvironment()
        {
            // Arrange
            var webHostFileProvider = Mock.Of <IFileProvider>();
            var webHostEnvironment  = Mock.Of <IWebHostEnvironment>(e => e.WebRootFileProvider == webHostFileProvider);

            var result = new VirtualFileResult("some-path", "text/plain");

            // Act
            var fileProvider = VirtualFileResultExecutor.GetFileProvider(result, webHostEnvironment);

            // Assert
            Assert.Same(webHostFileProvider, fileProvider);
        }
예제 #7
0
        public void Constructor_SetsContentTypeAndParameters()
        {
            // Arrange
            var path = Path.GetFullPath("helllo.txt");
            var contentType = "text/plain; charset=us-ascii; p1=p1-value";
            var expectedMediaType = contentType;

            // Act
            var result = new VirtualFileResult(path, contentType);

            // Assert
            Assert.Equal(path, result.FileName);
            MediaTypeAssert.Equal(expectedMediaType, result.ContentType);

        }
        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));
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }