Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit 70402c8

Browse files
committed
Adding RegexInlineRouteConstraint
1 parent b9baae4 commit 70402c8

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.Routing.Constraints
5+
{
6+
/// <summary>
7+
/// Represents a regex constraint which can be used as an inlineConstraint.
8+
/// </summary>
9+
public class RegexInlineRouteConstraint : RegexRouteConstraint
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="RegexInlineRouteConstraint" /> class.
13+
/// </summary>
14+
/// <param name="regexPattern">The regular expression pattern to match.</param>
15+
public RegexInlineRouteConstraint([NotNull] string regexPattern)
16+
: base(regexPattern)
17+
{
18+
}
19+
}
20+
}

src/Microsoft.AspNet.Routing/Microsoft.AspNet.Routing.kproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<PropertyGroup>
1717
<SchemaVersion>2.0</SchemaVersion>
1818
</PropertyGroup>
19+
<Compile Include="Constraints\RegexInlineRouteConstraint.cs" />
1920
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
2021
</Project>

src/Microsoft.AspNet.Routing/RouteOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ private static IDictionary<string, Type> GetDefaultConstraintMap()
5656

5757
// Regex-based constraints
5858
{ "alpha", typeof(AlphaRouteConstraint) },
59+
{ "regex", typeof(RegexInlineRouteConstraint) },
5960
};
6061
}
6162
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#if NET45
5+
6+
using System.Globalization;
7+
using System.Threading;
8+
using Microsoft.AspNet.Http;
9+
using Microsoft.AspNet.Routing.Constraints;
10+
using Moq;
11+
using Xunit;
12+
13+
namespace Microsoft.AspNet.Routing.Tests
14+
{
15+
public class RegexInlineRouteConstraintTests
16+
{
17+
[Theory]
18+
[InlineData("abc", "abc", true)] // simple match
19+
[InlineData("Abc", "abc", true)] // case insensitive match
20+
[InlineData("Abc ", "abc", true)] // Extra space on input match (because we don't add ^({0})$
21+
[InlineData("Abcd", "abc", true)] // Extra char
22+
[InlineData("^Abcd", "abc", true)] // Extra special char
23+
[InlineData("Abc", " abc", false)] // Missing char
24+
public void RegexInlineConstraintBuildRegexVerbatimFromInput(string routeValue,
25+
string constraintValue,
26+
bool shouldMatch)
27+
{
28+
// Arrange
29+
var constraint = new RegexInlineRouteConstraint(constraintValue);
30+
var values = new RouteValueDictionary(new {controller = routeValue});
31+
32+
// Assert
33+
Assert.Equal(shouldMatch, EasyMatch(constraint, "controller", values));
34+
}
35+
36+
[Fact]
37+
public void RegexInlineConstraint_FailsIfKeyIsNotFoundInRouteValues()
38+
{
39+
// Arrange
40+
var constraint = new RegexInlineRouteConstraint("^abc$");
41+
var values = new RouteValueDictionary(new { action = "abc" });
42+
43+
// Assert
44+
Assert.False(EasyMatch(constraint, "controller", values));
45+
}
46+
47+
[Fact]
48+
public void RegexInlineConstraint_IsCultureInsensitive()
49+
{
50+
// Arrange
51+
var constraint = new RegexInlineRouteConstraint("^([a-z]+)$");
52+
var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I
53+
54+
var currentThread = Thread.CurrentThread;
55+
var backupCulture = currentThread.CurrentCulture;
56+
57+
bool matchInTurkish;
58+
bool matchInUsEnglish;
59+
60+
// Act
61+
try
62+
{
63+
currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture
64+
matchInTurkish = EasyMatch(constraint, "controller", values);
65+
66+
currentThread.CurrentCulture = new CultureInfo("en-US");
67+
matchInUsEnglish = EasyMatch(constraint, "controller", values);
68+
}
69+
finally
70+
{
71+
currentThread.CurrentCulture = backupCulture;
72+
}
73+
74+
// Assert
75+
Assert.False(matchInUsEnglish); // this just verifies the test
76+
Assert.False(matchInTurkish);
77+
}
78+
79+
private static bool EasyMatch(IRouteConstraint constraint,
80+
string routeKey,
81+
RouteValueDictionary values)
82+
{
83+
return constraint.Match(httpContext: new Mock<HttpContext>().Object,
84+
route: new Mock<IRouter>().Object,
85+
routeKey: routeKey,
86+
values: values,
87+
routeDirection: RouteDirection.IncomingRequest);
88+
}
89+
}
90+
}
91+
#endif

test/Microsoft.AspNet.Routing.Tests/Constraints/RegexConstraintTests.cs renamed to test/Microsoft.AspNet.Routing.Tests/Constraints/RegexRouteConstraintTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.AspNet.Routing.Tests
1515
{
16-
public class RegexConstraintTests
16+
public class RegexRouteConstraintTests
1717
{
1818
[Theory]
1919
[InlineData("abc", "abc", true)] // simple match

test/Microsoft.AspNet.Routing.Tests/DefaultInlineConstraintResolverTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void ResolveConstraint_IntConstraintWithArgument_Throws()
4242
" with the following number of parameters: 1.",
4343
ex.Message);
4444
}
45+
4546
[Fact]
4647
public void ResolveConstraint_AlphaConstraint()
4748
{
@@ -52,6 +53,16 @@ public void ResolveConstraint_AlphaConstraint()
5253
Assert.IsType<AlphaRouteConstraint>(constraint);
5354
}
5455

56+
[Fact]
57+
public void ResolveConstraint_RegexInlineConstraint_WithAComma_PassesAsASingleArgument()
58+
{
59+
// Arrange & Act
60+
var constraint = _constraintResolver.ResolveConstraint("regex(ab,1)");
61+
62+
// Assert
63+
Assert.IsType<RegexInlineRouteConstraint>(constraint);
64+
}
65+
5566
[Fact]
5667
public void ResolveConstraint_BoolConstraint()
5768
{

test/Microsoft.AspNet.Routing.Tests/Microsoft.AspNet.Routing.Tests.kproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<PropertyGroup>
1717
<SchemaVersion>2.0</SchemaVersion>
1818
</PropertyGroup>
19+
1920
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
2021
</Project>

0 commit comments

Comments
 (0)