Example #1
0
        public void TreeRouter_CreateTreeRouter_ThrowsIfDifferentEntriesHaveTheSameName(
            IEnumerable<TreeRouteLinkGenerationEntry> namedEntries)
        {
            // Arrange
            string expectedExceptionMessage = "Two or more routes named 'NamedEntry' have different templates." +
                Environment.NewLine +
                "Parameter name: linkGenerationEntries";

            var next = new Mock<IRouter>().Object;

            // Act
            var builder = new TreeRouteBuilder(next, NullLoggerFactory.Instance);
            var exception = Assert.Throws<ArgumentException>(
                "linkGenerationEntries",
                () =>
                {
                    foreach (var entry in namedEntries)
                    {
                        builder.Add(entry);
                    }

                    return builder.Build(version: 1);
                });

            Assert.Equal(expectedExceptionMessage, exception.Message, StringComparer.OrdinalIgnoreCase);
        }
Example #2
0
        private static TreeRouter CreateAttributeRoute(
            IRouter next,
            IEnumerable<TreeRouteMatchingEntry> matchingEntries,
            IEnumerable<TreeRouteLinkGenerationEntry> generationEntries)
        {
            var builder = new TreeRouteBuilder(next, NullLoggerFactory.Instance);

            foreach (var entry in matchingEntries)
            {
                builder.Add(entry);
            }

            foreach (var entry in generationEntries)
            {
                builder.Add(entry);
            }

            return builder.Build(version: 1);
        }
Example #3
0
        private static TreeRouter CreateRoutingAttributeRoute(
            ILoggerFactory loggerFactory = null,
            params TreeRouteMatchingEntry[] entries)
        {
            loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;

            var builder = new TreeRouteBuilder(new StubRouter(), loggerFactory);

            foreach (var entry in entries)
            {
                builder.Add(entry);
            }

            return builder.Build(version: 1);
        }
Example #4
0
        private TreeRouter BuildRoute(ActionDescriptorCollection actions)
        {
            var routeBuilder = new TreeRouteBuilder(_target, _loggerFactory);
            var routeInfos = GetRouteInfos(_constraintResolver, actions.Items);

            // We're creating one AttributeRouteGenerationEntry per action. This allows us to match the intended
            // action by expected route values, and then use the TemplateBinder to generate the link.
            foreach (var routeInfo in routeInfos)
            {
                routeBuilder.Add(new TreeRouteLinkGenerationEntry()
                {
                    Binder = new TemplateBinder(_urlEncoder, _contextPool, routeInfo.ParsedTemplate, routeInfo.Defaults),
                    Defaults = routeInfo.Defaults,
                    Constraints = routeInfo.Constraints,
                    Order = routeInfo.Order,
                    GenerationPrecedence = routeInfo.GenerationPrecedence,
                    RequiredLinkValues = routeInfo.ActionDescriptor.RouteValueDefaults,
                    RouteGroup = routeInfo.RouteGroup,
                    Template = routeInfo.ParsedTemplate,
                    Name = routeInfo.Name,
                });
            }

            // We're creating one AttributeRouteMatchingEntry per group, so we need to identify the distinct set of
            // groups. It's guaranteed that all members of the group have the same template and precedence,
            // so we only need to hang on to a single instance of the RouteInfo for each group.
            var distinctRouteInfosByGroup = GroupRouteInfosByGroupId(routeInfos);
            foreach (var routeInfo in distinctRouteInfosByGroup)
            {
                routeBuilder.Add(new TreeRouteMatchingEntry()
                {
                    Order = routeInfo.Order,
                    Precedence = routeInfo.MatchPrecedence,
                    Target = _target,
                    RouteName = routeInfo.Name,
                    RouteTemplate = TemplateParser.Parse(routeInfo.RouteTemplate),
                    TemplateMatcher = new TemplateMatcher(
                        routeInfo.ParsedTemplate,
                        new RouteValueDictionary(StringComparer.OrdinalIgnoreCase)
                        {
                            { TreeRouter.RouteGroupKey, routeInfo.RouteGroup }
                        }),
                    Constraints = routeInfo.Constraints
                });
            }

            return routeBuilder.Build(actions.Version);
        }