public async Task Should_Filter_Exception(Exception exception, string exceptionMessage, int httpStatusCode) { var exceptionContext = GetExceptionContext(exception); var stringLocalizer = GetMockStringLocalizer <ApiResponseMessage>(ApiResponseMessage.Exception, ApiResponseMessage.Exception); var apiExceptionFilter = new ApiExceptionFilterAttribute(stringLocalizer.Object); await apiExceptionFilter.OnExceptionAsync(exceptionContext); var result = (exceptionContext.Result as JsonResult).Value as ApiResponse <object>; Assert.Equal(httpStatusCode, result.StatusCode); Assert.Equal(exceptionMessage, result.ResponseException.ErrorMessage); }
public void Apply(ActionModel action) { var isApiController = action.Controller.Attributes.OfType <ApiControllerAttribute>().Any(); if ((isApiController && _options.ApplyToApiControllerActions)) { var apiExceptionFilterAttribute = new ApiExceptionFilterAttribute(_options.ApiExceptionOptions); action.Filters.Add(apiExceptionFilterAttribute); } else if ((!isApiController && _options.ApplyToMvcActions)) { var apiExceptionFilterAttribute = new ApiExceptionFilterAttribute(_options.ApiExceptionOptions); action.Filters.Add(apiExceptionFilterAttribute); } }
public void Non_ApiException_Type_Exceptions_Should_Not_Set_Response() { // Arrange var apiExceptionFilter = new ApiExceptionFilterAttribute(); var context = new HttpActionExecutedContext { Exception = new Exception() }; // Act apiExceptionFilter.OnException(context); // Assert context.Response.Should().BeNull(); }
public void Unauthorized_ApiException_Should_Set_Correct_Unauthorized_Response() { // Arrange var apiExceptionFilter = new ApiExceptionFilterAttribute(); var context = GetMockActionContext(new UnauthorizedException("Not Authorized")); // Act apiExceptionFilter.OnException(context); // Assert var response = context.Response; response.Should().NotBeNull(); response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); var errorModelList = JsonConvert.DeserializeObject <List <ResponseErrorModel> >(response.Content.ReadAsStringAsync().Result); errorModelList.Should().HaveCount(1); errorModelList.First().ErrorDescription.Should().Be("Not Authorized"); }
public void DefaultMetaAddedToExceptions() { var logger = new Mock <ILogger <ApiExceptionFilterAttribute> >(); var defaultMetaDataRetriever = new Mock <IDefaultMetaDataRetriever>(); defaultMetaDataRetriever.Setup(r => r.GetDefaultMetaData()).Returns(new MetaCollection { { "mock", "meta" } }); var target = new ApiExceptionFilterAttribute(logger.Object, defaultMetaDataRetriever.Object); var testException = new NotImplementedException(); var context = GetContext(testException); target.OnException(context); var response = (context.Result as ObjectResult)?.Value as IApiResponse; Assert.Contains(response.Meta, m => m.Key == "mock"); }
public async Task OnAsyncExceptionTests() { Logger.Init("", "CoreAccessControl.log", "CoreAccessControl", Severity.Information, mock: true); var actionContext = new ActionContext() { HttpContext = new DefaultHttpContext(), RouteData = new RouteData(), ActionDescriptor = new ActionDescriptor() }; // The stacktrace message and source member variables are virtual and so we can stub them here. var mockException = new Mock <Exception>(); mockException.Setup(e => e.StackTrace) .Returns("Test stacktrace"); mockException.Setup(e => e.Message) .Returns("Test message"); mockException.Setup(e => e.Source) .Returns("Test source"); // the List<FilterMetadata> here doesn't have much relevance in the test but is required // for instantiation. So we instantiate a new instance of it with no members to ensure // it does not effect the test. var exceptionContext = new ExceptionContext(actionContext, new List <IFilterMetadata>()) { Exception = mockException.Object }; var filter = new ApiExceptionFilterAttribute(); await filter.OnExceptionAsync(exceptionContext); // Assumption here that your exception filter modifies status codes. // Just used as an example of how you can assert in this test. Assert.Equal(500, (int)exceptionContext.HttpContext.Response.StatusCode); }