114 lines
3.5 KiB
C#
114 lines
3.5 KiB
C#
// Copyright (c) .NET Foundation. 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.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
using Microsoft.AspNetCore.Mvc.Internal;
|
|
using Microsoft.AspNetCore.Mvc.TestCommon;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Testing;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNetCore.Mvc
|
|
{
|
|
public class FileContentResultTest
|
|
{
|
|
[Fact]
|
|
public void Constructor_SetsFileContents()
|
|
{
|
|
// Arrange
|
|
var fileContents = new byte[0];
|
|
|
|
// Act
|
|
var result = new FileContentResult(fileContents, "text/plain");
|
|
|
|
// Assert
|
|
Assert.Same(fileContents, result.FileContents);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_SetsContentTypeAndParameters()
|
|
{
|
|
// Arrange
|
|
var fileContents = new byte[0];
|
|
var contentType = "text/plain; charset=us-ascii; p1=p1-value";
|
|
var expectedMediaType = contentType;
|
|
|
|
// Act
|
|
var result = new FileContentResult(fileContents, contentType);
|
|
|
|
// Assert
|
|
Assert.Same(fileContents, result.FileContents);
|
|
MediaTypeAssert.Equal(expectedMediaType, result.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WriteFileAsync_CopiesBuffer_ToOutputStream()
|
|
{
|
|
// Arrange
|
|
var buffer = new byte[] { 1, 2, 3, 4, 5 };
|
|
|
|
var httpContext = GetHttpContext();
|
|
|
|
var outStream = new MemoryStream();
|
|
httpContext.Response.Body = outStream;
|
|
|
|
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
|
|
|
var result = new FileContentResult(buffer, "text/plain");
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(context);
|
|
|
|
// Assert
|
|
Assert.Equal(buffer, outStream.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding()
|
|
{
|
|
// Arrange
|
|
var expectedContentType = "text/foo; charset=us-ascii";
|
|
var buffer = new byte[] { 1, 2, 3, 4, 5 };
|
|
|
|
var httpContext = GetHttpContext();
|
|
|
|
var outStream = new MemoryStream();
|
|
httpContext.Response.Body = outStream;
|
|
|
|
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
|
|
|
var result = new FileContentResult(buffer, expectedContentType);
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(context);
|
|
|
|
// Assert
|
|
Assert.Equal(buffer, outStream.ToArray());
|
|
Assert.Equal(expectedContentType, httpContext.Response.ContentType);
|
|
}
|
|
|
|
private static IServiceCollection CreateServices()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<FileContentResultExecutor>();
|
|
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
|
|
|
|
return services;
|
|
}
|
|
|
|
private static HttpContext GetHttpContext()
|
|
{
|
|
var services = CreateServices();
|
|
|
|
var httpContext = new DefaultHttpContext();
|
|
httpContext.RequestServices = services.BuildServiceProvider();
|
|
|
|
return httpContext;
|
|
}
|
|
}
|
|
} |