Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExceptionHandlingMiddlewareTest.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Diligent.WebAPI.Host.Exceptions;
  2. using Diligent.WebAPI.Host.Middlewares;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Logging;
  5. using Moq;
  6. using System.Net;
  7. namespace Tests
  8. {
  9. [TestFixture]
  10. public class ExceptionHandlingMiddlewareTest
  11. {
  12. [SetUp]
  13. public void Setup()
  14. {
  15. }
  16. [Test]
  17. public async Task ExceptionHandlingMiddleware_Returns500StatusCode_WhenArgumentNullExceptionIsThrown()
  18. {
  19. //arrange
  20. var expectedException = new ArgumentNullException();
  21. RequestDelegate mockNext = (HttpContext) =>
  22. {
  23. return Task.FromException(expectedException);
  24. };
  25. var httpContext = new DefaultHttpContext();
  26. var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
  27. var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNext, logger.Object); ;
  28. //act
  29. await exceptionHandlingMiddleware.InvokeAsync(httpContext);
  30. //assert
  31. Assert.AreEqual(HttpStatusCode.InternalServerError, (HttpStatusCode)httpContext.Response.StatusCode);
  32. }
  33. [Test]
  34. public async Task ExceptionHandlingMiddleware_Returns404StatusCode_WhenNotFoundExceptionIsThrown()
  35. {
  36. //arrange
  37. var expectedException = new NotFoundException();
  38. RequestDelegate mockNext = (HttpContext) =>
  39. {
  40. return Task.FromException(expectedException);
  41. };
  42. var httpContext = new DefaultHttpContext();
  43. var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
  44. var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNext, logger.Object); ;
  45. //act
  46. await exceptionHandlingMiddleware.InvokeAsync(httpContext);
  47. //assert
  48. Assert.AreEqual(HttpStatusCode.NotFound, (HttpStatusCode)httpContext.Response.StatusCode);
  49. }
  50. [Test]
  51. public async Task ExceptionHandlingMiddleware_ReturnsOkStatusCode_WhenNoExceptionIsThrown()
  52. {
  53. //arrange
  54. var expectedException = new NotFoundException();
  55. RequestDelegate mockNextMiddleware = (HttpContext) =>
  56. {
  57. return Task.CompletedTask;
  58. };
  59. var httpContext = new DefaultHttpContext();
  60. var logger = new Mock<ILogger<ExceptionHandlingMiddleware>>();
  61. var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware(mockNextMiddleware, logger.Object); ;
  62. //act
  63. await exceptionHandlingMiddleware.InvokeAsync(httpContext);
  64. //assert
  65. Assert.Less((HttpStatusCode)httpContext.Response.StatusCode, 300);
  66. }
  67. }
  68. }