Add branching functional tests (#905)

This commit is contained in:
James Newton-King 2018-11-06 14:23:37 +13:00 committed by GitHub
parent 8acbae76e8
commit 444bf1d93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 8 deletions

View File

@ -24,6 +24,22 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
_client.BaseAddress = new Uri("http://localhost"); _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] [Fact]
public async Task MatchesRootPath_AndReturnsPlaintext() public async Task MatchesRootPath_AndReturnsPlaintext()
{ {

View File

@ -24,6 +24,22 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
_client.BaseAddress = new Uri("http://localhost"); _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] [Fact]
public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb() public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb()
{ {

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Internal; using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Internal; using Microsoft.AspNetCore.Routing.Internal;
@ -34,11 +35,11 @@ namespace RoutingWebSite
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
app.UseEndpointRouting(builder => app.UseEndpointRouting(routes =>
{ {
builder.MapHello("/helloworld", "World"); routes.MapHello("/helloworld", "World");
builder.MapGet( routes.MapGet(
"/", "/",
(httpContext) => (httpContext) =>
{ {
@ -56,7 +57,7 @@ namespace RoutingWebSite
response.ContentType = "text/plain"; response.ContentType = "text/plain";
return response.WriteAsync(sb.ToString()); return response.WriteAsync(sb.ToString());
}); });
builder.MapGet( routes.MapGet(
"/plaintext", "/plaintext",
(httpContext) => (httpContext) =>
{ {
@ -67,7 +68,7 @@ namespace RoutingWebSite
response.ContentLength = payloadLength; response.ContentLength = payloadLength;
return response.Body.WriteAsync(_plainTextPayload, 0, payloadLength); return response.Body.WriteAsync(_plainTextPayload, 0, payloadLength);
}); });
builder.MapGet( routes.MapGet(
"/withconstraints/{id:endsWith(_001)}", "/withconstraints/{id:endsWith(_001)}",
(httpContext) => (httpContext) =>
{ {
@ -76,7 +77,7 @@ namespace RoutingWebSite
response.ContentType = "text/plain"; response.ContentType = "text/plain";
return response.WriteAsync("WithConstraints"); return response.WriteAsync("WithConstraints");
}); });
builder.MapGet( routes.MapGet(
"/withoptionalconstraints/{id:endsWith(_001)?}", "/withoptionalconstraints/{id:endsWith(_001)?}",
(httpContext) => (httpContext) =>
{ {
@ -85,7 +86,7 @@ namespace RoutingWebSite
response.ContentType = "text/plain"; response.ContentType = "text/plain";
return response.WriteAsync("withoptionalconstraints"); return response.WriteAsync("withoptionalconstraints");
}); });
builder.MapGet( routes.MapGet(
"/WithSingleAsteriskCatchAll/{*path}", "/WithSingleAsteriskCatchAll/{*path}",
(httpContext) => (httpContext) =>
{ {
@ -98,7 +99,7 @@ namespace RoutingWebSite
"Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithSingleAsteriskCatchAll", new { })); "Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithSingleAsteriskCatchAll", new { }));
}, },
new RouteValuesAddressMetadata(routeName: "WithSingleAsteriskCatchAll", requiredValues: new RouteValueDictionary())); new RouteValuesAddressMetadata(routeName: "WithSingleAsteriskCatchAll", requiredValues: new RouteValueDictionary()));
builder.MapGet( routes.MapGet(
"/WithDoubleAsteriskCatchAll/{**path}", "/WithDoubleAsteriskCatchAll/{**path}",
(httpContext) => (httpContext) =>
{ {
@ -113,11 +114,24 @@ namespace RoutingWebSite
new RouteValuesAddressMetadata(routeName: "WithDoubleAsteriskCatchAll", requiredValues: new RouteValueDictionary())); new RouteValuesAddressMetadata(routeName: "WithDoubleAsteriskCatchAll", requiredValues: new RouteValueDictionary()));
}); });
app.Map("/Branch1", branch => SetupBranch(branch, "Branch1"));
app.Map("/Branch2", branch => SetupBranch(branch, "Branch2"));
app.UseStaticFiles(); app.UseStaticFiles();
// Imagine some more stuff here... // Imagine some more stuff here...
app.UseEndpoint(); 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();
}
} }
} }

View File

@ -38,6 +38,17 @@ namespace RoutingWebSite
defaults: new { lastName = "Doe" }, defaults: new { lastName = "Doe" },
constraints: new { lastName = new RegexRouteConstraint(new Regex("[a-zA-Z]{3}", RegexOptions.CultureInvariant, RegexMatchTimeout)) }); 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"]}"));
});
} }
} }
} }