Merge branch 'merge/release/2.2-to-master'

This commit is contained in:
James Newton-King 2018-08-30 08:13:00 +12:00
commit c685765cd3
No known key found for this signature in database
GPG Key ID: 0A66B2F456BF5526
2 changed files with 22 additions and 23 deletions

View File

@ -10,21 +10,12 @@ namespace Microsoft.AspNetCore.Routing.Tests
{ {
public class ConstraintsTestHelper public class ConstraintsTestHelper
{ {
public static bool TestConstraint(IRouteConstraint constraint, object value, Action<IRouter> routeConfig = null) public static bool TestConstraint(IRouteConstraint constraint, object value)
{ {
var context = new Mock<HttpContext>();
var route = new RouteCollection();
if (routeConfig != null)
{
routeConfig(route);
}
var parameterName = "fake"; var parameterName = "fake";
var values = new RouteValueDictionary() { { parameterName, value } }; var values = new RouteValueDictionary() { { parameterName, value } };
var routeDirection = RouteDirection.IncomingRequest; var routeDirection = RouteDirection.IncomingRequest;
return constraint.Match(context.Object, route, parameterName, values, routeDirection); return constraint.Match(httpContext: null, route: null, parameterName, values, routeDirection);
} }
} }
} }

View File

@ -691,8 +691,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal(expectedValues.OrderBy(kvp => kvp.Key), constraint.Values.OrderBy(kvp => kvp.Key)); Assert.Equal(expectedValues.OrderBy(kvp => kvp.Key), constraint.Values.OrderBy(kvp => kvp.Key));
} }
[Fact] [Theory]
public void GetLink_InlineConstraints_Success() [InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_Success(bool hasHttpContext)
{ {
// Arrange // Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint( var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -700,7 +702,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" }, defaults: new { controller = "Home", action = "Index" },
constraints: new { }); constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint); var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(new { }); var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act // Act
var link = linkGenerator.GetLink( var link = linkGenerator.GetLink(
@ -732,8 +734,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.False(canGenerateLink); Assert.False(canGenerateLink);
} }
[Fact] [Theory]
public void GetLink_InlineConstraints_OptionalParameter_ValuePresent() [InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_OptionalParameter_ValuePresent(bool hasHttpContext)
{ {
// Arrange // Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint( var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -741,7 +745,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" }, defaults: new { controller = "Home", action = "Index" },
constraints: new { }); constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint); var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { }); var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act // Act
var link = linkGenerator.GetLink(httpContext, new { action = "Index", controller = "Home", id = 98 }); var link = linkGenerator.GetLink(httpContext, new { action = "Index", controller = "Home", id = 98 });
@ -789,8 +793,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.False(canGenerateLink); Assert.False(canGenerateLink);
} }
[Fact] [Theory]
public void GetLink_InlineConstraints_MultipleInlineConstraints() [InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_MultipleInlineConstraints(bool hasHttpContext)
{ {
// Arrange // Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint( var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -798,7 +804,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" }, defaults: new { controller = "Home", action = "Index" },
constraints: new { }); constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint); var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { }); var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act // Act
var link = linkGenerator.GetLink( var link = linkGenerator.GetLink(
@ -809,8 +815,10 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal("/Home/Index/14", link); Assert.Equal("/Home/Index/14", link);
} }
[Fact] [Theory]
public void GetLink_InlineConstraints_CompositeInlineConstraint_Fails() [InlineData(true)]
[InlineData(false)]
public void GetLink_InlineConstraints_CompositeInlineConstraint_Fails(bool hasHttpContext)
{ {
// Arrange // Arrange
var endpoint = EndpointFactory.CreateRouteEndpoint( var endpoint = EndpointFactory.CreateRouteEndpoint(
@ -818,7 +826,7 @@ namespace Microsoft.AspNetCore.Routing
defaults: new { controller = "Home", action = "Index" }, defaults: new { controller = "Home", action = "Index" },
constraints: new { }); constraints: new { });
var linkGenerator = CreateLinkGenerator(endpoint); var linkGenerator = CreateLinkGenerator(endpoint);
var httpContext = CreateHttpContext(ambientValues: new { }); var httpContext = hasHttpContext ? CreateHttpContext(new { }) : null;
// Act // Act
var canGenerateLink = linkGenerator.TryGetLink( var canGenerateLink = linkGenerator.TryGetLink(