diff --git a/test/Microsoft.AspNetCore.Routing.FunctionalTests/EndpointRoutingSampleTest.cs b/test/Microsoft.AspNetCore.Routing.FunctionalTests/EndpointRoutingSampleTest.cs index 6f8241e38b..6201b01d7b 100644 --- a/test/Microsoft.AspNetCore.Routing.FunctionalTests/EndpointRoutingSampleTest.cs +++ b/test/Microsoft.AspNetCore.Routing.FunctionalTests/EndpointRoutingSampleTest.cs @@ -24,6 +24,22 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests _client.BaseAddress = new Uri("http://localhost"); } + [Theory] + [InlineData("Branch1")] + [InlineData("Branch2")] + public async Task Routing_CanRouteRequest_ToBranchRouter(string branch) + { + // Arrange + var message = new HttpRequestMessage(HttpMethod.Get, $"{branch}/api/get/5"); + + // Act + var response = await _client.SendAsync(message); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal($"{branch} - API Get 5", await response.Content.ReadAsStringAsync()); + } + [Fact] public async Task MatchesRootPath_AndReturnsPlaintext() { diff --git a/test/Microsoft.AspNetCore.Routing.FunctionalTests/RouterSampleTest.cs b/test/Microsoft.AspNetCore.Routing.FunctionalTests/RouterSampleTest.cs index 315dcc1b68..9388a03781 100644 --- a/test/Microsoft.AspNetCore.Routing.FunctionalTests/RouterSampleTest.cs +++ b/test/Microsoft.AspNetCore.Routing.FunctionalTests/RouterSampleTest.cs @@ -24,6 +24,22 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests _client.BaseAddress = new Uri("http://localhost"); } + [Theory] + [InlineData("Branch1")] + [InlineData("Branch2")] + public async Task Routing_CanRouteRequest_ToBranchRouter(string branch) + { + // Arrange + var message = new HttpRequestMessage(HttpMethod.Get, $"{branch}/api/get/5"); + + // Act + var response = await _client.SendAsync(message); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal($"{branch} - API Get 5", await response.Content.ReadAsStringAsync()); + } + [Fact] public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb() { diff --git a/test/WebSites/RoutingWebSite/UseEndpointRoutingStartup.cs b/test/WebSites/RoutingWebSite/UseEndpointRoutingStartup.cs index a26c16baa2..fb067a3fe4 100644 --- a/test/WebSites/RoutingWebSite/UseEndpointRoutingStartup.cs +++ b/test/WebSites/RoutingWebSite/UseEndpointRoutingStartup.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Internal; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Internal; @@ -34,11 +35,11 @@ namespace RoutingWebSite public void Configure(IApplicationBuilder app) { - app.UseEndpointRouting(builder => + app.UseEndpointRouting(routes => { - builder.MapHello("/helloworld", "World"); + routes.MapHello("/helloworld", "World"); - builder.MapGet( + routes.MapGet( "/", (httpContext) => { @@ -56,7 +57,7 @@ namespace RoutingWebSite response.ContentType = "text/plain"; return response.WriteAsync(sb.ToString()); }); - builder.MapGet( + routes.MapGet( "/plaintext", (httpContext) => { @@ -67,7 +68,7 @@ namespace RoutingWebSite response.ContentLength = payloadLength; return response.Body.WriteAsync(_plainTextPayload, 0, payloadLength); }); - builder.MapGet( + routes.MapGet( "/withconstraints/{id:endsWith(_001)}", (httpContext) => { @@ -76,7 +77,7 @@ namespace RoutingWebSite response.ContentType = "text/plain"; return response.WriteAsync("WithConstraints"); }); - builder.MapGet( + routes.MapGet( "/withoptionalconstraints/{id:endsWith(_001)?}", (httpContext) => { @@ -85,7 +86,7 @@ namespace RoutingWebSite response.ContentType = "text/plain"; return response.WriteAsync("withoptionalconstraints"); }); - builder.MapGet( + routes.MapGet( "/WithSingleAsteriskCatchAll/{*path}", (httpContext) => { @@ -98,7 +99,7 @@ namespace RoutingWebSite "Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithSingleAsteriskCatchAll", new { })); }, new RouteValuesAddressMetadata(routeName: "WithSingleAsteriskCatchAll", requiredValues: new RouteValueDictionary())); - builder.MapGet( + routes.MapGet( "/WithDoubleAsteriskCatchAll/{**path}", (httpContext) => { @@ -113,11 +114,24 @@ namespace RoutingWebSite new RouteValuesAddressMetadata(routeName: "WithDoubleAsteriskCatchAll", requiredValues: new RouteValueDictionary())); }); + app.Map("/Branch1", branch => SetupBranch(branch, "Branch1")); + app.Map("/Branch2", branch => SetupBranch(branch, "Branch2")); + app.UseStaticFiles(); // Imagine some more stuff here... app.UseEndpoint(); } + + private void SetupBranch(IApplicationBuilder app, string name) + { + app.UseEndpointRouting(routes => + { + routes.MapGet("api/get/{id}", (context) => context.Response.WriteAsync($"{name} - API Get {context.GetRouteData().Values["id"]}")); + }); + + app.UseEndpoint(); + } } } diff --git a/test/WebSites/RoutingWebSite/UseRouterStartup.cs b/test/WebSites/RoutingWebSite/UseRouterStartup.cs index fb27ce550f..f09f359acf 100644 --- a/test/WebSites/RoutingWebSite/UseRouterStartup.cs +++ b/test/WebSites/RoutingWebSite/UseRouterStartup.cs @@ -38,6 +38,17 @@ namespace RoutingWebSite defaults: new { lastName = "Doe" }, constraints: new { lastName = new RegexRouteConstraint(new Regex("[a-zA-Z]{3}", RegexOptions.CultureInvariant, RegexMatchTimeout)) }); }); + + app.Map("/Branch1", branch => SetupBranch(branch, "Branch1")); + app.Map("/Branch2", branch => SetupBranch(branch, "Branch2")); + } + + private void SetupBranch(IApplicationBuilder app, string name) + { + app.UseRouter(routes => + { + routes.MapGet("api/get/{id}", (request, response, routeData) => response.WriteAsync($"{name} - API Get {routeData.Values["id"]}")); + }); } } }