Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public void Constructor_SetsContentTypeAndParameters()
        {
            // Arrange
            var path              = Path.GetFullPath("helllo.txt");
            var contentType       = "text/plain; charset=us-ascii; p1=p1-value";
            var expectedMediaType = MediaTypeHeaderValue.Parse(contentType);

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

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

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

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

        }
Exemplo n.º 6
0
        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 VirtualFileResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

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

            // Act & Assert
            Assert.ThrowsAsync <DirectoryNotFoundException>(() => filePathResult.ExecuteResultAsync(context));
        }
Exemplo n.º 7
0
        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 VirtualFileResult(path, "text/plain")
            {
                FileProvider = fileProvider,
            };

            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);
        }
Exemplo n.º 8
0
        public void ExecuteResultAsync_ThrowsDirectoryNotFound_IfFileProviderCanNotFindTheDirectory(string path)
        {
            // Arrange
            // Point the IFileProvider root to a different subfolder
            using (var fileProvider = new PhysicalFileProvider(Path.GetFullPath("./Properties")))
            {
                var filePathResult = new VirtualFileResult(path, "text/plain")
                {
                    FileProvider = fileProvider,
                };

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

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

                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);
            }
        }
Exemplo n.º 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);
        }