aspnetcore/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/FilePathResultTest.cs

74 lines
2.5 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.HttpFeature;
using Microsoft.AspNet.PipelineCore;
using Microsoft.AspNet.Routing;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class FilePathResultTest
{
[Fact]
public void Constructor_SetsFileName()
{
// Arrange & Act
var path = Path.GetFullPath("helllo.txt");
var result = new FilePathResult(path, "text/plain");
// Act & Assert
Assert.Equal(path, result.FileName);
}
[Fact]
public async Task ExecuteResultAsync_FallsbackToStreamCopy_IfNoIHttpSendFilePresent()
{
// Arrange
var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
var result = new FilePathResult(path, "text/plain");
var httpContext = new DefaultHttpContext();
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);
}
[Fact]
public async Task ExecuteResultAsync_CallsSendFileAsync_IfIHttpSendFilePresent()
{
// Arrange
var path = Path.GetFullPath(Path.Combine("TestFiles", "FilePathResultTestFile.txt"));
var result = new FilePathResult(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 = new DefaultHttpContext();
httpContext.SetFeature(sendFileMock.Object);
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
// Act
await result.ExecuteResultAsync(context);
// Assert
sendFileMock.Verify();
}
}
}