87 lines
3.1 KiB
C#
87 lines
3.1 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.Buffers;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc.Internal;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using Xunit;
|
|
|
|
namespace System.Web.Http
|
|
{
|
|
public class ExceptionResultTest
|
|
{
|
|
[Fact]
|
|
public async Task ExceptionResult_SetsStatusCode()
|
|
{
|
|
// Arrange
|
|
var httpContext = new DefaultHttpContext();
|
|
httpContext.RequestServices = CreateServices();
|
|
|
|
var stream = new MemoryStream();
|
|
httpContext.Response.Body = stream;
|
|
|
|
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
|
var result = new ExceptionResult(new Exception("hello, world!"), includeErrorDetail: false);
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(context);
|
|
|
|
// Assert
|
|
Assert.Equal(StatusCodes.Status500InternalServerError, context.HttpContext.Response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExceptionResult_WritesHttpError()
|
|
{
|
|
// Arrange
|
|
var httpContext = new DefaultHttpContext();
|
|
httpContext.RequestServices = CreateServices();
|
|
|
|
var stream = new MemoryStream();
|
|
httpContext.Response.Body = stream;
|
|
|
|
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
|
var result = new ExceptionResult(new Exception("hello, world!"), includeErrorDetail: false);
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(context);
|
|
|
|
// Assert
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
var content = reader.ReadToEnd();
|
|
Assert.Equal("{\"Message\":\"An error has occurred.\"}", content);
|
|
}
|
|
}
|
|
|
|
private static IServiceProvider CreateServices()
|
|
{
|
|
var options = Options.Create(new MvcOptions());
|
|
options.Value.OutputFormatters.Add(new StringOutputFormatter());
|
|
options.Value.OutputFormatters.Add(new JsonOutputFormatter(
|
|
new JsonSerializerSettings(),
|
|
ArrayPool<char>.Shared));
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<IActionResultExecutor<ObjectResult>>(new ObjectResultExecutor(
|
|
new DefaultOutputFormatterSelector(options, NullLoggerFactory.Instance),
|
|
new TestHttpResponseStreamWriterFactory(),
|
|
NullLoggerFactory.Instance));
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
}
|
|
}
|