public async Task ExecuteResultAsync_FallsBackToWebRootFileProvider_IfNoFileProviderIsPresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain"); var appEnvironment = new Mock<IHostingEnvironment>(); appEnvironment.Setup(app => app.WebRootFileProvider) .Returns(GetFileProvider(path)); var httpContext = GetHttpContext(); httpContext.Response.Body = new MemoryStream(); httpContext.RequestServices = new ServiceCollection() .AddSingleton<IHostingEnvironment>(appEnvironment.Object) .AddTransient<ILoggerFactory, LoggerFactory>() .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 var path = Path.GetFullPath("helllo.txt"); // Act var result = new TestVirtualFileResult(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 TestVirtualFileResult(path, contentType); // Assert Assert.Equal(path, result.FileName); MediaTypeAssert.Equal(expectedMediaType, result.ContentType); }
public async Task WriteFileAsync_RangeProcessingNotEnabled_RangeRequestedIgnored() { // Arrange var path = Path.GetFullPath("helllo.txt"); var contentType = "text/plain; charset=us-ascii; p1=p1-value"; var result = new TestVirtualFileResult(path, contentType); var appEnvironment = new Mock <IHostingEnvironment>(); appEnvironment.Setup(app => app.WebRootFileProvider) .Returns(GetFileProvider(path)); var httpContext = GetHttpContext(); httpContext.Response.Body = new MemoryStream(); httpContext.RequestServices = new ServiceCollection() .AddSingleton(appEnvironment.Object) .AddTransient <IActionResultExecutor <VirtualFileResult>, TestVirtualFileResultExecutor>() .AddTransient <ILoggerFactory, LoggerFactory>() .BuildServiceProvider(); var entityTag = result.EntityTag = new EntityTagHeaderValue("\"Etag\""); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.IfModifiedSince = DateTimeOffset.MinValue; requestHeaders.Range = new RangeHeaderValue(0, 3); requestHeaders.IfRange = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"Etag\"")); httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); Assert.Equal("FilePathResultTestFile contents¡", body); }
public async Task WriteFileAsync_RangeRequested_NotModified() { // Arrange var path = Path.GetFullPath("helllo.txt"); var contentType = "text/plain; charset=us-ascii; p1=p1-value"; var result = new TestVirtualFileResult(path, contentType); result.EnableRangeProcessing = true; var appEnvironment = new Mock <IWebHostEnvironment>(); appEnvironment.Setup(app => app.WebRootFileProvider) .Returns(GetFileProvider(path)); var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); httpContext.RequestServices = new ServiceCollection() .AddSingleton(appEnvironment.Object) .AddTransient <IActionResultExecutor <VirtualFileResult>, TestVirtualFileResultExecutor>() .AddTransient <ILoggerFactory, LoggerFactory>() .BuildServiceProvider(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.IfModifiedSince = DateTimeOffset.MinValue.AddDays(1); httpContext.Request.Headers.Range = "bytes = 0-6"; httpContext.Request.Method = HttpMethods.Get; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers.ContentRange); Assert.NotEmpty(httpResponse.Headers.LastModified); Assert.False(httpResponse.Headers.ContainsKey(HeaderNames.ContentType)); Assert.Null(sendFileFeature.Name); // Not called }
public async Task ExecuteResultAsync_ReturnsFileContentsForRelativePaths() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(path, sendFileFeature.Name); }
public async Task ExecuteResultAsync_ReturnsFiles_ForDifferentPaths(string path) { // Arrange var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert Mock.Get(result.FileProvider).Verify(); Assert.Equal(path, sendFileFeature.Name); }
public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var httpContext = GetHttpContext(); 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_TrimsTilde_BeforeInvokingFileProvider(string path) { // Arrange var expectedPath = path.Substring(1); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(expectedPath), }; var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert Mock.Get(result.FileProvider).Verify(); Assert.Equal(expectedPath, sendFileFeature.Name); }
public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; var result = new TestVirtualFileResult( "FilePathResultTestFile_ASCII.txt", expectedContentType) { FileProvider = GetFileProvider("FilePathResultTestFile_ASCII.txt"), }; var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(expectedContentType, httpContext.Response.ContentType); Assert.Equal("FilePathResultTestFile_ASCII.txt", sendFileFeature.Name); }
public async Task ExecuteResultAsync_ReturnsFiles_ForDifferentPaths(string path) { // Arrange var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var httpContext = GetHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents¡", contents); }
public static async Task ExecuteResultAsync_ReturnsFileContentsForRelativePaths <TContext>( Func <VirtualFileResult, TContext, Task> function) { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var sendFileFeature = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFileFeature); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act object context = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext; await function(result, (TContext)context); // Assert Assert.Equal(path, sendFileFeature.Name); }
public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; var result = new TestVirtualFileResult( "FilePathResultTestFile_ASCII.txt", expectedContentType) { FileProvider = GetFileProvider("FilePathResultTestFile_ASCII.txt"), IsAscii = true, }; var httpContext = GetHttpContext(); 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 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 TestVirtualFileResult(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_CallsSendFileAsync_IfIHttpSendFilePresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), }; var sendFileMock = new Mock<IHttpSendFileFeature>(); sendFileMock .Setup(s => s.SendFileAsync(path, 0, null, CancellationToken.None)) .Returns(Task.FromResult<int>(0)); var httpContext = GetHttpContext(); httpContext.Features.Set(sendFileMock.Object); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert sendFileMock.Verify(); }
public async Task ExecuteResultAsync_TrimsTilde_BeforeInvokingFileProvider(string path) { // Arrange var expectedPath = path.Substring(1); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(expectedPath), }; var httpContext = GetHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); httpContext.Response.Body.Position = 0; // Assert var contents = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); Assert.Equal("FilePathResultTestFile contents¡", contents); Mock.Get(result.FileProvider).Verify(); }