Initial commit with inital CMS
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Exceptions;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Exceptions;
|
||||
|
||||
public class GlobalExceptionHandlerTests
|
||||
{
|
||||
private readonly ILogger<GlobalExceptionHandler> _logger;
|
||||
private readonly IHostEnvironment _env;
|
||||
private readonly GlobalExceptionHandler _handler;
|
||||
|
||||
public GlobalExceptionHandlerTests()
|
||||
{
|
||||
_logger = Substitute.For<ILogger<GlobalExceptionHandler>>();
|
||||
_env = Substitute.For<IHostEnvironment>();
|
||||
_handler = new GlobalExceptionHandler(_logger, _env);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryHandleAsync_ShouldReturnInternalServerError_ForGenericException()
|
||||
{
|
||||
// Arrange
|
||||
var context = new DefaultHttpContext();
|
||||
context.Response.Body = new MemoryStream();
|
||||
var exception = new Exception("Test error");
|
||||
var cancellationToken = CancellationToken.None;
|
||||
|
||||
_env.EnvironmentName.Returns("Production");
|
||||
|
||||
// Act
|
||||
var result = await _handler.TryHandleAsync(context, exception, cancellationToken);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
context.Response.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
|
||||
|
||||
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(context.Response.Body);
|
||||
var body = await reader.ReadToEndAsync();
|
||||
var response = JsonSerializer.Deserialize<ApiErrorResponse>(body, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
|
||||
response.Should().NotBeNull();
|
||||
response!.Message.Should().Be("Er is een interne serverfout opgetreden.");
|
||||
response.Detail.Should().BeNull();
|
||||
response.TraceId.Should().NotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryHandleAsync_ShouldIncludeDetails_WhenInDevelopment()
|
||||
{
|
||||
// Arrange
|
||||
var context = new DefaultHttpContext();
|
||||
context.Response.Body = new MemoryStream();
|
||||
var exception = new Exception("Test error");
|
||||
var cancellationToken = CancellationToken.None;
|
||||
|
||||
_env.EnvironmentName.Returns("Development");
|
||||
|
||||
// Act
|
||||
await _handler.TryHandleAsync(context, exception, cancellationToken);
|
||||
|
||||
// Assert
|
||||
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(context.Response.Body);
|
||||
var body = await reader.ReadToEndAsync();
|
||||
var response = JsonSerializer.Deserialize<ApiErrorResponse>(body, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
|
||||
response!.Detail.Should().Contain("Test error");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user