Exemplo n.º 1
0
        public void BuildEndpoints_JustHostWithWildcard_Works()
        {
            // Arrange
            var builder     = Create <RuntimeRouteBuilder>();
            var parsedRoute = new ParsedRoute
            {
                RouteId  = "route1",
                Host     = "*.example.com",
                Priority = 12,
            };
            var backend   = new BackendInfo("backend1", new EndpointManager(), new Mock <IProxyHttpClientFactory>().Object);
            var routeInfo = new RouteInfo("route1");

            // Act
            var config = builder.Build(parsedRoute, backend, routeInfo);

            // Assert
            config.BackendOrNull.Should().BeSameAs(backend);
            config.Priority.Should().Be(12);
            config.MatcherSummary.Should().Be(parsedRoute.GetMatcherSummary());
            config.AspNetCoreEndpoints.Should().HaveCount(1);
            var routeEndpoint = config.AspNetCoreEndpoints[0] as AspNetCore.Routing.RouteEndpoint;

            routeEndpoint.DisplayName.Should().Be("route1");
            routeEndpoint.Metadata.GetMetadata <RouteConfig>().Should().BeSameAs(config);
            routeEndpoint.RoutePattern.RawText.Should().Be("/{**catchall}");

            var hostMetadata = routeEndpoint.Metadata.GetMetadata <AspNetCore.Routing.HostAttribute>();

            hostMetadata.Should().NotBeNull();
            hostMetadata.Hosts.Should().BeEquivalentTo("*.example.com");
        }
        public void BuildEndpoints_JustHostWithWildcard_Works()
        {
            // Arrange
            var builder     = Create <RuntimeRouteBuilder>();
            var parsedRoute = new ParsedRoute
            {
                RouteId  = "route1",
                Host     = "*.example.com",
                Priority = 12,
            };
            var backend   = new BackendInfo("backend1", new DestinationManager(), new Mock <IProxyHttpClientFactory>().Object);
            var routeInfo = new RouteInfo("route1");

            // Act
            var config = builder.Build(parsedRoute, backend, routeInfo);

            // Assert
            Assert.Same(backend, config.BackendOrNull);
            Assert.Equal(12, config.Priority);
            Assert.Equal(parsedRoute.GetMatcherSummary(), config.MatcherSummary);
            Assert.Single(config.Endpoints);
            var routeEndpoint = config.Endpoints[0] as AspNetCore.Routing.RouteEndpoint;

            Assert.Equal("route1", routeEndpoint.DisplayName);
            Assert.Same(config, routeEndpoint.Metadata.GetMetadata <RouteConfig>());
            Assert.Equal("/{**catchall}", routeEndpoint.RoutePattern.RawText);

            var hostMetadata = routeEndpoint.Metadata.GetMetadata <AspNetCore.Routing.HostAttribute>();

            Assert.NotNull(hostMetadata);
            Assert.Single(hostMetadata.Hosts);
            Assert.Equal("*.example.com", hostMetadata.Hosts[0]);
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public RouteConfig Build(ParsedRoute source, BackendInfo backendOrNull, RouteInfo runtimeRoute)
        {
            Contracts.CheckValue(source, nameof(source));
            Contracts.CheckValue(runtimeRoute, nameof(runtimeRoute));

            // NOTE: `new RouteConfig(...)` needs a reference to the list of ASP .NET Core endpoints,
            // but the ASP .NET Core endpoints cannot be created without a `RouteConfig` metadata item.
            // We solve this chicken-egg problem by creating an (empty) list first
            // and passing a read-only wrapper of it to `RouteConfig.ctor`.
            // Recall that `List<T>.AsReadOnly()` creates a wrapper over the original list,
            // and changes to the underlying list *are* reflected on the read-only view.
            var aspNetCoreEndpoints = new List <Endpoint>(1);
            var newRouteConfig      = new RouteConfig(
                route: runtimeRoute,
                matcherSummary: source.GetMatcherSummary(),
                priority: source.Priority,
                backendOrNull: backendOrNull,
                aspNetCoreEndpoints: aspNetCoreEndpoints.AsReadOnly());

            // TODO: Handle arbitrary AST's properly
            // Catch-all pattern when no path was specified
            var pathPattern = string.IsNullOrEmpty(source.Path) ? "/{**catchall}" : source.Path;

            // TODO: Propagate route priority
            var endpointBuilder = new AspNetCore.Routing.RouteEndpointBuilder(
                requestDelegate: _pipeline ?? Invoke,
                routePattern: AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(pathPattern),
                order: 0);

            endpointBuilder.DisplayName = source.RouteId;
            endpointBuilder.Metadata.Add(newRouteConfig);

            if (source.Host != null)
            {
                endpointBuilder.Metadata.Add(new AspNetCore.Routing.HostAttribute(source.Host));
            }

            if (source.Methods != null && source.Methods.Count > 0)
            {
                endpointBuilder.Metadata.Add(new AspNetCore.Routing.HttpMethodMetadata(source.Methods));
            }

            var endpoint = endpointBuilder.Build();

            aspNetCoreEndpoints.Add(endpoint);

            return(newRouteConfig);
        }
Exemplo n.º 4
0
 public bool HasConfigChanged(ParsedRoute newConfig, BackendInfo backendOrNull)
 {
     return(Priority != newConfig.Priority ||
            BackendOrNull != backendOrNull ||
            !MatcherSummary.Equals(newConfig.GetMatcherSummary()));
 }