Beispiel #1
0
        public void MapMvcAttributeRoutes_WithPrefixedArea()
        {
            // Arrange
            var controllerTypes = new[] { typeof(PrefixedPugetSoundController) };
            var routes          = new RouteCollection();

            // Act
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            // Assert
            var attributeRoutes = GetAttributeRoutes(routes);

            Assert.Equal(1, attributeRoutes.Count);

            Route route = (Route)attributeRoutes.Single();

            Assert.Equal("puget-sound/prefpref/getme", route.Url);
            Assert.Equal("PugetSound", route.DataTokens["area"]);
            Assert.Equal((object)false, route.DataTokens["usenamespacefallback"]);
            Assert.Equal("GetMe", Assert.Single(route.GetTargetActionDescriptors()).ActionName);
            Assert.Equal(
                typeof(PrefixedPugetSoundController).Namespace,
                ((string[])route.DataTokens["namespaces"])[0]
                );
        }
Beispiel #2
0
        public void AttributeRouting_OptionalParametersGetRemoved()
        {
            // Arrange
            var controllerTypes = new[] { typeof(OptionalParameterController) };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext("~/Create");
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal("Create()", result.Content);

            // The request context should be updated to to contain the routedata of the direct route
            Assert.Equal("{action}/{id}", ((Route)requestContext.RouteData.Route).Url);
            Assert.Null(requestContext.RouteData.Values["id"]);
        }
Beispiel #3
0
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="constraintResolver">
        /// The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.
        /// </param>
        /// <param name="directRouteProvider">
        /// The <see cref="IDirectRouteProvider"/> to use for discovering and building routes.
        /// </param>
        // Corresponds to the MVC implementation of attribute routing in
        // System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
        public static void MapHttpAttributeRoutes(
            this HttpConfiguration configuration,
            IInlineConstraintResolver constraintResolver,
            IDirectRouteProvider directRouteProvider
            )
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            if (constraintResolver == null)
            {
                throw new ArgumentNullException("constraintResolver");
            }

            if (directRouteProvider == null)
            {
                throw new ArgumentNullException("directRouteProvider");
            }

            AttributeRoutingMapper.MapAttributeRoutes(
                configuration,
                constraintResolver,
                directRouteProvider
                );
        }
        private RouteCollection MapControllers(Type[] types)
        {
            RouteCollection routes = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, types);
            return(routes);
        }
Beispiel #5
0
        public void MapMvcAttributeRoutes_MapRouteAttributes()
        {
            // Arrange
            var controllerTypes = new[] { typeof(SimpleRoutingController) };
            var routes          = new RouteCollection();

            // Act
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            // Assert
            var expectedResults = new List <Tuple <string, string> >
            {
                new Tuple <string, string>("getme", "GetMe"),
                new Tuple <string, string>("postme", "PostMe"),
                new Tuple <string, string>("getorpostme", "GetOrPostMe"),
                new Tuple <string, string>("routeme", "RouteMe"),
                new Tuple <string, string>("once", "FoolMe"),
                new Tuple <string, string>("twice", "FoolMe"),
                new Tuple <string, string>("twice", "FoolMe"),
            };

            foreach (var expected in expectedResults)
            {
                var url        = expected.Item1;
                var methodName = expected.Item2;

                var   attributeRoutes = GetAttributeRoutes(routes);
                Route route           = attributeRoutes.Cast <Route>().Single(r => r.Url == url);
                Assert.Equal(methodName, Assert.Single(route.GetTargetActionDescriptors()).ActionName);
            }
        }
Beispiel #6
0
        public void AttributeRouting_WithActionNameSelectors(Type controllerType, string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            if (expectedAction == null)
            {
                // Act & Assert
                Assert.Throws <HttpException>(() => handler.ProcessRequest(context));
            }
            else
            {
                // Act
                handler.ProcessRequest(context);

                // Assert
                ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);
                Assert.Equal(expectedAction, result.Content);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="constraintResolver">
        /// The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints in route templates.
        /// </param>
        /// <param name="directRouteProvider">
        /// The <see cref="IDirectRouteProvider"/> to use for mapping routes.
        /// </param>
        public static void MapMvcAttributeRoutes(
            this RouteCollection routes,
            IInlineConstraintResolver constraintResolver,
            IDirectRouteProvider directRouteProvider
            )
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }

            if (constraintResolver == null)
            {
                throw new ArgumentNullException("constraintResolver");
            }

            if (directRouteProvider == null)
            {
                throw new ArgumentNullException("directRouteProvider");
            }

            AttributeRoutingMapper.MapAttributeRoutes(
                routes,
                constraintResolver,
                directRouteProvider
                );
        }
Beispiel #8
0
        public void AttributeRouting_MixedWithGeneralRouting(Type controllerType, string path, string expectedAction)
        {
            // Arrange
            var    controllerTypes = new[] { controllerType };
            var    routes          = new RouteCollection();
            object defaults        = new { controller = controllerType.Name.Substring(0, controllerType.Name.Length - 10) };

            routes.Add(new Route("standard/{action}", new RouteValueDictionary(defaults), null));
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            if (expectedAction == null)
            {
                // Act & Assert
                Assert.Throws <HttpException>(() => handler.ProcessRequest(context));
            }
            else
            {
                // Act
                handler.ProcessRequest(context);

                // Assert
                ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);
                Assert.Equal(expectedAction, result.Content);
            }
        }
        public void AttributeRouting_MethodOverloads_WithDifferentActionNames(
            Type controllerType,
            string path,
            string expectedAction
            )
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
Beispiel #10
0
        public void AttributeRouting_WithCustomizedRoutePrefixAttribute(string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[]
            {
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS1.HomeController),
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS2.AccountController),
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS3.OtherController),
            };

            var routes = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="routes"></param>
        public static void MapMvcAttributeRoutes(this RouteCollection routes)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }

            AttributeRoutingMapper.MapAttributeRoutes(routes, new DefaultInlineConstraintResolver());
        }
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        // Corresponds to the MVC implementation of attribute routing in
        // System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
        public static void MapHttpAttributeRoutes(this HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            AttributeRoutingMapper.MapAttributeRoutes(configuration, new DefaultInlineConstraintResolver(), new DefaultDirectRouteProvider());
        }
Beispiel #13
0
        public void AttributeRouting_WithNullPrefix_ThrowsInvalidOperationException()
        {
            // Arrange
            var controllerTypes = new[]
            {
                typeof(ControllersWithCustomizedRoutePrefixAttribute.Invalid.AccountController)
            };

            var routes = new RouteCollection();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes),
                                                      "The property 'prefix' from route prefix attribute on controller of type 'System.Web.Routing.ControllersWithCustomizedRoutePrefixAttribute.Invalid.AccountController' cannot be null.");
        }
Beispiel #14
0
        public void AttributeRouting_WithMultipleCustomizedRoutePrefixAttribute_ThrowsInvalidOperationException()
        {
            // Arrange
            var controllerTypes = new[]
            {
                typeof(ControllersWithCustomizedRoutePrefixAttribute.Invalid.HomeController)
            };

            var routes = new RouteCollection();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes),
                                                      "Only one route prefix attribute is supported. Remove extra attributes from the controller of type 'System.Web.Routing.ControllersWithCustomizedRoutePrefixAttribute.Invalid.HomeController'.");
        }
Beispiel #15
0
        public void AttributeRouting_WithInheritance_InvalidPaths(Type derivedController, string path)
        {
            // Arrange
            var controllerTypes = new[] { derivedController, derivedController.BaseType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);
            HttpContextBase context = GetContext(path);

            // Act
            RouteData routeData = routes.GetRouteData(context);

            // Assert
            Assert.Null(routeData);
        }
Beispiel #16
0
        public void TemplatesAreValidated(Type controllerType, string expectedErrorMessage)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            // Act & Assert
            if (expectedErrorMessage == null)
            {
                Assert.DoesNotThrow(() => AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes));
            }
            else
            {
                Assert.Throws <InvalidOperationException>(() => AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes), expectedErrorMessage);
            }
        }
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="controllerTypes">The controller types to scan.</param>
        public static void MapMvcAttributeRoutes(
            this RouteCollection routes,
            IEnumerable <Type> controllerTypes)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }

            if (controllerTypes == null)
            {
                throw new ArgumentNullException("controllerTypes");
            }

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);
        }
Beispiel #18
0
        public void AttributeRouting_AmbiguousActions_ThrowsAmbiguousException(Type controllerType, string path)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            Assert.Throws <AmbiguousMatchException>(() => handler.ProcessRequest(context));
        }
Beispiel #19
0
        public void MapMvcAttributeRoutes_CustomConstraintResolver()
        {
            // Arrange
            var controllerTypes = new[] { typeof(FruitConstraintController) };
            var routes          = new RouteCollection();

            // Act
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes, new FruitConstraintResolver());

            // Assert
            var attributeRoutes = GetAttributeRoutes(routes);

            Assert.Equal(1, attributeRoutes.Count);
            Route route = (Route)attributeRoutes.Single();

            Assert.Equal("fruits/{apple}", route.Url);
            Assert.IsAssignableFrom <FruitConstraint>(route.Constraints["apple"]);
        }
        public void ActionHelper_ChildAction_WithActionDirectRoute()
        {
            // Arrange
            AttributeRoutingMapper.MapAttributeRoutes(_routes, new Type[] { typeof(DirectRouteActionController) });

            MvcHandler mvcHandler = null;

            _httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(_htmlHelper.Object, "Action", null /* controllerName */, null /* routeValues */, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.Equal("Action", routeData.Values["action"]);
        }
Beispiel #21
0
        public void MapMvcAttributeRoutes_WithPrefixedController()
        {
            // Arrange
            var controllerTypes = new[] { typeof(PrefixedController) };
            var routes          = new RouteCollection();

            // Act
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            // Assert
            var attributeRoutes = GetAttributeRoutes(routes);

            Assert.Equal(1, attributeRoutes.Count);

            Route route = (Route)attributeRoutes.Single();

            Assert.Equal("prefpref/getme", route.Url);
            Assert.Equal("GetMe", Assert.Single(route.GetTargetActionDescriptors()).ActionName);
        }
Beispiel #22
0
        public void MapMvcAttributeRoutes_WithInlineConstraints_ParseConstraintsDefaultsAndOptionals()
        {
            // Arrange
            var controllerTypes = new[] { typeof(SimpleRoutingController) };
            var routes          = new RouteCollection();

            // Act
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            // Assert
            var   attributeRoutes = GetAttributeRoutes(routes);
            Route route           = attributeRoutes.Cast <Route>().Single(r => r.GetTargetActionDescriptors().Single().ActionName == "Parameterized");

            Assert.NotNull(route);

            Assert.Equal("i/{have}/{id}/{defaultsto}/{name}", route.Url);
            Assert.IsAssignableFrom <IntRouteConstraint>(route.Constraints["id"]);
            Assert.Equal("VAL", route.Defaults["defaultsto"]);
            Assert.Equal("", route.Defaults["name"].ToString());
        }