Example #1
0
        public override Matcher Build()
        {
            var selector = new DefaultEndpointSelector();

            var groups = _endpoints
                         .GroupBy(e => (e.Order, e.RoutePattern.InboundPrecedence, e.RoutePattern.RawText))
                         .OrderBy(g => g.Key.Order)
                         .ThenBy(g => g.Key.InboundPrecedence);

            var routes = new RouteCollection();

            foreach (var group in groups)
            {
                var candidates = group.ToArray();
                var endpoint   = group.First();

                // RoutePattern.Defaults contains the default values parsed from the template
                // as well as those specified with a literal. We need to separate those
                // for legacy cases.
                //
                // To do this we re-parse the original text and compare.
                var withoutDefaults = RoutePatternFactory.Parse(endpoint.RoutePattern.RawText);
                var defaults        = new RouteValueDictionary(endpoint.RoutePattern.Defaults);
                for (var i = 0; i < withoutDefaults.Parameters.Count; i++)
                {
                    var parameter = withoutDefaults.Parameters[i];
                    if (parameter.Default != null)
                    {
                        defaults.Remove(parameter.Name);
                    }
                }

                routes.Add(new Route(
                               new SelectorRouter(selector, candidates),
                               endpoint.RoutePattern.RawText,
                               defaults,
                               new Dictionary <string, object>(),
                               new RouteValueDictionary(),
                               _constraintResolver));
            }

            return(new RouteMatcher(routes));
        }
Example #2
0
        public override Matcher Build()
        {
            var builder = new TreeRouteBuilder(
                NullLoggerFactory.Instance,
                new DefaultObjectPool <UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()),
                new DefaultInlineConstraintResolver(Options.Create(new RouteOptions()), new TestServiceProvider()));

            var selector = new DefaultEndpointSelector();

            var groups = _endpoints
                         .GroupBy(e => (e.Order, e.RoutePattern.InboundPrecedence, e.RoutePattern.RawText))
                         .OrderBy(g => g.Key.Order)
                         .ThenBy(g => g.Key.InboundPrecedence);

            var routes = new RouteCollection();

            foreach (var group in groups)
            {
                var candidates = group.ToArray();

                // RouteEndpoint.Values contains the default values parsed from the template
                // as well as those specified with a literal. We need to separate those
                // for legacy cases.
                var endpoint = group.First();
                var defaults = new RouteValueDictionary(endpoint.RoutePattern.Defaults);
                for (var i = 0; i < endpoint.RoutePattern.Parameters.Count; i++)
                {
                    var parameter = endpoint.RoutePattern.Parameters[i];
                    if (parameter.Default != null)
                    {
                        defaults.Remove(parameter.Name);
                    }
                }

                builder.MapInbound(
                    new SelectorRouter(selector, candidates),
                    new RouteTemplate(endpoint.RoutePattern),
                    routeName: null,
                    order: endpoint.Order);
            }

            return(new TreeRouterMatcher(builder.Build()));
        }
        public void Setup()
        {
            Endpoints    = new RouteEndpoint[10];
            Endpoints[0] = CreateEndpoint("/product", "GET");
            Endpoints[1] = CreateEndpoint("/product/{id}", "GET");

            Endpoints[2] = CreateEndpoint("/account", "GET");
            Endpoints[3] = CreateEndpoint("/account/{id}");
            Endpoints[4] = CreateEndpoint("/account/{id}", "POST");
            Endpoints[5] = CreateEndpoint("/account/{id}", "UPDATE");

            Endpoints[6] = CreateEndpoint("/v2/account", "GET");
            Endpoints[7] = CreateEndpoint("/v2/account/{id}");
            Endpoints[8] = CreateEndpoint("/v2/account/{id}", "POST");
            Endpoints[9] = CreateEndpoint("/v2/account/{id}", "UPDATE");

            // Define an unordered mixture of policies that implement INodeBuilderPolicy,
            // IEndpointComparerPolicy and/or IEndpointSelectorPolicy
            _policies = new List <MatcherPolicy>()
            {
                CreateNodeBuilderPolicy(4),
                CreateUberPolicy(2),
                CreateNodeBuilderPolicy(3),
                CreateEndpointComparerPolicy(5),
                CreateNodeBuilderPolicy(1),
                CreateEndpointSelectorPolicy(9),
                CreateEndpointComparerPolicy(7),
                CreateNodeBuilderPolicy(6),
                CreateEndpointSelectorPolicy(10),
                CreateUberPolicy(12),
                CreateEndpointComparerPolicy(11)
            };
            _loggerFactory          = NullLoggerFactory.Instance;
            _selector               = new DefaultEndpointSelector();
            _parameterPolicyFactory = new DefaultParameterPolicyFactory(Options.Create(new RouteOptions()), new TestServiceProvider());

            _services = CreateServices();
        }