-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for HttpGlobalExceptionFilter, excluded from code co…
…verage some infrastructure code
- Loading branch information
1 parent
1d00089
commit df351a8
Showing
8 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/HappyCode.NetCoreBoilerplate.Api/Infrastructure/Configurations/BannerConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/HappyCode.NetCoreBoilerplate.Api/Infrastructure/Configurations/SerilogConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions
21
src/HappyCode.NetCoreBoilerplate.Api/Infrastructure/Middlewares/ConnectionInfoMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.Infrastructure.Middlewares | ||
namespace HappyCode.NetCoreBoilerplate.Api.Infrastructure.Middlewares; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class ConnectionInfoMiddleware(RequestDelegate next, ILogger<ConnectionInfoMiddleware> logger) | ||
{ | ||
public class ConnectionInfoMiddleware(RequestDelegate next, ILogger<ConnectionInfoMiddleware> logger) | ||
{ | ||
private readonly RequestDelegate _next = next; | ||
private readonly ILogger<ConnectionInfoMiddleware> _logger = logger; | ||
private readonly RequestDelegate _next = next; | ||
private readonly ILogger<ConnectionInfoMiddleware> _logger = logger; | ||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
_logger.LogDebug("===> Connection: {Connection}", new { ConnectionId = httpContext.Connection.Id, LocalIP = httpContext.Connection.LocalIpAddress, RemoteIP = httpContext.Connection.RemoteIpAddress }); | ||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
_logger.LogDebug("===> Connection: {Connection}", new { ConnectionId = httpContext.Connection.Id, LocalIP = httpContext.Connection.LocalIpAddress, RemoteIP = httpContext.Connection.RemoteIpAddress }); | ||
|
||
await _next.Invoke(httpContext); | ||
} | ||
await _next.Invoke(httpContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...NetCoreBoilerplate.Api.UnitTests/Infrastructure/Filters/HttpGlobalExceptionFilterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.Infrastructure.Filters; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Infrastructure.Filters | ||
{ | ||
public class HttpGlobalExceptionFilterTests | ||
{ | ||
private const string _exMessage = "Some exception test message"; | ||
|
||
private readonly HttpGlobalExceptionFilter _sut; | ||
|
||
private readonly Mock<IWebHostEnvironment> _envMock; | ||
|
||
public HttpGlobalExceptionFilterTests() | ||
{ | ||
_envMock = new Mock<IWebHostEnvironment>(MockBehavior.Strict); | ||
|
||
_sut = new HttpGlobalExceptionFilter(_envMock.Object, new Mock<ILogger<HttpGlobalExceptionFilter>>().Object); | ||
} | ||
|
||
[Fact] | ||
public void When_Development_Then_exception_details_returned() | ||
{ | ||
//given | ||
_envMock.SetupGet(x => x.EnvironmentName).Returns("Development"); | ||
|
||
//when | ||
var context = GetMockedContext(); | ||
_sut.OnException(context); | ||
|
||
//then | ||
context.Result.Should().NotBeNull(); | ||
context.Result.Should().BeOfType<ObjectResult>() | ||
.Subject.StatusCode.Should().Be(StatusCodes.Status500InternalServerError); | ||
context.Result.Should().BeOfType<ObjectResult>() | ||
.Subject.Value.Should().BeOfType<ErrorResponse>() | ||
.Subject.Exception.Should().NotBeNullOrEmpty() | ||
.And.Contain(_exMessage); | ||
} | ||
|
||
[Fact] | ||
public void When_not_Development_Then_exception_details_empty() | ||
{ | ||
//given | ||
_envMock.SetupGet(x => x.EnvironmentName).Returns("Other"); | ||
|
||
//when | ||
var context = GetMockedContext(); | ||
_sut.OnException(context); | ||
|
||
//then | ||
context.Result.Should().NotBeNull(); | ||
context.Result.Should().BeOfType<ObjectResult>() | ||
.Subject.Value.Should().BeOfType<ErrorResponse>() | ||
.Subject.Exception.Should().BeNullOrEmpty(); | ||
} | ||
|
||
private ExceptionContext GetMockedContext() | ||
{ | ||
var actionContext = new ActionContext( | ||
Mock.Of<HttpContext>(), | ||
Mock.Of<RouteData>(), | ||
Mock.Of<ActionDescriptor>(), | ||
Mock.Of<ModelStateDictionary>() | ||
); | ||
|
||
return new ExceptionContext(actionContext, []) | ||
{ | ||
Exception = new Exception(_exMessage), | ||
}; | ||
} | ||
} | ||
} |