Fix RouteValueAddressScheme to require metadata to match (#910)
This commit is contained in:
parent
444bf1d93f
commit
f6b1138ce3
|
|
@ -125,6 +125,11 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (endpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>() == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (endpoint.Metadata.GetMetadata<ISuppressLinkGenerationMetadata>()?.SuppressLinkGeneration == true)
|
if (endpoint.Metadata.GetMetadata<ISuppressLinkGenerationMetadata>()?.SuppressLinkGeneration == true)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -321,7 +321,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void GetLink_ParameterTransformer()
|
public void GetLink_ParameterTransformer()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint = EndpointFactory.CreateRouteEndpoint("{controller:upper-case}/{name}");
|
var endpoint = EndpointFactory.CreateRouteEndpoint("{controller:upper-case}/{name}", requiredValues: new { controller = "Home", name = "Test" });
|
||||||
|
|
||||||
Action<IServiceCollection> configure = (s) =>
|
Action<IServiceCollection> configure = (s) =>
|
||||||
{
|
{
|
||||||
|
|
@ -344,7 +344,10 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void GetLink_ParameterTransformer_ForQueryString()
|
public void GetLink_ParameterTransformer_ForQueryString()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint = EndpointFactory.CreateRouteEndpoint("{controller:upper-case}/{name}", policies: new { c = new UpperCaseParameterTransform(), });
|
var endpoint = EndpointFactory.CreateRouteEndpoint(
|
||||||
|
"{controller:upper-case}/{name}",
|
||||||
|
requiredValues: new { controller = "Home", name = "Test", c = "hithere", },
|
||||||
|
policies: new { c = new UpperCaseParameterTransform(), });
|
||||||
|
|
||||||
Action<IServiceCollection> configure = (s) =>
|
Action<IServiceCollection> configure = (s) =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void GetOutboundMatches_GetsNamedMatchesFor_EndpointsHaving_IRouteNameMetadata()
|
public void GetOutboundMatches_GetsNamedMatchesFor_EndpointsHaving_IRouteNameMetadata()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint1 = CreateEndpoint("/a");
|
var endpoint1 = CreateEndpoint("/a", routeName: "other");
|
||||||
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void GetOutboundMatches_GroupsMultipleEndpoints_WithSameName()
|
public void GetOutboundMatches_GroupsMultipleEndpoints_WithSameName()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint1 = CreateEndpoint("/a");
|
var endpoint1 = CreateEndpoint("/a", routeName: "other");
|
||||||
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
||||||
var endpoint3 = CreateEndpoint("/b", routeName: "named");
|
var endpoint3 = CreateEndpoint("/b", routeName: "named");
|
||||||
|
|
||||||
|
|
@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void GetOutboundMatches_GroupsMultipleEndpoints_WithSameName_IgnoringCase()
|
public void GetOutboundMatches_GroupsMultipleEndpoints_WithSameName_IgnoringCase()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint1 = CreateEndpoint("/a");
|
var endpoint1 = CreateEndpoint("/a", routeName: "other");
|
||||||
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
var endpoint2 = CreateEndpoint("/a", routeName: "named");
|
||||||
var endpoint3 = CreateEndpoint("/b", routeName: "NaMed");
|
var endpoint3 = CreateEndpoint("/b", routeName: "NaMed");
|
||||||
|
|
||||||
|
|
@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public void EndpointDataSource_ChangeCallback_Refreshes_OutboundMatches()
|
public void EndpointDataSource_ChangeCallback_Refreshes_OutboundMatches()
|
||||||
{
|
{
|
||||||
// Arrange 1
|
// Arrange 1
|
||||||
var endpoint1 = CreateEndpoint("/a");
|
var endpoint1 = CreateEndpoint("/a", requiredValues: new { });
|
||||||
var dynamicDataSource = new DynamicEndpointDataSource(new[] { endpoint1 });
|
var dynamicDataSource = new DynamicEndpointDataSource(new[] { endpoint1 });
|
||||||
|
|
||||||
// Act 1
|
// Act 1
|
||||||
|
|
@ -93,21 +93,21 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
Assert.Same(endpoint1, actual);
|
Assert.Same(endpoint1, actual);
|
||||||
|
|
||||||
// Arrange 2
|
// Arrange 2
|
||||||
var endpoint2 = CreateEndpoint("/b");
|
var endpoint2 = CreateEndpoint("/b", requiredValues: new { });
|
||||||
|
|
||||||
// Act 2
|
// Act 2
|
||||||
// Trigger change
|
// Trigger change
|
||||||
dynamicDataSource.AddEndpoint(endpoint2);
|
dynamicDataSource.AddEndpoint(endpoint2);
|
||||||
|
|
||||||
// Arrange 2
|
// Arrange 2
|
||||||
var endpoint3 = CreateEndpoint("/c");
|
var endpoint3 = CreateEndpoint("/c", requiredValues: new { });
|
||||||
|
|
||||||
// Act 2
|
// Act 2
|
||||||
// Trigger change
|
// Trigger change
|
||||||
dynamicDataSource.AddEndpoint(endpoint3);
|
dynamicDataSource.AddEndpoint(endpoint3);
|
||||||
|
|
||||||
// Arrange 3
|
// Arrange 3
|
||||||
var endpoint4 = CreateEndpoint("/d");
|
var endpoint4 = CreateEndpoint("/d", requiredValues: new { });
|
||||||
|
|
||||||
// Act 3
|
// Act 3
|
||||||
// Trigger change
|
// Trigger change
|
||||||
|
|
@ -146,13 +146,11 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
var endpoint1 = CreateEndpoint(
|
var endpoint1 = CreateEndpoint(
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
defaults: new { zipCode = 3510 },
|
defaults: new { zipCode = 3510 },
|
||||||
requiredValues: new { id = 7 },
|
requiredValues: new { id = 7 });
|
||||||
routeName: "OrdersApi");
|
|
||||||
var endpoint2 = CreateEndpoint(
|
var endpoint2 = CreateEndpoint(
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
defaults: new { id = 12 },
|
defaults: new { id = 12 },
|
||||||
requiredValues: new { zipCode = 3510 },
|
requiredValues: new { zipCode = 3510 });
|
||||||
routeName: "OrdersApi");
|
|
||||||
var addressScheme = CreateAddressScheme(endpoint1, endpoint2);
|
var addressScheme = CreateAddressScheme(endpoint1, endpoint2);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -174,46 +172,12 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
var endpoint1 = CreateEndpoint(
|
var endpoint1 = CreateEndpoint(
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
defaults: new { zipCode = 3510 },
|
defaults: new { zipCode = 3510 },
|
||||||
requiredValues: new { id = 7 },
|
requiredValues: new { id = 7 });
|
||||||
routeName: "OrdersApi");
|
|
||||||
var endpoint2 = CreateEndpoint(
|
var endpoint2 = CreateEndpoint(
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
defaults: new { id = 12 },
|
defaults: new { id = 12 });
|
||||||
routeName: "OrdersApi");
|
|
||||||
var addressScheme = CreateAddressScheme(endpoint1, endpoint2);
|
var addressScheme = CreateAddressScheme(endpoint1, endpoint2);
|
||||||
|
|
||||||
// Act
|
|
||||||
var foundEndpoints = addressScheme.FindEndpoints(
|
|
||||||
new RouteValuesAddress
|
|
||||||
{
|
|
||||||
ExplicitValues = new RouteValueDictionary(new { id = 13 }),
|
|
||||||
AmbientValues = new RouteValueDictionary(new { zipCode = 3500 }),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
var actual = Assert.Single(foundEndpoints);
|
|
||||||
Assert.Same(endpoint2, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void FindEndpoints_LookedUpByCriteria_MultipleMatches()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var endpoint1 = CreateEndpoint(
|
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
|
||||||
defaults: new { zipCode = 3510 },
|
|
||||||
requiredValues: new { id = 7 },
|
|
||||||
routeName: "OrdersApi");
|
|
||||||
var endpoint2 = CreateEndpoint(
|
|
||||||
"api/orders/{id}/{name?}/{urgent}/{zipCode}",
|
|
||||||
defaults: new { id = 12 },
|
|
||||||
routeName: "OrdersApi");
|
|
||||||
var endpoint3 = CreateEndpoint(
|
|
||||||
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
|
||||||
defaults: new { id = 12 },
|
|
||||||
routeName: "OrdersApi");
|
|
||||||
var addressScheme = CreateAddressScheme(endpoint1, endpoint2, endpoint3);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var foundEndpoints = addressScheme.FindEndpoints(
|
var foundEndpoints = addressScheme.FindEndpoints(
|
||||||
new RouteValuesAddress
|
new RouteValuesAddress
|
||||||
|
|
@ -223,7 +187,64 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Contains(endpoint1, foundEndpoints);
|
var actual = Assert.Single(foundEndpoints);
|
||||||
|
Assert.Same(endpoint1, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FindEndpoints_LookedUpByCriteria_MultipleMatches()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var endpoint1 = CreateEndpoint(
|
||||||
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
|
defaults: new { zipCode = 3510 },
|
||||||
|
requiredValues: new { id = 7 });
|
||||||
|
var endpoint2 = CreateEndpoint(
|
||||||
|
"api/orders/{id}/{name?}/{urgent}/{zipCode}",
|
||||||
|
defaults: new { id = 12 },
|
||||||
|
requiredValues: new { id = 12 });
|
||||||
|
var endpoint3 = CreateEndpoint(
|
||||||
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
|
defaults: new { id = 12 },
|
||||||
|
requiredValues: new { id = 12 });
|
||||||
|
var addressScheme = CreateAddressScheme(endpoint1, endpoint2, endpoint3);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var foundEndpoints = addressScheme.FindEndpoints(
|
||||||
|
new RouteValuesAddress
|
||||||
|
{
|
||||||
|
ExplicitValues = new RouteValueDictionary(new { id = 12 }),
|
||||||
|
AmbientValues = new RouteValueDictionary(new { zipCode = 3500 }),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Collection(foundEndpoints,
|
||||||
|
e => Assert.Equal(endpoint3, e),
|
||||||
|
e => Assert.Equal(endpoint2, e));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FindEndpoints_LookedUpByCriteria_ExcludeEndpointWithoutRouteValuesAddressMetadata()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var endpoint1 = CreateEndpoint(
|
||||||
|
"api/orders/{id}/{name?}/{urgent=true}/{zipCode}",
|
||||||
|
defaults: new { zipCode = 3510 },
|
||||||
|
requiredValues: new { id = 7 });
|
||||||
|
var endpoint2 = CreateEndpoint("test");
|
||||||
|
|
||||||
|
var addressScheme = CreateAddressScheme(endpoint1, endpoint2);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var foundEndpoints = addressScheme.FindEndpoints(
|
||||||
|
new RouteValuesAddress
|
||||||
|
{
|
||||||
|
ExplicitValues = new RouteValueDictionary(new { id = 7 }),
|
||||||
|
AmbientValues = new RouteValueDictionary(new { zipCode = 3500 }),
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.DoesNotContain(endpoint2, foundEndpoints);
|
||||||
Assert.Contains(endpoint1, foundEndpoints);
|
Assert.Contains(endpoint1, foundEndpoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -302,7 +323,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
// Arrange
|
// Arrange
|
||||||
var endpoint = EndpointFactory.CreateRouteEndpoint(
|
var endpoint = EndpointFactory.CreateRouteEndpoint(
|
||||||
"/a",
|
"/a",
|
||||||
metadata: new object[] { new SuppressLinkGenerationMetadata(), new EncourageLinkGenerationMetadata(), });
|
metadata: new object[] { new SuppressLinkGenerationMetadata(), new EncourageLinkGenerationMetadata(), new RouteValuesAddressMetadata(string.Empty), });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var addressScheme = CreateAddressScheme(endpoint);
|
var addressScheme = CreateAddressScheme(endpoint);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue