示例#1
0
        public void MapHttpAttributeRoutes_RespectsRouteOrder()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            var routePrefixes        = new Collection <RoutePrefixAttribute>()
            {
            };
            var routeProviders = new Collection <IHttpRouteInfoProvider>()
            {
                new HttpGetAttribute("get1")
                {
                    RouteOrder = 1
                },
                new HttpGetAttribute("get2"),
                new HttpGetAttribute("get3")
                {
                    RouteOrder = -1
                }
            };

            SetUpConfiguration(config, routePrefixes, routeProviders);

            // Act
            config.MapHttpAttributeRoutes();

            // Assert
            HttpRouteCollection routes = config.Routes;

            Assert.Equal(3, routes.Count);
            Assert.Equal("get3", routes.ElementAt(0).RouteTemplate);
            Assert.Equal("get2", routes.ElementAt(1).RouteTemplate);
            Assert.Equal("get1", routes.ElementAt(2).RouteTemplate);
        }
示例#2
0
        public void MapHttpAttributeRoutes_RespectsRouteOrderAcrossControllers()
        {
            // Arrange
            var config = new HttpConfiguration();
            HttpControllerDescriptor controllerDescriptor1 = CreateControllerDescriptor(config, "Controller1", new Collection <RoutePrefixAttribute>());
            HttpActionDescriptor     actionDescriptor1     = CreateActionDescriptor(
                "Action1",
                new Collection <IHttpRouteInfoProvider>()
            {
                new HttpGetAttribute("action1/route1")
                {
                    RouteOrder = 3
                }, new HttpGetAttribute("action1/route2")
                {
                    RouteOrder = 1
                }
            },
                controllerDescriptor1);
            HttpControllerDescriptor controllerDescriptor2 = CreateControllerDescriptor(config, "Controller2", new Collection <RoutePrefixAttribute>());
            HttpActionDescriptor     actionDescriptor2     = CreateActionDescriptor(
                "Action2",
                new Collection <IHttpRouteInfoProvider>()
            {
                new HttpGetAttribute("action2/route1")
                {
                    RouteOrder = 2
                }
            },
                controllerDescriptor2);

            var controllerSelector = CreateControllerSelector(new[] { controllerDescriptor1, controllerDescriptor2 });

            config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
            var actionSelector = CreateActionSelector(
                new Dictionary <HttpControllerDescriptor, IEnumerable <HttpActionDescriptor> >()
            {
                { controllerDescriptor1, new HttpActionDescriptor[] { actionDescriptor1 } },
                { controllerDescriptor2, new HttpActionDescriptor[] { actionDescriptor2 } }
            });

            config.Services.Replace(typeof(IHttpActionSelector), actionSelector);

            // Act
            config.MapHttpAttributeRoutes();

            // Assert
            HttpRouteCollection routes = config.Routes;

            Assert.Equal(3, routes.Count);
            Assert.Equal("action1/route2", routes.ElementAt(0).RouteTemplate);
            Assert.Equal("action2/route1", routes.ElementAt(1).RouteTemplate);
            Assert.Equal("action1/route1", routes.ElementAt(2).RouteTemplate);
        }
        public void MapHttpAttributeRoutes_AddsLinkGenerationRoutes()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            var routePrefixes        = new Collection <IRoutePrefix>()
            {
            };
            var routeProviders = new Collection <RouteAttribute>()
            {
                new RouteAttribute("get1")
                {
                    Name = "one"
                },
                new RouteAttribute("get2")
                {
                    Name = "two"
                },
                new RouteAttribute("get3")
                {
                    Name = "three"
                }
            };

            SetUpConfiguration(config, routePrefixes, routeProviders);

            // Act
            config.MapHttpAttributeRoutes();
            config.Initializer(config);

            // Assert
            HttpRouteCollection routes = config.Routes;

            Assert.Equal(4, routes.Count); // 1 attr route, plus 3 generation routes
            Assert.IsType <RouteCollectionRoute>(routes.ElementAt(0));
            for (int i = 1; i < 4; i++)
            {
                Assert.IsType <LinkGenerationRoute>(routes.ElementAt(i));
            }

            Assert.IsType <LinkGenerationRoute>(routes["one"]);
            Assert.IsType <LinkGenerationRoute>(routes["two"]);
            Assert.IsType <LinkGenerationRoute>(routes["three"]);
        }