mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#18533: Fixing RouteValueDictionary comparisons
Work Item: 18533 --HG-- branch : 1.x
This commit is contained in:
@@ -309,6 +309,7 @@
|
|||||||
<Compile Include="UI\Navigation\MenuItemComparerTests.cs" />
|
<Compile Include="UI\Navigation\MenuItemComparerTests.cs" />
|
||||||
<Compile Include="UI\Navigation\NavigationManagerTests.cs" />
|
<Compile Include="UI\Navigation\NavigationManagerTests.cs" />
|
||||||
<Compile Include="UI\Navigation\PositionComparerTests.cs" />
|
<Compile Include="UI\Navigation\PositionComparerTests.cs" />
|
||||||
|
<Compile Include="Utility\Extensions\RouteValueDictionaryExtensionsTests.cs" />
|
||||||
<Compile Include="Utility\Extensions\StringExtensionsTests.cs" />
|
<Compile Include="Utility\Extensions\StringExtensionsTests.cs" />
|
||||||
<Compile Include="Utility\ReflectOnTests.cs" />
|
<Compile Include="Utility\ReflectOnTests.cs" />
|
||||||
<Compile Include="Utility\ReflectTests.cs" />
|
<Compile Include="Utility\ReflectTests.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Web.Routing;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Orchard.Utility.Extensions;
|
||||||
|
|
||||||
|
namespace Orchard.Tests.Utility.Extensions {
|
||||||
|
[TestFixture]
|
||||||
|
class RouteValueDictionaryExtensionsTests {
|
||||||
|
[Test]
|
||||||
|
public void IdenticalRouteValueDictionariesShouldMatch() {
|
||||||
|
Assert.IsTrue(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
|
||||||
|
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CasedRouteValueDictionariesShouldMatch() {
|
||||||
|
Assert.IsTrue(new RouteValueDictionary { { "controller", "foo" }, { "action", "BAR" } }
|
||||||
|
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RouteValueDictionariesWithDifferentNumbersOfValuesShouldNotMatch() {
|
||||||
|
Assert.IsFalse(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
|
||||||
|
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" }, { "area", "baz" } }));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RouteValueDictionariesWithDifferentValuesShouldMatch() {
|
||||||
|
Assert.IsFalse(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
|
||||||
|
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "baz" } }));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,8 +35,21 @@ namespace Orchard.Utility.Extensions {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// keys can be different in case
|
var bools = x.Join(y,
|
||||||
return x.Keys.All(key => x[key].ToString().Equals(y[key].ToString(), StringComparison.OrdinalIgnoreCase));
|
kv1 => kv1.Key.ToLowerInvariant(),
|
||||||
|
kv2 => kv2.Key.ToLowerInvariant(),
|
||||||
|
(kv1, kv2) => StringMatch(kv1.Value, kv2.Value)
|
||||||
|
).ToArray();
|
||||||
|
|
||||||
|
return bools.All(b => b) && bools.Count() == x.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool StringMatch(object value1, object value2) {
|
||||||
|
return string.Equals(
|
||||||
|
Convert.ToString(value1, CultureInfo.InvariantCulture),
|
||||||
|
Convert.ToString(value2, CultureInfo.InvariantCulture),
|
||||||
|
StringComparison.InvariantCultureIgnoreCase
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RouteValueDictionary ToRouteValueDictionary(this IEnumerable<KeyValuePair<string, string>> routeValues) {
|
public static RouteValueDictionary ToRouteValueDictionary(this IEnumerable<KeyValuePair<string, string>> routeValues) {
|
||||||
|
|||||||
Reference in New Issue
Block a user