Esempio n. 1
0
        public async Task InvokeErrorHandler_ExceptionTest()
        {
            var applicationInsightsMock = new ApplicationInsightsSettings()
            {
                InstrumentationKey = "118047f1-b165-4bff-9471-e87fd3fe167c"
            };

            _applicationInsightsMock.Setup(x => x.Value)
            .Returns(applicationInsightsMock);

            _webHostEnvironmentMock.Setup(x => x.EnvironmentName)
            .Returns("Development");

            var httpContext             = new DefaultHttpContext().Request.HttpContext;
            var exceptionHandlerFeature = new ExceptionHandlerFeature()
            {
                Error = new Exception("Mock error exception")
            };

            httpContext.Features.Set <IExceptionHandlerFeature>(exceptionHandlerFeature);

            var errorHandlerMiddleware = new ErrorHandlerMiddleware(_applicationInsightsMock.Object, _webHostEnvironmentMock.Object);
            await errorHandlerMiddleware.Invoke(httpContext);

            Assert.NotNull(errorHandlerMiddleware);
        }
        public async Task ShouldLogHouseholdException()
        {
            // Arrange
            string expectedFormattedMsg = "test";
            string extectedErrorMsg     = "test";
            var    logger  = new Mock <ILogger <ErrorHandlerMiddleware> >();
            var    context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();

            var actual = new ErrorHandlerMiddleware(next: (innerHttpContext) =>
            {
                throw new HouseholdException(extectedErrorMsg);
            }, logger: logger.Object);

            // Act
            await actual.Invoke(context);

            // Assert
            logger.Verify(e => e.Log(
                              LogLevel.Error,
                              It.IsAny <EventId>(),
                              It.Is <FormattedLogValues>(a => a.ToString().Equals(expectedFormattedMsg)),
                              It.Is <Exception>(a => a.Message.Equals(extectedErrorMsg)),
                              It.IsAny <Func <object, Exception, string> >()),
                          Times.Once
                          );
        }
        public async Task ShouldCreateResponseForException()
        {
            // Arrange
            string expectedErrorCode = "error";
            string extectedErrorMsg  = "test";
            var    logger            = new Mock <ILogger <ErrorHandlerMiddleware> >();
            var    context           = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();

            var actual = new ErrorHandlerMiddleware(next: (innerHttpContext) =>
            {
                throw new Exception(extectedErrorMsg);
            }, logger: logger.Object);

            // Act
            await actual.Invoke(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            var reader      = new StreamReader(context.Response.Body);
            var streamText  = reader.ReadToEnd();
            var objResponse = JsonConvert.DeserializeObject <dynamic>(streamText);

            // Assert
            Assert.IsTrue(context.Response.StatusCode == (int)HttpStatusCode.BadRequest);
            Assert.IsTrue(context.Response.ContentType == "application/json");
            Assert.IsTrue(objResponse != null);
            Assert.IsTrue(objResponse.code == expectedErrorCode);
            Assert.IsTrue(objResponse.message == extectedErrorMsg);
        }
        public async Task TestHandledExceptionHandler()
        {
            middleware = new ErrorHandlerMiddleware(
                next: (innerHttpContext) => Task.FromException(new SimpliPassException("Test Exception")), logger.Object);

            await middleware.Invoke(httpContext);

            int statusCode = httpContext.Response.StatusCode;

            Assert.AreEqual(statusCode, (int)HttpStatusCode.BadRequest);
        }
        public async Task InvokeErrorHandlerMiddlewareWhenNotExistExceptionTest()
        {
            _webHostEnvironmentMock.Setup(setup => setup.EnvironmentName)
            .Returns("Development");

            var httpContext = new DefaultHttpContext().Request.HttpContext;

            var middleware = new ErrorHandlerMiddleware(_webHostEnvironmentMock.Object);
            await middleware.Invoke(httpContext);

            Assert.NotNull(middleware);
        }
Esempio n. 6
0
        public async Task InvokeErrorHandler_NotExceptionTestAsync()
        {
            var applicationInsightsMock = new ApplicationInsightsSettings("118047f1-b165-4bff-9471-e87fd3fe167c");

            _applicationInsightsMock.Setup(x => x.Value)
            .Returns(applicationInsightsMock);

            _hostingEnvironmentMock.Setup(x => x.EnvironmentName)
            .Returns("Development");

            var httpContext = new DefaultHttpContext().Request.HttpContext;

            var errorHandlerMiddleware = new ErrorHandlerMiddleware(_applicationInsightsMock.Object, _hostingEnvironmentMock.Object);
            await errorHandlerMiddleware.Invoke(httpContext);

            Assert.NotNull(errorHandlerMiddleware);
        }
        public async Task ErrorHandlerMiddleware_AoReceberUmThrow_RetornarStatus500()
        {
            // Arrange
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();

            _mocker.GetMock <RequestDelegate>()
            .Setup(x => x.Invoke(It.IsAny <HttpContext>()))
            .Throws(new Exception());

            // Act
            await _middleware.Invoke(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            var reader     = new StreamReader(context.Response.Body);
            var streamText = reader.ReadToEnd();

            //Assert
            Assert.Equal((int)HttpStatusCode.InternalServerError, context.Response.StatusCode);
            Assert.Contains("Exception", streamText);
        }