This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #1035] RouteGroupConstraint should only be added once for non
attribute routed actions 1. Changed ReflectedActionDescriptorProvider to add RouteGroupConstraint only once for non attribute routed actions. 2. Added tests to cover the scenario.
- Loading branch information
Showing
2 changed files
with
144 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,16 +84,16 @@ public void GetDescriptors_WithRouteDataConstraint_WithBlockNonAttributedActions | |
typeof(BlockNonAttributedActionsController).GetTypeInfo()).ToArray(); | ||
|
||
var descriptorWithoutConstraint = Assert.Single( | ||
descriptors, | ||
descriptors, | ||
ad => ad.RouteConstraints.Any( | ||
c => c.RouteKey == "key" && c.KeyHandling == RouteKeyHandling.DenyKey)); | ||
|
||
var descriptorWithConstraint = Assert.Single( | ||
descriptors, | ||
ad => ad.RouteConstraints.Any( | ||
c => | ||
c.KeyHandling == RouteKeyHandling.RequireKey && | ||
c.RouteKey == "key" && | ||
c => | ||
c.KeyHandling == RouteKeyHandling.RequireKey && | ||
c.RouteKey == "key" && | ||
c.RouteValue == "value")); | ||
|
||
// Assert | ||
|
@@ -265,6 +265,67 @@ public void AttributeRouting_TokenReplacement_ThrowsWithMultipleMessages() | |
Assert.Equal(expectedMessage, ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void AttributeRouting_RouteGroupConstraint_IsAddedOnceForNonAttributeRoutes() | ||
{ | ||
// Arrange | ||
var provider = GetProvider( | ||
typeof(MixedAttributeRouteController).GetTypeInfo(), | ||
typeof(ConstrainedController).GetTypeInfo()); | ||
|
||
// Act | ||
var actionDescriptors = provider.GetDescriptors(); | ||
|
||
// Assert | ||
Assert.NotNull(actionDescriptors); | ||
Assert.Equal(4, actionDescriptors.Count()); | ||
|
||
var nonAttributeRoutedAction = Assert.Single(actionDescriptors, ad => ad.Name.Equals("AnotherNonAttributedAction")); | ||
|
||
This comment has been minimized.
Sorry, something went wrong. |
||
Assert.Equal(5, nonAttributeRoutedAction.RouteConstraints.Count); | ||
|
||
var routeGroupConstraint = Assert.Single(nonAttributeRoutedAction.RouteConstraints, rc => rc.RouteKey.Equals(AttributeRouting.RouteGroupKey)); | ||
Assert.Equal(RouteKeyHandling.DenyKey, routeGroupConstraint.KeyHandling); | ||
} | ||
|
||
[Fact] | ||
public void AttributeRouting_AddsDefaultRouteValues_ForAttributeRoutedActions() | ||
{ | ||
// Arrange | ||
var provider = GetProvider( | ||
typeof(MixedAttributeRouteController).GetTypeInfo(), | ||
typeof(ConstrainedController).GetTypeInfo()); | ||
|
||
// Act | ||
var actionDescriptors = provider.GetDescriptors(); | ||
|
||
// Assert | ||
Assert.NotNull(actionDescriptors); | ||
Assert.Equal(4, actionDescriptors.Count()); | ||
|
||
var indexAction = Assert.Single(actionDescriptors, ad => ad.Name.Equals("Index")); | ||
|
||
Assert.Equal(1, indexAction.RouteConstraints.Count); | ||
|
||
var routeGroupConstraint = Assert.Single(indexAction.RouteConstraints, rc => rc.RouteKey.Equals(AttributeRouting.RouteGroupKey)); | ||
Assert.Equal(RouteKeyHandling.RequireKey, routeGroupConstraint.KeyHandling); | ||
This comment has been minimized.
Sorry, something went wrong.
rynowak
Member
|
||
Assert.Equal("__ROUTE__0-INDEX", routeGroupConstraint.RouteValue); | ||
|
||
Assert.Equal(4, indexAction.RouteValueDefaults.Count); | ||
|
||
var controllerDefault = Assert.Single(indexAction.RouteValueDefaults, rd => rd.Key.Equals("controller", StringComparison.OrdinalIgnoreCase)); | ||
Assert.Equal("MixedAttributeRoute", controllerDefault.Value); | ||
|
||
var actionDefault = Assert.Single(indexAction.RouteValueDefaults, rd => rd.Key.Equals("action", StringComparison.OrdinalIgnoreCase)); | ||
Assert.Equal("Index", actionDefault.Value); | ||
|
||
var myRouteConstraintDefault = Assert.Single(indexAction.RouteValueDefaults, rd => rd.Key.Equals("key", StringComparison.OrdinalIgnoreCase)); | ||
Assert.Null(myRouteConstraintDefault.Value); | ||
|
||
var anotherRouteConstraint = Assert.Single(indexAction.RouteValueDefaults, rd => rd.Key.Equals("second", StringComparison.OrdinalIgnoreCase)); | ||
Assert.Null(anotherRouteConstraint.Value); | ||
} | ||
|
||
[Fact] | ||
public void AttributeRouting_TokenReplacement_CaseInsensitive() | ||
{ | ||
|
@@ -318,7 +379,7 @@ public void AttributeRouting_DoesNotValidateParameters() | |
} | ||
|
||
private ReflectedActionDescriptorProvider GetProvider( | ||
TypeInfo controllerTypeInfo, | ||
TypeInfo controllerTypeInfo, | ||
IEnumerable<IFilter> filters = null) | ||
{ | ||
var conventions = new StaticActionDiscoveryConventions(controllerTypeInfo); | ||
|
@@ -338,6 +399,26 @@ public void AttributeRouting_DoesNotValidateParameters() | |
return provider; | ||
} | ||
|
||
private ReflectedActionDescriptorProvider GetProvider( | ||
params TypeInfo[] controllerTypeInfo) | ||
{ | ||
var conventions = new StaticActionDiscoveryConventions(controllerTypeInfo); | ||
|
||
var assemblyProvider = new Mock<IControllerAssemblyProvider>(); | ||
assemblyProvider | ||
.SetupGet(ap => ap.CandidateAssemblies) | ||
.Returns(new Assembly[] { controllerTypeInfo.First().Assembly }); | ||
|
||
var provider = new ReflectedActionDescriptorProvider( | ||
assemblyProvider.Object, | ||
conventions, | ||
Enumerable.Empty<IFilter>(), | ||
new MockMvcOptionsAccessor(), | ||
Mock.Of<IInlineConstraintResolver>()); | ||
|
||
return provider; | ||
} | ||
|
||
private IEnumerable<ActionDescriptor> GetDescriptors(params TypeInfo[] controllerTypeInfos) | ||
{ | ||
var conventions = new StaticActionDiscoveryConventions(controllerTypeInfos); | ||
|
@@ -353,7 +434,7 @@ private IEnumerable<ActionDescriptor> GetDescriptors(params TypeInfo[] controlle | |
null, | ||
new MockMvcOptionsAccessor(), | ||
null); | ||
|
||
return provider.GetDescriptors(); | ||
} | ||
|
||
|
@@ -386,6 +467,14 @@ public MyRouteConstraintAttribute(bool blockNonAttributedActions) | |
} | ||
} | ||
|
||
public class MySecondRouteConstraintAttribute : RouteConstraintAttribute | ||
{ | ||
public MySecondRouteConstraintAttribute(bool blockNonAttributedActions) | ||
: base("second", "value", blockNonAttributedActions) | ||
{ | ||
} | ||
} | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
rynowak
Member
|
||
[MyRouteConstraintAttribute(blockNonAttributedActions: true)] | ||
private class BlockNonAttributedActionsController | ||
{ | ||
|
@@ -458,5 +547,23 @@ private class SameGroupIdController | |
[HttpGet("stub/Action1")] | ||
public void Action2() { } | ||
} | ||
|
||
private class MixedAttributeRouteController | ||
{ | ||
[HttpGet("Index")] | ||
public void Index() { } | ||
|
||
[HttpGet("Edit")] | ||
public void Edit() { } | ||
|
||
public void AnotherNonAttributedAction() { } | ||
} | ||
|
||
[MyRouteConstraint(blockNonAttributedActions: true)] | ||
[MySecondRouteConstraint(blockNonAttributedActions: true)] | ||
private class ConstrainedController | ||
{ | ||
public void ConstrainedNonAttributedAction() { } | ||
} | ||
} | ||
} |
why not check all of them? the test doesn't do what the name suggests