Пример #1
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);
            }
        }
Пример #2
0
        public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithHandler_Throws()
        {
            // Arrange
            string areaPrefix       = null;
            string controllerPrefix = null;
            ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor("IgnoreController");
            ActionDescriptor     actionDescriptor     = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction");
            Route route = new Route(url: null, routeHandler: null);

            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[] { actionDescriptor });
            route.RouteHandler = new Mock <IRouteHandler>(MockBehavior.Strict).Object;
            ActionDescriptor[]  originalActions            = route.GetTargetActionDescriptors();
            RouteEntry          entry                      = new RouteEntry(name: null, route: route);
            IDirectRouteFactory factory                    = CreateStubRouteFactory(entry);
            IReadOnlyCollection <ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor };
            IInlineConstraintResolver constraintResolver   =
                new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object;

            // Act & Assert
            string expectedMessage = "Direct routing does not support per-route route handlers.";

            Assert.Throws <InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix,
                                                                                                        controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage);
        }
Пример #3
0
        public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithEmptyActionDescriptors_Throws()
        {
            // Arrange
            string areaPrefix       = null;
            string controllerPrefix = null;
            Route  route            = new Route(url: null, routeHandler: null);

            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[0]);
            ActionDescriptor[] originalActions = route.GetTargetActionDescriptors();
            Assert.NotNull(originalActions); // Guard
            Assert.Empty(originalActions);   // Guard
            RouteEntry           entry   = new RouteEntry(name: null, route: route);
            IDirectRouteFactory  factory = CreateStubRouteFactory(entry);
            ControllerDescriptor controllerDescriptor      = CreateStubControllerDescriptor("IgnoreController");
            ActionDescriptor     actionDescriptor          = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction");
            IReadOnlyCollection <ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor };
            IInlineConstraintResolver constraintResolver   =
                new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object;

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                                     "that each direct route map to a non-empty set of actions.";

            Assert.Throws <InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix,
                                                                                                        controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage);
        }
        public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithEmptyActionDescriptors_Throws()
        {
            // Arrange
            string areaPrefix = null;
            string controllerPrefix = null;
            Route route = new Route(url: null, routeHandler: null);
            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[0]);
            ActionDescriptor[] originalActions = route.GetTargetActionDescriptors();
            Assert.NotNull(originalActions); // Guard
            Assert.Equal(0, originalActions.Length); // Guard
            RouteEntry entry = new RouteEntry(name: null, route: route);
            IDirectRouteFactory factory = CreateStubRouteFactory(entry);
            ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor("IgnoreController");
            ActionDescriptor actionDescriptor = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction");
            IReadOnlyCollection<ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor };
            IInlineConstraintResolver constraintResolver =
                new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                "that each direct route map to a non-empty set of actions.";
            Assert.Throws<InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix,
                controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage);
        }
Пример #5
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]
                );
        }
Пример #6
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);
        }
        public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithHandler_Throws()
        {
            // Arrange
            string areaPrefix = null;
            string controllerPrefix = null;
            ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor("IgnoreController");
            ActionDescriptor actionDescriptor = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction");
            Route route = new Route(url: null, routeHandler: null);
            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[] { actionDescriptor });
            route.RouteHandler = new Mock<IRouteHandler>(MockBehavior.Strict).Object;
            ActionDescriptor[] originalActions = route.GetTargetActionDescriptors();
            RouteEntry entry = new RouteEntry(name: null, route: route);
            IDirectRouteFactory factory = CreateStubRouteFactory(entry);
            IReadOnlyCollection<ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor };
            IInlineConstraintResolver constraintResolver =
                new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            // Act & Assert
            string expectedMessage = "Direct routing does not support per-route route handlers.";
            Assert.Throws<InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix,
                controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage);
        }