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; namespace Heimdall.Api.Tests; public class ExceptionFilterTests { 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 }; } [Fact] public void OnException_LogsErrorAndReturns500() { var filter = new ExceptionFilter(NullLogger.Instance); var context = CreateContext(new InvalidOperationException("Test error")); filter.OnException(context); Assert.True(context.ExceptionHandled); var result = Assert.IsType(context.Result); Assert.Equal(500, result.StatusCode); } [Fact] public void OnException_ResponseContainsErrorCode() { var filter = new ExceptionFilter(NullLogger.Instance); var context = CreateContext(new InvalidOperationException("Any error")); filter.OnException(context); var result = Assert.IsType(context.Result); Assert.Equal(500, result.StatusCode); Assert.NotNull(result.Value); } }