when_ArgumentOutOfRangeException_raised_but_config_is_set_to_argument_it_should_raise_exception_again() { const string fieldName = "ArgumentOutOfRange"; const string error = "NameValidationException"; var badRequestController = new BadRequestController(); var modelState = new ModelStateDictionary(); modelState.AddModelError(fieldName, error); var result = badRequestController.GetBadRequestWithModelState(modelState); var config = new WebApiErrorHandlingConfig(); config.CatchArgumentException(); var middleware = TestHelper.BuildWebApiErrorHandlingMiddleware(innerHttpContext => throw new ArgumentOutOfRangeException( fieldName, error), config); var context = new DefaultHttpContext(); context.Response.Body = new MemoryStream(); Func <Task> act = async() => { await middleware.Invoke(context); }; await act.Should().ThrowAsync <ArgumentOutOfRangeException>(); }
public static IApplicationBuilder UseWebApiErrorHandling(this IApplicationBuilder builder, ILogger logger) { var config = new WebApiErrorHandlingConfig(); return(builder.UseMiddleware <WebApiErrorHandlingMiddleware>(logger, config)); }
public static WebApiErrorHandlingMiddleware BuildWebApiErrorHandlingMiddleware( RequestDelegate next, WebApiErrorHandlingConfig config) { return(new WebApiErrorHandlingMiddleware(next, new FakeLogger(), config)); }
public async Task when_ArgumentNullException_raised_it_should_BadRequest_http_status_code() { const string fieldName = "ArgumentNull"; const string error = "NameValidationException"; var badRequestController = new BadRequestController(); var modelState = new ModelStateDictionary(); modelState.AddModelError(fieldName, error); var result = badRequestController.GetBadRequestWithModelState(modelState); var config = new WebApiErrorHandlingConfig(); config.CatchArgumentNullException(); var middleware = TestHelper.BuildWebApiErrorHandlingMiddleware(innerHttpContext => throw new ArgumentNullException(fieldName, error), config); var context = new DefaultHttpContext(); context.Response.Body = new MemoryStream(); await middleware.Invoke(context); context.Response.Body.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(context.Response.Body); var streamText = reader.ReadToEnd(); var expected = JToken.Parse(result.SerializeToJson()); var actual = JToken.Parse(streamText); Assert.Equal(HttpStatusCode.BadRequest, context.Response.StatusCode.AsEnum <HttpStatusCode>()); Assert.True(JToken.DeepEquals(actual, expected)); }
public WebApiErrorHandlingMiddleware(RequestDelegate next, ILogger logger, WebApiErrorHandlingConfig config) { _next = next; _logger = logger; _config = config; }