-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds ExceptionMiddleware * Adds Exception Middleware unit tests
- Loading branch information
1 parent
d53e0db
commit 239db37
Showing
4 changed files
with
118 additions
and
32 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
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
84 changes: 84 additions & 0 deletions
84
tests/core/FinancialHub.Core.WebApi.Tests/Middlewares/ExceptionMiddlewareTests.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 FinancialHub.Core.WebApi.Middlewares; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using System.IO; | ||
using System.Text.Json; | ||
|
||
namespace FinancialHub.Core.WebApi.Tests.Middleware | ||
{ | ||
public class ExceptionMiddlewareTests | ||
{ | ||
private ILogger<ExceptionMiddleware> logger; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
this.logger = new Mock<ILogger<ExceptionMiddleware>>().Object; | ||
} | ||
|
||
[Test] | ||
public void InvokeAsync_SuccessRequest_ShouldNotThrowException() | ||
{ | ||
static Task next(HttpContext hc) => Task.CompletedTask; | ||
var middleware = new ExceptionMiddleware(next, this.logger); | ||
var context = new DefaultHttpContext(); | ||
|
||
Assert.That( | ||
async () => await middleware.InvokeAsync(context), | ||
Throws.Nothing | ||
); | ||
} | ||
|
||
[Test] | ||
public async Task InvokeAsync_SuccessRequest_ShouldNotChangePayload() | ||
{ | ||
static Task next(HttpContext hc) => Task.CompletedTask; | ||
|
||
var middleware = new ExceptionMiddleware(next, this.logger); | ||
|
||
var response = new Mock<HttpResponse>(); | ||
response | ||
.SetupGet(x => x.StatusCode) | ||
.Returns(200); | ||
var context = new Mock<HttpContext>(); | ||
context | ||
.SetupGet(x => x.Response) | ||
.Returns(response.Object); | ||
context | ||
.SetupGet(x => x.Request) | ||
.Returns(new DefaultHttpContext().Request); | ||
|
||
var defaultContext = new DefaultHttpContext(); | ||
|
||
await middleware.InvokeAsync(context.Object); | ||
|
||
Assert.That(response.Object.StatusCode, Is.EqualTo(200)); | ||
} | ||
|
||
[Test] | ||
public void InvokeAsync_RequestWithUnhandledError_ShouldNotThrowException() | ||
{ | ||
static Task next(HttpContext hc) => Task.CompletedTask; | ||
var middleware = new ExceptionMiddleware(next, this.logger); | ||
var context = new DefaultHttpContext(); | ||
|
||
Assert.That( | ||
async () => await middleware.InvokeAsync(context), | ||
Throws.Nothing | ||
); | ||
} | ||
|
||
[Test] | ||
public async Task InvokeAsync_RequestWithUnhandledError_ShouldChangeStatusTo500() | ||
{ | ||
static Task next(HttpContext hc) => Task.FromException(new Exception("Error")); | ||
|
||
var middleware = new ExceptionMiddleware(next, this.logger); | ||
var context = new DefaultHttpContext(); | ||
|
||
await middleware.InvokeAsync(context); | ||
|
||
Assert.That(context.Response.StatusCode, Is.EqualTo(500)); | ||
} | ||
} | ||
} |