public static async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding <TContext>(
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var expectedContentType = "text/foo; charset=us-ascii";
            var path        = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile_ASCII.txt"));
            var result      = new TestPhysicalFileResult(path, expectedContentType);
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            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(expectedContentType, httpContext.Response.ContentType);
            Assert.Equal(Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile_ASCII.txt")), sendFile.Name);
            Assert.Equal(0, sendFile.Offset);
            Assert.Null(sendFile.Length);
            Assert.Equal(CancellationToken.None, sendFile.Token);
        }
        public async Task WriteFileAsync_WritesRangeRequested(long?start, long?end, string expectedString, long contentLength)
        {
            // Arrange
            var path           = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result         = new TestPhysicalFileResult(path, "text/plain");
            var httpContext    = GetHttpContext();
            var requestHeaders = httpContext.Request.GetTypedHeaders();

            requestHeaders.IfModifiedSince = DateTimeOffset.MinValue;
            requestHeaders.Range           = new RangeHeaderValue(start, end);
            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
            start = start ?? 34 - 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, 34);

            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 static async Task ExecuteResultAsync_WorksWithAbsolutePaths <TContext>(
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);

            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.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile.txt")), sendFile.Name);
            Assert.Equal(0, sendFile.Offset);
            Assert.Null(sendFile.Length);
            Assert.Equal(CancellationToken.None, sendFile.Token);
        }
        public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestedIgnored()
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            result.EnableRangeProcessing = true;
            var entityTag   = result.EntityTag = new EntityTagHeaderValue("\"Etag\"");
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            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.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
            Assert.Equal(Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")), sendFile.Name);
            Assert.Equal(0, sendFile.Offset);
            Assert.Null(sendFile.Length);
        }
        public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestedIgnored()
        {
            // Arrange
            var path           = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result         = new TestPhysicalFileResult(path, "text/plain");
            var entityTag      = result.EntityTag = new EntityTagHeaderValue("\"Etag\"");
            var httpContext    = GetHttpContext();
            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;
            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("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
            Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
            Assert.Equal("FilePathResultTestFile contents�", body);
        }
        public static async Task ExecuteResultAsync_CallsSendFileAsync_IfIHttpSendFilePresent <TContext>(
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path         = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result       = new TestPhysicalFileResult(path, "text/plain");
            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 actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            // Act
            object context = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext;

            await function(result, (TContext)context);

            // Assert
            sendFileMock.Verify();
        }
        public async Task WriteFileAsync_RangeRequested_NotModified()
        {
            // Arrange
            var path           = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result         = new TestPhysicalFileResult(path, "text/plain");
            var httpContext    = GetHttpContext();
            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.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
            Assert.Null(httpResponse.ContentLength);
            Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
            Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
            Assert.Empty(body);
        }
        public static async Task WriteFileAsync_RangeHeaderMalformed_RangeRequestIgnored <TContext>(
            string rangeString,
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path        = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result      = new TestPhysicalFileResult(path, "text/plain");
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            var requestHeaders = httpContext.Request.GetTypedHeaders();

            requestHeaders.IfModifiedSince    = DateTimeOffset.MinValue;
            httpContext.Request.Headers.Range = rangeString;
            httpContext.Request.Method        = HttpMethods.Get;
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            // Act
            object context = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext;

            await function(result, (TContext)context);

            // Assert
            var httpResponse = actionContext.HttpContext.Response;

            Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
            Assert.Empty(httpResponse.Headers.ContentRange);
            Assert.NotEmpty(httpResponse.Headers.LastModified);
            Assert.Equal(Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")), sendFile.Name);
            Assert.Equal(0, sendFile.Offset);
            Assert.Null(sendFile.Length);
        }
        public static async Task WriteFileAsync_RangeRequested_PreconditionFailed <TContext>(
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            result.EnableRangeProcessing = true;
            var httpContext    = GetHttpContext();
            var requestHeaders = httpContext.Request.GetTypedHeaders();

            requestHeaders.IfUnmodifiedSince  = DateTimeOffset.MinValue;
            httpContext.Request.Headers.Range = "bytes = 0-6";
            httpContext.Request.Method        = HttpMethods.Get;
            httpContext.Response.Body         = new MemoryStream();
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            // Act
            object context = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext;

            await function(result, (TContext)context);

            // 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.Status412PreconditionFailed, httpResponse.StatusCode);
            Assert.Null(httpResponse.ContentLength);
            Assert.Empty(httpResponse.Headers.ContentRange);
            Assert.NotEmpty(httpResponse.Headers.LastModified);
            Assert.Empty(body);
        }
Ejemplo n.º 10
0
        public async Task WriteFileAsync_RangeRequestedNotSatisfiable(string rangeString)
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            result.EnableRangeProcessing = true;
            var httpContext    = GetHttpContext();
            var requestHeaders = httpContext.Request.GetTypedHeaders();

            requestHeaders.IfModifiedSince = DateTimeOffset.MinValue;
            httpContext.Request.Headers[HeaderNames.Range] = rangeString;
            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(34);

            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 void ExecuteAsync_ThrowsDirectoryNotFound_IfItCanNotFindTheDirectory_ForRootPaths(string path)
        {
            // Arrange
            var result  = new TestPhysicalFileResult(path, "text/plain");
            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync <DirectoryNotFoundException>(() => result.ExecuteResultAsync(context));
        }
        public void ExecuteAsync_ThrowsFileNotFound_WhenFileDoesNotExist_ForRootPaths(string path)
        {
            // Arrange
            var result  = new TestPhysicalFileResult(path, "text/plain");
            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync <FileNotFoundException>(() => result.ExecuteResultAsync(context));
        }
        public void Constructor_SetsFileName()
        {
            // Arrange
            var path = Path.GetFullPath("helllo.txt");

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

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

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

            // Assert
            Assert.Equal(path, result.FileName);
        }
        public async Task ExecuteAsync_ThrowsNotSupported_ForNonRootedPaths(string path)
        {
            // Arrange
            var result          = new TestPhysicalFileResult(path, "text/plain");
            var context         = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
            var expectedMessage = $"Path '{path}' was not rooted.";

            // Act
            var ex = await Assert.ThrowsAsync <NotSupportedException>(() => result.ExecuteResultAsync(context));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
        }
Ejemplo n.º 16
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 TestPhysicalFileResult(path, contentType);

            // Assert
            Assert.Equal(path, result.FileName);
            MediaTypeAssert.Equal(expectedMediaType, result.ContentType);
        }
        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 TestPhysicalFileResult(path, contentType);

            // Assert
            Assert.Equal(path, result.FileName);
            MediaTypeAssert.Equal(expectedMediaType, result.ContentType);
        }
        public static void ExecuteAsync_ThrowsFileNotFound_WhenFileDoesNotExist_ForRootPaths <TContext>(
            string path,
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var result        = new TestPhysicalFileResult(path, "text/plain");
            var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            object context = typeof(TContext) == typeof(HttpContext) ? actionContext.HttpContext : actionContext;

            Assert.ThrowsAsync <FileNotFoundException>(
                () => function(result, (TContext)context));
        }
        public static async Task ExecuteAsync_ThrowsNotSupported_ForNonRootedPaths <TContext>(
            string path,
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var result          = new TestPhysicalFileResult(path, "text/plain");
            var actionContext   = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
            var expectedMessage = $"Path '{path}' was not rooted.";

            // Act
            object context = typeof(TContext) == typeof(HttpContext) ? actionContext.HttpContext : actionContext;
            var    ex      = await Assert.ThrowsAsync <NotSupportedException>(
                () => function(result, (TContext)context));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
        }
        public static async Task ExecuteResultAsync_CallsSendFileAsyncWithRequestedRange_IfIHttpSendFilePresent <TContext>(
            long?start,
            long?end,
            long contentLength,
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            result.EnableRangeProcessing = true;
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            var context        = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            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
            object functionContext = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext;

            await function(result, (TContext)functionContext);

            // Assert
            start = start ?? 34 - end;
            end   = start + contentLength - 1;
            var httpResponse = actionContext.HttpContext.Response;

            Assert.Equal(Path.GetFullPath(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, 34);

            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);
        }
Ejemplo n.º 21
0
        public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent()
        {
            // Arrange
            var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");
            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);
        }
Ejemplo n.º 22
0
        public async Task ExecuteResultAsync_CallsSendFileAsync_IfIHttpSendFilePresent()
        {
            // Arrange
            var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");
            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 static async Task WriteFileAsync_IfRangeHeaderValid_WritesRequestedRange <TContext>(
            Func <PhysicalFileResult, TContext, Task> function)
        {
            // Arrange
            var path      = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result    = new TestPhysicalFileResult(path, "text/plain");
            var entityTag = result.EntityTag = new EntityTagHeaderValue("\"Etag\"");

            result.EnableRangeProcessing = true;
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            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;
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            // Act
            object context = typeof(TContext) == typeof(HttpContext) ? httpContext : actionContext;

            await function(result, (TContext)context);

            // Assert
            var httpResponse = actionContext.HttpContext.Response;

            Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode);
            Assert.Equal("bytes", httpResponse.Headers.AcceptRanges);
            var contentRange = new ContentRangeHeaderValue(0, 3, 34);

            Assert.Equal(contentRange.ToString(), httpResponse.Headers.ContentRange);
            Assert.NotEmpty(httpResponse.Headers.LastModified);
            Assert.Equal(entityTag.ToString(), httpResponse.Headers.ETag);
            Assert.Equal(4, httpResponse.ContentLength);
            Assert.Equal(Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")), sendFile.Name);
            Assert.Equal(0, sendFile.Offset);
            Assert.Equal(4, sendFile.Length);
        }
        public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent()
        {
            // Arrange
            var path        = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result      = new TestPhysicalFileResult(path, "text/plain");
            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_CallsSendFileAsync_IfIHttpSendFilePresent()
        {
            // Arrange
            var path         = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result       = new TestPhysicalFileResult(path, "text/plain");
            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 path   = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile_ASCII.txt"));
            var result = new TestPhysicalFileResult(path, expectedContentType)
            {
                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 WriteFileAsync_WritesRangeRequested(long?start, long?end, string expectedString, long contentLength)
        {
            // Arrange
            var path   = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
            var result = new TestPhysicalFileResult(path, "text/plain");

            result.EnableRangeProcessing = true;
            var sendFile    = new TestSendFileFeature();
            var httpContext = GetHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(sendFile);
            var requestHeaders = httpContext.Request.GetTypedHeaders();

            requestHeaders.IfModifiedSince = DateTimeOffset.MinValue;
            requestHeaders.Range           = new RangeHeaderValue(start, end);
            httpContext.Request.Method     = HttpMethods.Get;
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            var startResult  = start ?? 34 - end;
            var endResult    = startResult + contentLength - 1;
            var httpResponse = actionContext.HttpContext.Response;
            var contentRange = new ContentRangeHeaderValue(startResult.Value, endResult.Value, 34);

            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(Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt")), sendFile.Name);
            Assert.Equal(startResult, sendFile.Offset);
            Assert.Equal((long?)contentLength, sendFile.Length);
        }
Ejemplo n.º 28
0
        public void ExecuteAsync_ThrowsDirectoryNotFound_IfItCanNotFindTheDirectory_ForRootPaths(string path)
        {
            // Arrange
            var result = new TestPhysicalFileResult(path, "text/plain");
            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync<DirectoryNotFoundException>(() => result.ExecuteResultAsync(context));
        }
Ejemplo n.º 29
0
        public void ExecuteAsync_ThrowsFileNotFound_WhenFileDoesNotExist_ForRootPaths(string path)
        {
            // Arrange
            var result = new TestPhysicalFileResult(path, "text/plain");
            var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());

            // Act & Assert
            Assert.ThrowsAsync<FileNotFoundException>(() => result.ExecuteResultAsync(context));
        }
Ejemplo n.º 30
0
        public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding()
        {
            // Arrange
            var expectedContentType = "text/foo; charset=us-ascii";
            var path = Path.GetFullPath(Path.Combine(".", "TestFiles", "FilePathResultTestFile_ASCII.txt"));
            var result = new TestPhysicalFileResult(path, MediaTypeHeaderValue.Parse(expectedContentType))
            {
                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);
        }
Ejemplo n.º 31
0
        public async Task ExecuteAsync_ThrowsNotSupported_ForNonRootedPaths(string path)
        {
            // Arrange
            var result = new TestPhysicalFileResult(path, "text/plain");
            var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
            var expectedMessage = $"Path '{path}' was not rooted.";

            // Act
            var ex = await Assert.ThrowsAsync<NotSupportedException>(() => result.ExecuteResultAsync(context));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
        }