public async Task InvokeAsyncThrowsExceptionTest() { var problemDetailsFactory = SetupProblemDetailsFactory(); var expectedProblemDetails = problemDetailsFactory.InternalServerError(); var expectedSerializedProblemDetails = JsonSerializer.Serialize(expectedProblemDetails); var expectedProblemDetailsBytes = Encoding.UTF8.GetBytes(expectedSerializedProblemDetails); var expectedArgumentException = new ArgumentException("Test Message"); var headers = SetupHeaders(); var mockLogger = new Mock <ILogger <ExceptionHandlerMiddleware> >(); var mockHttpRequest = mockLogger.SetupHttpRequest(); var mockHttpResponse = SetupHttpResponse(mockLogger, headers, expectedProblemDetailsBytes); var mockConnectionInfo = mockLogger.SetupConnectionInfo(); var mockHttpContext = mockLogger.SetupHttpContext(mockHttpRequest, mockHttpResponse, mockConnectionInfo); var mockRequestDelegate = SetupRequestDelegate(mockHttpContext, expectedArgumentException); var middleware = new ExceptionHandlerMiddleware(mockRequestDelegate.Object, mockLogger.Object, problemDetailsFactory); await middleware.InvokeAsync(mockHttpContext.Object); VerifyRequestDelegate(mockHttpContext, mockRequestDelegate); AssertHeaders(headers); VerifyContext(mockHttpContext); VerifyHttpResponse(mockHttpResponse, expectedProblemDetailsBytes); VerifyLogs(mockLogger, expectedSerializedProblemDetails, expectedArgumentException); mockLogger.VerifyBeginScope("Internal Server Error"); mockLogger.VerifyLogResponse(); }
public async Task InvokeAsync_NoExceptionFromNextMiddleware() { ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware() { Next = new DummyMiddleware() }; var context = this.GetContext(); await middleware.InvokeAsync(context); context.Response.StatusCode.ShouldBe(202); }
public async Task InvokeAsyncSuccessTest() { var mockHttpContext = new Mock <HttpContext>(); var mockRequestDelegate = new Mock <RequestDelegate>(); mockRequestDelegate.Setup(requestDelegate => requestDelegate.Invoke(mockHttpContext.Object)); var mockLogger = new Mock <ILogger <ExceptionHandlerMiddleware> >(); var middleware = new ExceptionHandlerMiddleware(mockRequestDelegate.Object, mockLogger.Object, null); await middleware.InvokeAsync(mockHttpContext.Object); mockRequestDelegate.Verify(requestDelegate => requestDelegate.Invoke(mockHttpContext.Object), Times.Once); // If an exception was thrown, then this test will fail with null references as the required objects were not setup. // We could setup additional mocks to verify the methods weren't called. // However, due to complications with extension methods, as detailed in the throws exception case below, // I am fine with leaving this scenario simple with the null reference exceptions. }
public async Task InvokeAsync_CallLogExceptionAsync() { Exception loggedException = null; ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware() { Next = new FaultyMiddleware(), LogExceptionAsync = (Exception e) => { loggedException = e; return(Task.CompletedTask); } }; var context = this.GetContext(); await middleware.InvokeAsync(context); var typedLoggedException = loggedException.ShouldBeOfType <ApplicationException>(); typedLoggedException.Message.ShouldBe("oh no!"); }
public async Task InvokeAsync_NoExceptionHandler() { ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware() { Next = new FaultyMiddleware() }; var context = this.GetContext(); await middleware.InvokeAsync(context); context.Response.StatusCode.ShouldBe(500); context.Response.Body.Position = 0; var contents = context.Response.Body.ReadAsString(); var response = JsonConvert.DeserializeObject <ErrorResponse>(contents); response.ShouldNotBeNull(); response.CorrelationId.ShouldNotBeNullOrWhiteSpace(); response.Error.ShouldNotBeNull(); response.Error.Code.ShouldBe("INTERNAL_SERVER_ERROR"); response.Error.Message.ShouldBe("An internal server error occurred"); }
public async Task InvokeAsync_DefaultExceptionHandler_BadRequest() { ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware() { Next = new BadRequestMiddleware(), ExceptionHandler = ExceptionHandlerMiddleware.DefaultExceptionHandler }; var context = this.GetContext(); await middleware.InvokeAsync(context); context.Response.StatusCode.ShouldBe(400); context.Response.Body.Position = 0; var contents = context.Response.Body.ReadAsString(); var response = JsonConvert.DeserializeObject <ErrorResponse>(contents); response.ShouldNotBeNull(); response.CorrelationId.ShouldNotBeNullOrWhiteSpace(); response.Error.ShouldNotBeNull(); response.Error.Code.ShouldBe("BAD_REQUEST"); response.Error.Message.ShouldBe("oh no!"); }
public async Task InvokeAsync_ThrowsExceptionWhenNextIsNotSet() { ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware(); var context = this.GetContext(); var exception = await ShouldThrowAsyncExtensions.ShouldThrowAsync <MiddlewarePipelineException>(() => middleware.InvokeAsync(context)); exception.Message.ShouldContain("must have a Next middleware"); }