Adding RegexInlineRouteConstraint
This commit is contained in:
parent
b9baae4693
commit
70402c8f2c
|
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Routing.Constraints
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a regex constraint which can be used as an inlineConstraint.
|
||||||
|
/// </summary>
|
||||||
|
public class RegexInlineRouteConstraint : RegexRouteConstraint
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RegexInlineRouteConstraint" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="regexPattern">The regular expression pattern to match.</param>
|
||||||
|
public RegexInlineRouteConstraint([NotNull] string regexPattern)
|
||||||
|
: base(regexPattern)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,5 +16,6 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<SchemaVersion>2.0</SchemaVersion>
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<Compile Include="Constraints\RegexInlineRouteConstraint.cs" />
|
||||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -56,6 +56,7 @@ namespace Microsoft.AspNet.Routing
|
||||||
|
|
||||||
// Regex-based constraints
|
// Regex-based constraints
|
||||||
{ "alpha", typeof(AlphaRouteConstraint) },
|
{ "alpha", typeof(AlphaRouteConstraint) },
|
||||||
|
{ "regex", typeof(RegexInlineRouteConstraint) },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,91 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
#if NET45
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Threading;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Routing.Constraints;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Routing.Tests
|
||||||
|
{
|
||||||
|
public class RegexInlineRouteConstraintTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData("abc", "abc", true)] // simple match
|
||||||
|
[InlineData("Abc", "abc", true)] // case insensitive match
|
||||||
|
[InlineData("Abc ", "abc", true)] // Extra space on input match (because we don't add ^({0})$
|
||||||
|
[InlineData("Abcd", "abc", true)] // Extra char
|
||||||
|
[InlineData("^Abcd", "abc", true)] // Extra special char
|
||||||
|
[InlineData("Abc", " abc", false)] // Missing char
|
||||||
|
public void RegexInlineConstraintBuildRegexVerbatimFromInput(string routeValue,
|
||||||
|
string constraintValue,
|
||||||
|
bool shouldMatch)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var constraint = new RegexInlineRouteConstraint(constraintValue);
|
||||||
|
var values = new RouteValueDictionary(new {controller = routeValue});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(shouldMatch, EasyMatch(constraint, "controller", values));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RegexInlineConstraint_FailsIfKeyIsNotFoundInRouteValues()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var constraint = new RegexInlineRouteConstraint("^abc$");
|
||||||
|
var values = new RouteValueDictionary(new { action = "abc" });
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.False(EasyMatch(constraint, "controller", values));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RegexInlineConstraint_IsCultureInsensitive()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var constraint = new RegexInlineRouteConstraint("^([a-z]+)$");
|
||||||
|
var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I
|
||||||
|
|
||||||
|
var currentThread = Thread.CurrentThread;
|
||||||
|
var backupCulture = currentThread.CurrentCulture;
|
||||||
|
|
||||||
|
bool matchInTurkish;
|
||||||
|
bool matchInUsEnglish;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
try
|
||||||
|
{
|
||||||
|
currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture
|
||||||
|
matchInTurkish = EasyMatch(constraint, "controller", values);
|
||||||
|
|
||||||
|
currentThread.CurrentCulture = new CultureInfo("en-US");
|
||||||
|
matchInUsEnglish = EasyMatch(constraint, "controller", values);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
currentThread.CurrentCulture = backupCulture;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.False(matchInUsEnglish); // this just verifies the test
|
||||||
|
Assert.False(matchInTurkish);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool EasyMatch(IRouteConstraint constraint,
|
||||||
|
string routeKey,
|
||||||
|
RouteValueDictionary values)
|
||||||
|
{
|
||||||
|
return constraint.Match(httpContext: new Mock<HttpContext>().Object,
|
||||||
|
route: new Mock<IRouter>().Object,
|
||||||
|
routeKey: routeKey,
|
||||||
|
values: values,
|
||||||
|
routeDirection: RouteDirection.IncomingRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
@ -13,7 +13,7 @@ using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Routing.Tests
|
namespace Microsoft.AspNet.Routing.Tests
|
||||||
{
|
{
|
||||||
public class RegexConstraintTests
|
public class RegexRouteConstraintTests
|
||||||
{
|
{
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("abc", "abc", true)] // simple match
|
[InlineData("abc", "abc", true)] // simple match
|
||||||
|
|
@ -42,6 +42,7 @@ namespace Microsoft.AspNet.Routing.Tests
|
||||||
" with the following number of parameters: 1.",
|
" with the following number of parameters: 1.",
|
||||||
ex.Message);
|
ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ResolveConstraint_AlphaConstraint()
|
public void ResolveConstraint_AlphaConstraint()
|
||||||
{
|
{
|
||||||
|
|
@ -52,6 +53,16 @@ namespace Microsoft.AspNet.Routing.Tests
|
||||||
Assert.IsType<AlphaRouteConstraint>(constraint);
|
Assert.IsType<AlphaRouteConstraint>(constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ResolveConstraint_RegexInlineConstraint_WithAComma_PassesAsASingleArgument()
|
||||||
|
{
|
||||||
|
// Arrange & Act
|
||||||
|
var constraint = _constraintResolver.ResolveConstraint("regex(ab,1)");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsType<RegexInlineRouteConstraint>(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ResolveConstraint_BoolConstraint()
|
public void ResolveConstraint_BoolConstraint()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -16,5 +16,6 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<SchemaVersion>2.0</SchemaVersion>
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||||
</Project>
|
</Project>
|
||||||
Loading…
Reference in New Issue