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);
        }
Example #4
0
 internal ControllerActionInvokerCacheEntry(
     FilterItem[] cachedFilters,
     Func <ControllerContext, object> controllerFactory,
     Action <ControllerContext, object> controllerReleaser,
     ControllerBinderDelegate controllerBinderDelegate,
     ObjectMethodExecutor objectMethodExecutor,
     ActionMethodExecutor actionMethodExecutor)
 {
     ControllerFactory        = controllerFactory;
     ControllerReleaser       = controllerReleaser;
     ControllerBinderDelegate = controllerBinderDelegate;
     CachedFilters            = cachedFilters;
     ObjectMethodExecutor     = objectMethodExecutor;
     ActionMethodExecutor     = actionMethodExecutor;
 }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        public (ControllerActionInvokerCacheEntry cacheEntry, IFilterMetadata[] filters) GetCachedResult(ControllerContext controllerContext)
        {
            var cache            = CurrentCache;
            var actionDescriptor = controllerContext.ActionDescriptor;

            IFilterMetadata[] filters;
            if (!cache.Entries.TryGetValue(actionDescriptor, out var cacheEntry))
            {
                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);
                cacheEntry = cache.Entries.GetOrAdd(actionDescriptor, cacheEntry);
            }
            else
            {
                // Filter instances from statically defined filter descriptors + from filter providers
                filters = FilterFactory.CreateUncachedFilters(_filterProviders, controllerContext, cacheEntry.CachedFilters);
            }

            return(cacheEntry, filters);
        }
Example #8
0
        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);
        }