Пример #1
0
    public void ActionMethodExecutor_ExecutesActionsReturningActionResultOfT(bool withFilter)
    {
        // Arrange
        var mapper               = new ActionResultTypeMapper();
        var controller           = new TestController();
        var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsActionResultOfT));
        var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
        var filterContext        = new ControllerEndpointFilterInvocationContext(new Controllers.ControllerActionDescriptor(),
                                                                                 new ActionContext(),
                                                                                 objectMethodExecutor,
                                                                                 mapper,
                                                                                 controller,
                                                                                 Array.Empty <object>());

        // Act
        var valueTask = Execute(actionMethodExecutor, filterContext, withFilter);

        // Assert
        var result = Assert.IsType <ObjectResult>(valueTask.Result);

        Assert.Equal("SyncObjectResultExecutor", actionMethodExecutor.GetType().Name);
        Assert.NotNull(result.Value);
        Assert.IsType <TestModel>(result.Value);
        Assert.Equal(typeof(TestModel), result.DeclaredType);
    }
Пример #2
0
    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.CreateAsyncControllerReleaser(actionDescriptor);
            var propertyBinderFactory = ControllerBinderDelegateProvider.CreateBinderDelegate(
                _parameterBinder,
                _modelBinderFactory,
                _modelMetadataProvider,
                actionDescriptor,
                _mvcOptions);

            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
            var filterExecutor       = actionDescriptor.FilterDelegate is not null
                ? ActionMethodExecutor.GetFilterExecutor(actionDescriptor)
                : null;

            cacheEntry = new ControllerActionInvokerCacheEntry(
                filterFactoryResult.CacheableFilters,
                controllerFactory,
                controllerReleaser,
                propertyBinderFactory,
                objectMethodExecutor,
                filterExecutor ?? actionMethodExecutor,
                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);
    }
Пример #3
0
        public void ActionMethodExecutor_ThrowsIfIConvertFromIActionResult_ReturnsNull()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsCustomConvertibleFromIActionResult));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(
                () => actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>()));

            Assert.Equal($"Cannot return null from an action method with a return type of '{typeof(CustomConvertibleFromAction)}'.", ex.Message);
        }
Пример #4
0
        public void ActionMethodExecutor_ExecutesActionsAsynchronouslyReturningIActionResultAsObject()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnIActionResultAsObjectAsync));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act
            var valueTask = actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>());

            // Assert
            Assert.IsType <OkResult>(valueTask.Result);
        }
Пример #5
0
        public void ActionMethodExecutor_ExecutesActionsReturningSubTypeOfActionResult()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsIActionResultSubType));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act
            var valueTask = actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>());

            // Assert
            Assert.IsType <PartialViewResult>(valueTask.Result);
        }
Пример #6
0
        public void ActionMethodExecutor_ExecutesActionsReturningIActionResult()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnIActionResult));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act
            var valueTask = actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>());

            // Assert
            Assert.True(valueTask.IsCompleted);
            Assert.IsType <ContentResult>(valueTask.Result);
        }
Пример #7
0
        public void ActionMethodExecutor_ExecutesVoidActions()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.VoidAction));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act
            var valueTask = actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>());

            // Assert
            Assert.True(controller.Executed);
            Assert.IsType <EmptyResult>(valueTask.Result);
        }
Пример #8
0
            private static ControllerActionInvokerCacheEntry CreateCacheEntry(
                ControllerActionDescriptor actionDescriptor,
                MockControllerFactory controllerFactory)
            {
                var objectMethodExecutor = CreateExecutor(actionDescriptor);

                return(new ControllerActionInvokerCacheEntry(
                           new FilterItem[0],
                           controllerFactory.CreateController,
                           controllerFactory.ReleaseController,
                           null,
                           objectMethodExecutor,
                           ActionMethodExecutor.GetExecutor(objectMethodExecutor)));
            }
        public void ActionMethodExecutor_ExecutesActionsReturningActionResultAsObject()
        {
            // 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);
        }
Пример #10
0
    public void ActionMethodExecutorExecutesActionsAsynchronouslyReturningIActionResult()
    {
        // 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
        Assert.Equal("TaskOfIActionResultExecutor", actionMethodExecutor.GetType().Name);
        Assert.IsType <StatusCodeResult>(valueTask.Result);
    }
Пример #11
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 <ViewResult>(valueTask.Result);
        }
Пример #12
0
    public void ActionMethodExecutor_ExecutesActionsReturnAwaitable()
    {
        // Arrange
        var mapper               = new ActionResultTypeMapper();
        var controller           = new TestController();
        var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsAwaitable));
        var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

        // Act
        var awaitableResult = actionMethodExecutor.Execute(mapper, objectMethodExecutor, controller, Array.Empty <object>());

        // Assert
        Assert.Equal("AwaitableResultExecutor", actionMethodExecutor.GetType().Name);
        Assert.True(controller.Executed);
        Assert.IsType <EmptyResult>(awaitableResult.Result);
    }
Пример #13
0
        public void ActionMethodExecutor_ExecutesActionsReturningModelAsObject()
        {
            // Arrange
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnModelAsObject));
            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);

            // Act
            var valueTask = actionMethodExecutor.Execute(objectMethodExecutor, controller, Array.Empty <object>());

            // Assert
            var result = Assert.IsType <ObjectResult>(valueTask.Result);

            Assert.NotNull(result.Value);
            Assert.IsType <TestModel>(result.Value);
        }
Пример #14
0
        public void ActionMethodExecutor_ExecutesActionsAsynchronouslyReturningActionResultOfT()
        {
            // Arrange
            var mapper               = new ActionResultTypeMapper();
            var controller           = new TestController();
            var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnActionResultOFTAsync));
            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);
        }
        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);
        }
Пример #16
0
    public async Task ActionMethodExecutor_ThrowsIfIConvertFromIActionResult_ReturnsNull(bool withFilter)
    {
        // Arrange
        var mapper               = new ActionResultTypeMapper();
        var controller           = new TestController();
        var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsCustomConvertibleFromIActionResult));
        var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
        var filterContext        = new ControllerEndpointFilterInvocationContext(new Controllers.ControllerActionDescriptor(),
                                                                                 new ActionContext(),
                                                                                 objectMethodExecutor,
                                                                                 mapper,
                                                                                 controller,
                                                                                 Array.Empty <object>());

        // Act & Assert
        var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => Execute(actionMethodExecutor, filterContext, withFilter).AsTask());

        Assert.Equal($"Cannot return null from an action method with a return type of '{typeof(CustomConvertibleFromAction)}'.", ex.Message);
    }
Пример #17
0
    public void ActionMethodExecutor_ExecutesActionsReturningSubTypeOfActionResult(bool withFilter)
    {
        // Arrange
        var mapper               = new ActionResultTypeMapper();
        var controller           = new TestController();
        var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnsIActionResultSubType));
        var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
        var filterContext        = new ControllerEndpointFilterInvocationContext(new Controllers.ControllerActionDescriptor(),
                                                                                 new ActionContext(),
                                                                                 objectMethodExecutor,
                                                                                 mapper,
                                                                                 controller,
                                                                                 Array.Empty <object>());

        // Act
        var valueTask = Execute(actionMethodExecutor, filterContext, withFilter);

        // Assert
        Assert.Equal("SyncActionResultExecutor", actionMethodExecutor.GetType().Name);
        Assert.IsType <ContentResult>(valueTask.Result);
    }
Пример #18
0
    public async Task ActionMethodExecutor_ExecutesActionsAsynchronouslyReturningActionResultSubType(bool withFilter)
    {
        // Arrange
        var mapper               = new ActionResultTypeMapper();
        var controller           = new TestController();
        var objectMethodExecutor = GetExecutor(nameof(TestController.ReturnActionResultAsync));
        var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
        var filterContext        = new ControllerEndpointFilterInvocationContext(new Controllers.ControllerActionDescriptor(),
                                                                                 new ActionContext(),
                                                                                 objectMethodExecutor,
                                                                                 mapper,
                                                                                 controller,
                                                                                 Array.Empty <object>());

        // Act
        var valueTask = Execute(actionMethodExecutor, filterContext, withFilter);

        // Assert
        await valueTask;

        Assert.Equal("TaskOfActionResultExecutor", actionMethodExecutor.GetType().Name);
        Assert.IsType <ViewResult>(valueTask.Result);
    }