Example #1
0
        protected internal virtual void ProcessRequest(HttpContextBase httpContext)
        {
            AddVersionHeader(httpContext);

            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");

            // Instantiate the controller and call Execute
            IControllerFactory factory    = ControllerBuilder.GetControllerFactory();
            IController        controller = factory.CreateController(RequestContext, controllerName);

            if (controller == null)
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentUICulture,
                              MvcResources.ControllerBuilder_FactoryReturnedNull,
                              factory.GetType(),
                              controllerName));
            }
            try {
                controller.Execute(RequestContext);
            }
            finally {
                factory.ReleaseController(controller);
            }
        }
        private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
        {
            // If request validation has already been enabled, make it lazy. This allows attributes like [HttpPost] (which looks
            // at Request.Form) to work correctly without triggering full validation.
            // Tolerate null HttpContext for testing.
            HttpContext currentContext = HttpContext.Current;

            if (currentContext != null)
            {
                bool?isRequestValidationEnabled = ValidationUtility.IsValidationEnabled(currentContext);
                if (isRequestValidationEnabled == true)
                {
                    ValidationUtility.EnableDynamicValidation(currentContext);
                }
            }

            AddVersionHeader(httpContext);
            RemoveOptionalRoutingParameters();

            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");

            // Instantiate the controller and call Execute
            factory    = ControllerBuilder.GetControllerFactory();
            controller = factory.CreateController(RequestContext, controllerName);
            if (controller == null)
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              MvcResources.ControllerBuilder_FactoryReturnedNull,
                              factory.GetType(),
                              controllerName));
            }
        }
        public void Should_be_able_to_register_controller_factory()
        {
            var builder = new ControllerBuilder();

            new RegisterControllerFactory(adapter.Object, builder).Execute();

            Assert.Same(controllerFactory.Object, builder.GetControllerFactory());
        }
Example #4
0
        public void ControllerBuilderReturnsDefaultControllerBuilderByDefault() {
            // Arrange
            ControllerBuilder cb = new ControllerBuilder();

            // Act
            IControllerFactory cf = cb.GetControllerFactory();

            // Assert
            Assert.IsInstanceOfType(cf, typeof(DefaultControllerFactory));
        }
        public void Should_not_register_controller_factory_when_excluded()
        {
            RegisterControllerFactory.Excluded = true;
            DependencyResolver.SetResolver(null);

            var builder = new ControllerBuilder();

            new RegisterControllerFactory(adapter.Object, builder).Execute();

            Assert.NotSame(controllerFactory.Object, builder.GetControllerFactory());
        }
Example #6
0
        private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
        {
            AddVersionHeader(httpContext);
            RemoveOptionalRoutingParameters();

            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");

            // Instantiate the controller and call Execute
            factory    = ControllerBuilder.GetControllerFactory();
            controller = factory.CreateController(RequestContext, controllerName);
            if (controller == null)
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentUICulture,
                              MvcResources.ControllerBuilder_FactoryReturnedNull,
                              factory.GetType(),
                              controllerName));
            }
        }
Example #7
0
        public void CreateControllerWithFactoryTypeReturnsValidType() {
            // Arrange
            ControllerBuilder cb = new ControllerBuilder();
            cb.SetControllerFactory(typeof(MockControllerFactory));

            // Act
            IControllerFactory cf = cb.GetControllerFactory();

            // Assert
            Assert.IsInstanceOfType(cf, typeof(MockControllerFactory));
        }
Example #8
0
        public void CreateControllerWithFactoryInstanceReturnsInstance() {
            // Arrange
            ControllerBuilder cb = new ControllerBuilder();
            DefaultControllerFactory factory = new DefaultControllerFactory();
            cb.SetControllerFactory(factory);

            // Act
            IControllerFactory cf = cb.GetControllerFactory();

            // Assert
            Assert.AreSame(factory, cf);
        }
        public void ControllerBuilderGetControllerFactoryDelegatesToResolver() {
            //Arrange
            Mock<IControllerFactory> factory = new Mock<IControllerFactory>();
            Resolver<IControllerFactory> resolver = new Resolver<IControllerFactory> { Current = factory.Object };
            ControllerBuilder builder = new ControllerBuilder(resolver);

            //Act
            IControllerFactory result = builder.GetControllerFactory();

            //Assert
            Assert.AreSame(factory.Object, result);

        }
        public void SettingControllerFactoryReturnsSetFactory() {
            // Arrange
            ControllerBuilder builder = new ControllerBuilder();
            Mock<IControllerFactory> setFactory = new Mock<IControllerFactory>();

            // Act
            builder.SetControllerFactory(setFactory.Object);

            // Assert
            Assert.AreSame(setFactory.Object, builder.GetControllerFactory());
        }
        public void DefaultControllerFactoryIsDefaultControllerFactory() {
            // Arrange
            ControllerBuilder builder = new ControllerBuilder();

            // Act
            IControllerFactory returnedControllerFactory = builder.GetControllerFactory();

            //Assert
            Assert.AreEqual(typeof(DefaultControllerFactory), returnedControllerFactory.GetType());
        }