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_FallsBackToWebRootFileProvider_IfNoFileProviderIsPresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain"); var appEnvironment = new Mock <IWebHostEnvironment>(); 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 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_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 async Task ExecuteResultAsync_FallsBackToWebRootFileProvider_IfNoFileProviderIsPresent() { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain"); 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 context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(path, sendFileFeature.Name); Assert.Equal(0, sendFileFeature.Offset); Assert.Null(sendFileFeature.Length); }
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(); }
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 <IHttpResponseBodyFeature>(); 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_ReturnsFileContentsForRelativePaths() { // 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_CallsSendFileAsyncWithRequestedRange_IfIHttpSendFilePresent(long?start, long?end, string expectedString, long contentLength) { // Arrange var path = Path.Combine("TestFiles", "FilePathResultTestFile.txt"); var result = new TestVirtualFileResult(path, "text/plain") { FileProvider = GetFileProvider(path), EnableRangeProcessing = true, }; var sendFile = new TestSendFileFeature(); var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var appEnvironment = new Mock <IWebHostEnvironment>(); appEnvironment.Setup(app => app.WebRootFileProvider) .Returns(GetFileProvider(path)); httpContext.RequestServices = new ServiceCollection() .AddSingleton(appEnvironment.Object) .AddTransient <IActionResultExecutor <VirtualFileResult>, TestVirtualFileResultExecutor>() .AddTransient <ILoggerFactory, LoggerFactory>() .BuildServiceProvider(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.Range = new RangeHeaderValue(start, end); requestHeaders.IfUnmodifiedSince = DateTimeOffset.MinValue.AddDays(1); httpContext.Request.Method = HttpMethods.Get; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert start = start ?? 33 - end; end = start + contentLength - 1; var httpResponse = actionContext.HttpContext.Response; Assert.Equal(Path.Combine("TestFiles", "FilePathResultTestFile.txt"), sendFile.Name); Assert.Equal(start, sendFile.Offset); Assert.Equal(contentLength, sendFile.Length); Assert.Equal(CancellationToken.None, sendFile.Token); var contentRange = new ContentRangeHeaderValue(start.Value, end.Value, 33); Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers.AcceptRanges); Assert.Equal(contentRange.ToString(), httpResponse.Headers.ContentRange); Assert.NotEmpty(httpResponse.Headers.LastModified); Assert.Equal(contentLength, httpResponse.ContentLength); }
public async Task WriteFileAsync_IfRangeHeaderValid_WritesRequestedRange() { // 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 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.Status206PartialContent, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); var contentRange = new ContentRangeHeaderValue(0, 3, 33); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); Assert.Equal(4, httpResponse.ContentLength); Assert.Equal("File", body); }
public async Task WriteFileAsync_WritesRangeRequested(long?start, long?end, string expectedString, long contentLength) { // 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 httpContext = GetHttpContext(); httpContext.Response.Body = new MemoryStream(); httpContext.RequestServices = new ServiceCollection() .AddSingleton(appEnvironment.Object) .AddTransient <IActionResultExecutor <VirtualFileResult>, TestVirtualFileResultExecutor>() .AddTransient <ILoggerFactory, LoggerFactory>() .BuildServiceProvider(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.Range = new RangeHeaderValue(start, end); requestHeaders.IfUnmodifiedSince = DateTimeOffset.MinValue.AddDays(1); httpContext.Request.Method = HttpMethods.Get; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert start = start ?? 33 - end; end = start + contentLength - 1; var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; var contentRange = new ContentRangeHeaderValue(start.Value, end.Value, 33); Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Equal(contentLength, httpResponse.ContentLength); Assert.Equal(expectedString, 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 httpContext = GetHttpContext(); httpContext.Response.Body = new MemoryStream(); 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[HeaderNames.Range] = "bytes = 0-6"; 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.Status304NotModified, httpResponse.StatusCode); Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.False(httpResponse.Headers.ContainsKey(HeaderNames.ContentType)); Assert.Empty(body); }
public async Task WriteFileAsync_RangeRequestedNotSatisfiable(string rangeString) { // 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 <TestVirtualFileResultExecutor>() .AddTransient <ILoggerFactory, LoggerFactory>() .BuildServiceProvider(); var requestHeaders = httpContext.Request.GetTypedHeaders(); httpContext.Request.Headers[HeaderNames.Range] = rangeString; requestHeaders.IfUnmodifiedSince = DateTimeOffset.MinValue.AddDays(1); 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; var contentRange = new ContentRangeHeaderValue(33); Assert.Equal(StatusCodes.Status416RangeNotSatisfiable, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); }
public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestedIgnored() { // 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 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("\"NotEtag\"")); 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.Status200OK, httpResponse.StatusCode); Assert.Equal(entityTag.ToString(), httpResponse.Headers.ETag); Assert.Equal(path, sendFileFeature.Name); Assert.Equal(0, sendFileFeature.Offset); Assert.Null(sendFileFeature.Length); }
public async Task WriteFileAsync_RangeRequested_PreconditionFailed() { // 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.IfUnmodifiedSince = DateTimeOffset.MinValue; 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.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers.ContentRange); Assert.NotEmpty(httpResponse.Headers.LastModified); 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 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 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_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_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(); }
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); }