Updating tests to use moq.netcore

This commit is contained in:
Pranav K 2015-11-12 17:50:10 -08:00
parent 825f82d7e0
commit cad81fa608
36 changed files with 182 additions and 271 deletions

View File

@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Routing.Constraints
HttpContext httpContext, HttpContext httpContext,
IRouter route, IRouter route,
string routeKey, string routeKey,
IDictionary<string, object> routeValues, IDictionary<string, object> values,
RouteDirection routeDirection) RouteDirection routeDirection)
{ {
if (httpContext == null) if (httpContext == null)
@ -60,14 +60,14 @@ namespace Microsoft.AspNet.Routing.Constraints
throw new ArgumentNullException(nameof(routeKey)); throw new ArgumentNullException(nameof(routeKey));
} }
if (routeValues == null) if (values == null)
{ {
throw new ArgumentNullException(nameof(routeValues)); throw new ArgumentNullException(nameof(values));
} }
object routeValue; object routeValue;
if (routeValues.TryGetValue(routeKey, out routeValue) if (values.TryGetValue(routeKey, out routeValue)
&& routeValue != null) && routeValue != null)
{ {
var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture); var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture);

View File

@ -20,10 +20,11 @@ namespace Microsoft.AspNet.Routing
/// <param name="values">A dictionary that contains the parameters for the URL.</param> /// <param name="values">A dictionary that contains the parameters for the URL.</param>
/// <param name="routeDirection">An object that indicates whether the constraint check is being performed when an incoming request is being handled or when a URL is being generated.</param> /// <param name="routeDirection">An object that indicates whether the constraint check is being performed when an incoming request is being handled or when a URL is being generated.</param>
/// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns>
bool Match(HttpContext httpContext, bool Match(
IRouter route, HttpContext httpContext,
string routeKey, IRouter route,
IDictionary<string, object> values, string routeKey,
RouteDirection routeDirection); IDictionary<string, object> values,
RouteDirection routeDirection);
} }
} }

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -2048,5 +2046,3 @@ namespace Microsoft.AspNet.Routing.Tree
} }
} }
} }
#endif

View File

@ -3,18 +3,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
#if DNX451
using Microsoft.AspNet.Routing.Logging; using Microsoft.AspNet.Routing.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Moq; using Moq;
#endif
using Xunit; using Xunit;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
public class ConstraintMatcherTest public class ConstraintMatcherTest
{ {
#if DNX451
private const string _name = "name"; private const string _name = "name";
[Fact] [Fact]
@ -213,7 +210,6 @@ namespace Microsoft.AspNet.Routing
logger: logger); logger: logger);
return sink; return sink;
} }
#endif
private class PassConstraint : IRouteConstraint private class PassConstraint : IRouteConstraint
{ {

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -32,5 +30,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -40,5 +38,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -19,9 +17,10 @@ namespace Microsoft.AspNet.Routing.Tests
[InlineData(true, false, false)] [InlineData(true, false, false)]
[InlineData(false, true, false)] [InlineData(false, true, false)]
[InlineData(false, false, false)] [InlineData(false, false, false)]
public void CompositeRouteConstraint_Match_CallsMatchOnInnerConstraints(bool inner1Result, public void CompositeRouteConstraint_Match_CallsMatchOnInnerConstraints(
bool inner2Result, bool inner1Result,
bool expected) bool inner2Result,
bool expected)
{ {
// Arrange // Arrange
var inner1 = MockConstraintWithResult(inner1Result); var inner1 = MockConstraintWithResult(inner1Result);
@ -36,11 +35,12 @@ namespace Microsoft.AspNet.Routing.Tests
} }
static Expression<Func<IRouteConstraint, bool>> ConstraintMatchMethodExpression = static Expression<Func<IRouteConstraint, bool>> ConstraintMatchMethodExpression =
c => c.Match(It.IsAny<HttpContext>(), c => c.Match(
It.IsAny<IRouter>(), It.IsAny<HttpContext>(),
It.IsAny<string>(), It.IsAny<IRouter>(),
It.IsAny<Dictionary<string, object>>(), It.IsAny<string>(),
It.IsAny<RouteDirection>()); It.IsAny<Dictionary<string, object>>(),
It.IsAny<RouteDirection>());
private static Mock<IRouteConstraint> MockConstraintWithResult(bool result) private static Mock<IRouteConstraint> MockConstraintWithResult(bool result)
{ {
@ -52,5 +52,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
@ -30,5 +28,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
@ -54,5 +52,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -43,5 +41,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -30,5 +28,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -29,5 +27,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
@ -37,5 +35,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -29,5 +27,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
@ -53,10 +50,11 @@ namespace Microsoft.AspNet.Routing.Tests
var expectedMessage = "Value must be greater than or equal to 0."; var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new LengthRouteConstraint(-1), ExceptionAssert.ThrowsArgumentOutOfRange(
"length", () => new LengthRouteConstraint(-1),
expectedMessage, "length",
-1); expectedMessage,
-1);
} }
[Fact] [Fact]
@ -66,10 +64,11 @@ namespace Microsoft.AspNet.Routing.Tests
var expectedMessage = "Value must be greater than or equal to 0."; var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new LengthRouteConstraint(-1, 3), ExceptionAssert.ThrowsArgumentOutOfRange(
"minLength", () => new LengthRouteConstraint(-1, 3),
expectedMessage, "minLength",
-1); expectedMessage,
-1);
} }
[Fact] [Fact]
@ -79,25 +78,26 @@ namespace Microsoft.AspNet.Routing.Tests
var expectedMessage = "Value must be greater than or equal to 0."; var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new LengthRouteConstraint(0, -1), ExceptionAssert.ThrowsArgumentOutOfRange(
"maxLength", () => new LengthRouteConstraint(0, -1),
expectedMessage, "maxLength",
-1); expectedMessage,
-1);
} }
[Fact] [Fact]
public void LengthRouteConstraint_MinGreaterThanMax_Throws() public void LengthRouteConstraint_MinGreaterThanMax_Throws()
{ {
// Arrange // Arrange
var expectedMessage = "The value for argument 'minLength' should be less than or equal to the " + "value for the argument 'maxLength'."; var expectedMessage = "The value for argument 'minLength' should be less than or equal to the " +
"value for the argument 'maxLength'.";
// Arrange Act & Assert // Arrange Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new LengthRouteConstraint(3, 2), ExceptionAssert.ThrowsArgumentOutOfRange(
"minLength", () => new LengthRouteConstraint(3, 2),
expectedMessage, "minLength",
3); expectedMessage,
3);
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -31,5 +29,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
@ -36,12 +33,11 @@ namespace Microsoft.AspNet.Routing.Tests
var expectedMessage = "Value must be greater than or equal to 0."; var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new MaxLengthRouteConstraint(-1), ExceptionAssert.ThrowsArgumentOutOfRange(
"maxLength", () => new MaxLengthRouteConstraint(-1),
expectedMessage, "maxLength",
-1); expectedMessage,
-1);
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -27,5 +25,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
@ -36,12 +33,11 @@ namespace Microsoft.AspNet.Routing.Tests
var expectedMessage = "Value must be greater than or equal to 0."; var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new MinLengthRouteConstraint(-1), ExceptionAssert.ThrowsArgumentOutOfRange(
"minLength", () => new MinLengthRouteConstraint(-1),
expectedMessage, "minLength",
-1); expectedMessage,
-1);
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Xunit; using Xunit;
@ -27,5 +25,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
@ -41,12 +38,11 @@ namespace Microsoft.AspNet.Routing.Tests
"argument 'max'."; "argument 'max'.";
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(() => new RangeRouteConstraint(3, 2), ExceptionAssert.ThrowsArgumentOutOfRange(
"min", () => new RangeRouteConstraint(3, 2),
expectedMessage, "min",
3); expectedMessage,
3);
} }
} }
} }
#endif

View File

@ -1,10 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System.Globalization;
using System.Threading;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
@ -22,16 +18,25 @@ namespace Microsoft.AspNet.Routing.Tests
[InlineData("Abcd", "abc", true)] // Extra char [InlineData("Abcd", "abc", true)] // Extra char
[InlineData("^Abcd", "abc", true)] // Extra special char [InlineData("^Abcd", "abc", true)] // Extra special char
[InlineData("Abc", " abc", false)] // Missing char [InlineData("Abc", " abc", false)] // Missing char
public void RegexInlineConstraintBuildRegexVerbatimFromInput(string routeValue, public void RegexInlineConstraintBuildRegexVerbatimFromInput(
string constraintValue, string routeValue,
bool shouldMatch) string constraintValue,
bool shouldMatch)
{ {
// Arrange // Arrange
var constraint = new RegexInlineRouteConstraint(constraintValue); var constraint = new RegexInlineRouteConstraint(constraintValue);
var values = new RouteValueDictionary(new {controller = routeValue}); var values = new RouteValueDictionary(new { controller = routeValue });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.Equal(shouldMatch, EasyMatch(constraint, "controller", values)); Assert.Equal(shouldMatch, match);
} }
[Fact] [Fact]
@ -41,12 +46,22 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = new RegexInlineRouteConstraint("^abc$"); var constraint = new RegexInlineRouteConstraint("^abc$");
var values = new RouteValueDictionary(new { action = "abc" }); var values = new RouteValueDictionary(new { action = "abc" });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.False(EasyMatch(constraint, "controller", values)); Assert.False(match);
} }
[Fact] [Theory]
public void RegexInlineConstraint_IsCultureInsensitive() [InlineData("tr-TR")]
[InlineData("en-US")]
public void RegexInlineConstraint_IsCultureInsensitive(string culture)
{ {
if (TestPlatformHelper.IsMono) if (TestPlatformHelper.IsMono)
{ {
@ -59,41 +74,19 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = new RegexInlineRouteConstraint("^([a-z]+)$"); var constraint = new RegexInlineRouteConstraint("^([a-z]+)$");
var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I
var currentThread = Thread.CurrentThread; using (new CultureReplacer(culture))
var backupCulture = currentThread.CurrentCulture;
bool matchInTurkish;
bool matchInUsEnglish;
// Act
try
{ {
currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture // Act
matchInTurkish = EasyMatch(constraint, "controller", values); var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
currentThread.CurrentCulture = new CultureInfo("en-US"); // Assert
matchInUsEnglish = EasyMatch(constraint, "controller", values); Assert.False(match);
} }
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

View File

@ -1,11 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
@ -26,16 +22,25 @@ namespace Microsoft.AspNet.Routing.Tests
[InlineData("123-456-2334", @"^\d{3}-\d{3}-\d{4}$", true)] // ssn [InlineData("123-456-2334", @"^\d{3}-\d{3}-\d{4}$", true)] // ssn
[InlineData(@"12/4/2013", @"^\d{1,2}\/\d{1,2}\/\d{4}$", true)] // date [InlineData(@"12/4/2013", @"^\d{1,2}\/\d{1,2}\/\d{4}$", true)] // date
[InlineData(@"abc@def.com", @"^\w+[\w\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$", true)] // email [InlineData(@"abc@def.com", @"^\w+[\w\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$", true)] // email
public void RegexConstraintBuildRegexVerbatimFromInput(string routeValue, public void RegexConstraintBuildRegexVerbatimFromInput(
string constraintValue, string routeValue,
bool shouldMatch) string constraintValue,
bool shouldMatch)
{ {
// Arrange // Arrange
var constraint = new RegexRouteConstraint(constraintValue); var constraint = new RegexRouteConstraint(constraintValue);
var values = new RouteValueDictionary(new {controller = routeValue}); var values = new RouteValueDictionary(new { controller = routeValue });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.Equal(shouldMatch, EasyMatch(constraint, "controller", values)); Assert.Equal(shouldMatch, match);
} }
[Fact] [Fact]
@ -43,10 +48,18 @@ namespace Microsoft.AspNet.Routing.Tests
{ {
// Arrange // Arrange
var constraint = new RegexRouteConstraint(new Regex("^abc$")); var constraint = new RegexRouteConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { controller = "abc"}); var values = new RouteValueDictionary(new { controller = "abc" });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.True(EasyMatch(constraint, "controller", values)); Assert.True(match);
} }
[Fact] [Fact]
@ -56,8 +69,16 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = new RegexRouteConstraint(new Regex("^abc$")); var constraint = new RegexRouteConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { controller = "Abc" }); var values = new RouteValueDictionary(new { controller = "Abc" });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.False(EasyMatch(constraint, "controller", values)); Assert.False(match);
} }
[Fact] [Fact]
@ -67,12 +88,22 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = new RegexRouteConstraint(new Regex("^abc$")); var constraint = new RegexRouteConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { action = "abc" }); var values = new RouteValueDictionary(new { action = "abc" });
// Act
var match = constraint.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
// Assert // Assert
Assert.False(EasyMatch(constraint, "controller", values)); Assert.False(match);
} }
[Fact] [Theory]
public void RegexConstraintIsCultureInsensitiveWhenConstructedWithString() [InlineData("tr-TR")]
[InlineData("en-US")]
public void RegexConstraintIsCultureInsensitiveWhenConstructedWithString(string culture)
{ {
if (TestPlatformHelper.IsMono) if (TestPlatformHelper.IsMono)
{ {
@ -85,41 +116,19 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = new RegexRouteConstraint("^([a-z]+)$"); var constraint = new RegexRouteConstraint("^([a-z]+)$");
var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I
var currentThread = Thread.CurrentThread; using (new CultureReplacer(culture))
var backupCulture = currentThread.CurrentCulture;
bool matchInTurkish;
bool matchInUsEnglish;
// Act
try
{ {
currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture // Act
matchInTurkish = EasyMatch(constraint, "controller", values); var match = constraint.Match(
httpContext: new Mock<HttpContext>().Object,
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: values,
routeDirection: RouteDirection.IncomingRequest);
currentThread.CurrentCulture = new CultureInfo("en-US"); // Assert
matchInUsEnglish = EasyMatch(constraint, "controller", values); Assert.False(match);
} }
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

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing.Constraints; using Microsoft.AspNet.Routing.Constraints;
using Moq; using Moq;
@ -94,5 +91,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
@ -370,4 +369,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -3,12 +3,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing.Template; using Microsoft.AspNet.Routing.Template;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
using Xunit; using Xunit;
using System.Linq;
namespace Microsoft.AspNet.Routing.Tests namespace Microsoft.AspNet.Routing.Tests
{ {

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -904,4 +903,3 @@ namespace Microsoft.AspNet.Routing
} }
} }
} }
#endif

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
@ -189,4 +188,3 @@ namespace Microsoft.AspNet.Routing
} }
} }
} }
#endif

View File

@ -35,11 +35,12 @@ namespace Microsoft.AspNet.Routing.Tests
} }
public string Pattern { get; private set; } public string Pattern { get; private set; }
public bool Match(HttpContext httpContext, public bool Match(
IRouter route, HttpContext httpContext,
string routeKey, IRouter route,
IDictionary<string, object> values, string routeKey,
RouteDirection routeDirection) IDictionary<string, object> values,
RouteDirection routeDirection)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using Microsoft.AspNet.Routing;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
using Moq; using Moq;
using Xunit; using Xunit;
@ -121,4 +119,3 @@ namespace Microsoft.AspNet.Routing.Template
} }
} }
} }
#endif

View File

@ -1,20 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
using Moq;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Routing.Template.Tests namespace Microsoft.AspNet.Routing.Template.Tests
{ {
public class TemplateBinderTests public class TemplateBinderTests
{ {
private static IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver(); private readonly IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver();
public static IEnumerable<object[]> EmptyAndNullDefaultValues public static IEnumerable<object[]> EmptyAndNullDefaultValues
{ {
@ -1212,4 +1210,3 @@ namespace Microsoft.AspNet.Routing.Template.Tests
} }
} }
} }
#endif

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -916,4 +915,3 @@ namespace Microsoft.AspNet.Routing.Template.Tests
} }
} }
} }
#endif

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -1972,4 +1971,3 @@ namespace Microsoft.AspNet.Routing.Template
} }
} }
} }
#endif

View File

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using System; using System;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -130,5 +128,3 @@ namespace Microsoft.AspNet.Routing.Tests
} }
} }
} }
#endif

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if DNX451
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Moq; using Moq;
using Xunit; using Xunit;
@ -65,4 +64,3 @@ namespace Microsoft.AspNet.Routing
} }
} }
} }
#endif

View File

@ -1,24 +1,28 @@
{ {
"compilationOptions": { "compilationOptions": {
"warningsAsErrors": true "warningsAsErrors": true
},
"dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.AspNet.Routing": "1.0.0-*",
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.Logging.Testing": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"frameworks": {
"dnxcore50": {
"dependencies": {
"moq.netcore": "4.4.0-beta8"
}
}, },
"dependencies": { "dnx451": {
"Microsoft.AspNet.Http": "1.0.0-*", "dependencies": {
"Microsoft.AspNet.Routing": "1.0.0-*", "Moq": "4.2.1312.1622"
"Microsoft.AspNet.Testing": "1.0.0-*", }
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.Logging.Testing": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"frameworks": {
"dnxcore50": { },
"dnx451": {
"dependencies": {
"Moq": "4.2.1312.1622"
}
}
},
"commands": {
"test": "xunit.runner.aspnet"
} }
},
"commands": {
"test": "xunit.runner.aspnet"
}
} }