2026-07-19 20:53:48 +00:00
|
|
|
using Heimdall.Api.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
|
2026-07-17 17:39:30 +00:00
|
|
|
namespace Heimdall.Api.Tests;
|
|
|
|
|
|
2026-07-19 20:53:48 +00:00
|
|
|
public class ExceptionFilterTests
|
2026-07-17 17:39:30 +00:00
|
|
|
{
|
2026-07-19 20:53:48 +00:00
|
|
|
private static ExceptionContext CreateContext(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
var httpContext = new DefaultHttpContext();
|
|
|
|
|
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
|
|
|
|
return new ExceptionContext(actionContext, [])
|
|
|
|
|
{
|
|
|
|
|
Exception = exception
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 17:39:30 +00:00
|
|
|
[Fact]
|
2026-07-19 20:53:48 +00:00
|
|
|
public void OnException_LogsErrorAndReturns500()
|
|
|
|
|
{
|
|
|
|
|
var filter = new ExceptionFilter(NullLogger<ExceptionFilter>.Instance);
|
|
|
|
|
var context = CreateContext(new InvalidOperationException("Test error"));
|
|
|
|
|
|
|
|
|
|
filter.OnException(context);
|
|
|
|
|
|
|
|
|
|
Assert.True(context.ExceptionHandled);
|
|
|
|
|
var result = Assert.IsType<ObjectResult>(context.Result);
|
|
|
|
|
Assert.Equal(500, result.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void OnException_ResponseContainsErrorCode()
|
|
|
|
|
{
|
|
|
|
|
var filter = new ExceptionFilter(NullLogger<ExceptionFilter>.Instance);
|
|
|
|
|
var context = CreateContext(new InvalidOperationException("Any error"));
|
|
|
|
|
|
|
|
|
|
filter.OnException(context);
|
|
|
|
|
|
|
|
|
|
var result = Assert.IsType<ObjectResult>(context.Result);
|
|
|
|
|
Assert.Equal(500, result.StatusCode);
|
|
|
|
|
Assert.NotNull(result.Value);
|
|
|
|
|
}
|
2026-07-17 17:39:30 +00:00
|
|
|
}
|