public (ControllerActionInvokerCacheEntry cacheEntry, IFilterMetadata[] filters) GetCachedResult(ControllerContext controllerContext) { var actionDescriptor = controllerContext.ActionDescriptor; IFilterMetadata[] filters; var cacheEntry = actionDescriptor.CacheEntry; // We don't care about thread safety here if (cacheEntry is null) { var filterFactoryResult = FilterFactory.GetAllFilters(_filterProviders, controllerContext); filters = filterFactoryResult.Filters; var parameterDefaultValues = ParameterDefaultValues .GetParameterDefaultValues(actionDescriptor.MethodInfo); var objectMethodExecutor = ObjectMethodExecutor.Create( actionDescriptor.MethodInfo, actionDescriptor.ControllerTypeInfo, parameterDefaultValues); var controllerFactory = _controllerFactoryProvider.CreateControllerFactory(actionDescriptor); var controllerReleaser = _controllerFactoryProvider.CreateControllerReleaser(actionDescriptor); var propertyBinderFactory = ControllerBinderDelegateProvider.CreateBinderDelegate( _parameterBinder, _modelBinderFactory, _modelMetadataProvider, actionDescriptor, _mvcOptions); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); cacheEntry = new ControllerActionInvokerCacheEntry( filterFactoryResult.CacheableFilters, controllerFactory, controllerReleaser, propertyBinderFactory, objectMethodExecutor, actionMethodExecutor); actionDescriptor.CacheEntry = cacheEntry; } else { // Filter instances from statically defined filter descriptors + from filter providers filters = FilterFactory.CreateUncachedFilters(_filterProviders, controllerContext, cacheEntry.CachedFilters); } return(cacheEntry, filters); }
public void ActionMethodExecutor_ExecutesActionsReturningSubTypeOfActionResult() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsIActionResultSubType)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act var valueTask = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>()); // Assert Assert.IsType <ContentResult>(valueTask.Result); }
public void ActionMethodExecutor_ThrowsIfIConvertFromIActionResult_ReturnsNull() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsCustomConvertibleFromIActionResult)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act & Assert var ex = Assert.Throws <InvalidOperationException>( () => actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>())); Assert.Equal($"Cannot return null from an action method with a return type of '{typeof(CustomConvertibleFromAction)}'.", ex.Message); }
public void ActionMethodExecutor_ExecutesActionsAsynchronouslyReturningIActionResultAsObject() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnIActionResultAsObjectAsync)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act var valueTask = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>()); // Assert Assert.IsType <OkResult>(valueTask.Result); }
public void ActionMethodExecutor_ExecutesVoidActions() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.VoidAction)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act var valueTask = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>()); // Assert Assert.True(controller.Executed); Assert.IsType <EmptyResult>(valueTask.Result); }
public async Task ActionMethodExecutor_ExecutesActionsAsynchronouslyReturningActionResultSubType() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnIActionResultAsync)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act var valueTask = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>()); // Assert await valueTask; Assert.IsType <StatusCodeResult>(valueTask.Result); }
public void ActionMethodExecutor_ExecutesActionsReturningModelAsModel() { // Arrange var mapper = new ActionResultTypeMapper(); var controller = new TestController(); var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsModelAsModel)); var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor); // Act var valueTask = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>()); // Assert var result = Assert.IsType <ObjectResult>(valueTask.Result); Assert.NotNull(result.Value); Assert.IsType <TestModel>(result.Value); Assert.Equal(typeof(TestModel), result.DeclaredType); }