| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using Diligent.WebAPI.Host.Exceptions;
- using Diligent.WebAPI.Host.Middlewares;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Moq;
- using System.Net;
-
- namespace Tests
- {
- [TestFixture]
- public class ExceptionHandlingMiddlewareTest
- {
- [SetUp]
- public void Setup()
- {
-
- }
-
- [Test]
- public async Task ExceptionHandlingMiddleware_Returns500StatusCode_WhenArgumentNullExceptionIsThrown()
- {
- //arrange
- var expectedException = new ArgumentNullException();
- RequestDelegate mockNext = (HttpContext) =>
- {
- return Task.FromException(expectedException);
- };
- var httpContext = new DefaultHttpContext();
-
- var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
-
- var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNext, logger.Object); ;
-
- //act
- await exceptionHandlingMiddleware.InvokeAsync(httpContext);
-
- //assert
- Assert.AreEqual(HttpStatusCode.InternalServerError, (HttpStatusCode)httpContext.Response.StatusCode);
- }
-
- [Test]
- public async Task ExceptionHandlingMiddleware_Returns404StatusCode_WhenNotFoundExceptionIsThrown()
- {
- //arrange
- var expectedException = new NotFoundException();
- RequestDelegate mockNext = (HttpContext) =>
- {
- return Task.FromException(expectedException);
- };
- var httpContext = new DefaultHttpContext();
-
- var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
-
- var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNext, logger.Object); ;
-
- //act
- await exceptionHandlingMiddleware.InvokeAsync(httpContext);
-
- //assert
- Assert.AreEqual(HttpStatusCode.NotFound, (HttpStatusCode)httpContext.Response.StatusCode);
- }
-
- [Test]
- public async Task ExceptionHandlingMiddleware_ReturnsOkStatusCode_WhenNoExceptionIsThrown()
- {
- //arrange
- var expectedException = new NotFoundException();
-
- RequestDelegate mockNextMiddleware = (HttpContext) =>
- {
- return Task.CompletedTask;
- };
-
- var httpContext = new DefaultHttpContext();
- var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
-
- var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNextMiddleware, logger.Object); ;
-
- //act
- await exceptionHandlingMiddleware.InvokeAsync(httpContext);
-
- //assert
- Assert.Less((HttpStatusCode)httpContext.Response.StatusCode, 300);
- }
- }
-
- }
|