aspnetcore/test/Microsoft.AspNetCore.Mvc.Co.../FileContentResultTest.cs

157 lines
5.0 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.Http.Features;
using Microsoft.AspNetCore.Mvc.Abstractions;
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);
}
[Fact]
public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable()
{
// Arrange
var expectedContentType = "text/foo; charset=us-ascii";
var buffer = new byte[] { 1, 2, 3, 4, 5 };
var httpContext = GetHttpContext();
var bufferingFeature = new TestBufferingFeature();
httpContext.Features.Set<IHttpBufferingFeature>(bufferingFeature);
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);
Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
}
private static IServiceCollection CreateServices()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
return services;
}
private static HttpContext GetHttpContext()
{
var services = CreateServices();
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
private class TestBufferingFeature : IHttpBufferingFeature
{
public bool DisableResponseBufferingInvoked { get; private set; }
public bool DisableRequestBufferingInvoked { get; private set; }
public void DisableRequestBuffering()
{
DisableRequestBufferingInvoked = true;
}
public void DisableResponseBuffering()
{
DisableResponseBufferingInvoked = true;
}
}
}
}