diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/UrlMatches/IntegerMatch.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlMatches/IntegerMatch.cs index 770bbbd6d1..73b814731f 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/UrlMatches/IntegerMatch.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlMatches/IntegerMatch.cs @@ -3,6 +3,7 @@ using System; using System.Globalization; +using Microsoft.AspNetCore.Rewrite; namespace Microsoft.AspNetCore.Rewrite.Internal.UrlMatches { @@ -21,7 +22,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlMatches int compValue; if (!int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out compValue)) { - throw new FormatException("Syntax error for integers in comparison."); + throw new FormatException(Resources.Error_IntegerMatch_FormatExceptionMessage); } _value = compValue; _operation = operation; diff --git a/src/Microsoft.AspNetCore.Rewrite/Properties/AssemblyInfo.cs b/src/Microsoft.AspNetCore.Rewrite/Properties/AssemblyInfo.cs index 2dc4003a17..b56ddcf4e4 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Properties/AssemblyInfo.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Properties/AssemblyInfo.cs @@ -3,9 +3,11 @@ using System.Reflection; using System.Resources; +using System.Runtime.CompilerServices; [assembly: AssemblyMetadata("Serviceable", "True")] [assembly: NeutralResourcesLanguage("en-us")] [assembly: AssemblyCompany("Microsoft Corporation.")] [assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")] [assembly: AssemblyProduct("Microsoft ASP.NET Core")] +[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Rewrite.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] diff --git a/src/Microsoft.AspNetCore.Rewrite/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Rewrite/Properties/Resources.Designer.cs index 4ab56f4ce3..25261d8664 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Properties/Resources.Designer.cs @@ -138,6 +138,22 @@ namespace Microsoft.AspNetCore.Rewrite return string.Format(CultureInfo.CurrentCulture, GetString("Error_ModRewriteGeneralParseError"), p0); } + /// + /// Syntax error for integers in comparison. + /// + internal static string Error_IntegerMatch_FormatExceptionMessage + { + get { return GetString("Error_IntegerMatch_FormatExceptionMessage"); } + } + + /// + /// Syntax error for integers in comparison. + /// + internal static string FormatError_IntegerMatch_FormatExceptionMessage() + { + return GetString("Error_IntegerMatch_FormatExceptionMessage"); + } + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNetCore.Rewrite/Resources.resx b/src/Microsoft.AspNetCore.Rewrite/Resources.resx index cb7277289a..70a314f06b 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Resources.resx +++ b/src/Microsoft.AspNetCore.Rewrite/Resources.resx @@ -141,4 +141,7 @@ Could not parse the mod_rewrite file. Line number '{0}'. + + Syntax error for integers in comparison. + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/ExactMatchTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/ExactMatchTests.cs new file mode 100644 index 0000000000..9b85354adc --- /dev/null +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/ExactMatchTests.cs @@ -0,0 +1,27 @@ +// 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. + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite.Internal; +using Microsoft.AspNetCore.Rewrite.Internal.UrlActions; +using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches; +using Xunit; + +namespace Microsoft.AspNetCore.Rewrite.Tests.UrlMatches +{ + public class ExactMatchTests + { + [Theory] + [InlineDataAttribute(true,"string",false,"string",true)] + [InlineDataAttribute(true, "string", true, "string", false)] + [InlineDataAttribute(false, "STRING", false, "string",false)] + [InlineDataAttribute(false, "STRING", true, "string", true)] + public void ExactMatch_Case_Sensitivity_Negate_Tests(bool ignoreCase, string inputString, bool negate, string pattern, bool expectedResult) + { + var context = new RewriteContext { HttpContext = new DefaultHttpContext() }; + var Match = new ExactMatch(ignoreCase, inputString, negate); + var matchResults = Match.Evaluate(pattern, context); + Assert.Equal(matchResults.Success, expectedResult); + } + } +} diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/IntegerMatchTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/IntegerMatchTests.cs new file mode 100644 index 0000000000..bb7408d9f6 --- /dev/null +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/IntegerMatchTests.cs @@ -0,0 +1,41 @@ +// 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. + +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite; +using Microsoft.AspNetCore.Rewrite.Internal; +using Microsoft.AspNetCore.Rewrite.Internal.UrlActions; +using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches; +using Xunit; + +namespace Microsoft.AspNetCore.Rewrite.Tests.UrlMatches +{ + public class IntegerMatchTests + { + [Fact] + public void IntegerMatch_Constructor_Integer_Parse_Excetion() + { + var ex = Assert.Throws(() => new IntegerMatch("Not an int", IntegerOperationType.Equal)); + Assert.Equal(ex.Message, Resources.Error_IntegerMatch_FormatExceptionMessage); + } + + [Theory] + [InlineData(1,IntegerOperationType.Equal,"1",true)] + [InlineData(1, IntegerOperationType.NotEqual, "2", true)] + [InlineData(2, IntegerOperationType.Less, "1", true)] + [InlineData(1, IntegerOperationType.LessEqual, "2", false)] + [InlineData(1, IntegerOperationType.Greater, "2", true)] + [InlineData(2, IntegerOperationType.GreaterEqual, "1", false)] + [InlineData(1, IntegerOperationType.Equal, "Not an int", false)] + [InlineData(1, IntegerOperationType.Equal, "", false)] + [InlineData(1, IntegerOperationType.Equal, "2147483648", false)] + public void IntegerMatch_Evaluation_Cases_Tests(int value,IntegerOperationType operation, string input,bool expectedResult) + { + var context = new RewriteContext { HttpContext = new DefaultHttpContext() }; + var integerMatch = new IntegerMatch(value, operation); + var matchResult = integerMatch.Evaluate(input, context); + Assert.Equal(matchResult.Success, expectedResult); + } + } +} diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/StringMatchTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/StringMatchTests.cs new file mode 100644 index 0000000000..0cd62e1f91 --- /dev/null +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlMatches/StringMatchTests.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +namespace Microsoft.AspNetCore.Rewrite.Tests.UrlMatches +{ + public class StringMatchTests + { + [Theory] + [InlineData("hi", StringOperationType.Equal,true,"hi",true)] + [InlineData("a", StringOperationType.Greater, true, "b", true)] + [InlineData("a", StringOperationType.GreaterEqual, true, "b", true)] + [InlineData("b", StringOperationType.Less,true, "a", true)] + [InlineData("b", StringOperationType.LessEqual, true, "a", true)] + [InlineData("", StringOperationType.Equal, true, "", true)] + [InlineData(null, StringOperationType.Equal, true, null, true)] + public void StringMatch_Evaluation_Check_Cases(string value, StringOperationType operation, bool ignoreCase, string input, bool expectedResult) + { + var context = new RewriteContext { HttpContext = new DefaultHttpContext() }; + var stringMatch = new StringMatch(value, operation, ignoreCase); + var matchResult = stringMatch.Evaluate(input, context); + Assert.Equal(matchResult.Success, expectedResult); + } + } +} diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/project.json b/test/Microsoft.AspNetCore.Rewrite.Tests/project.json index 904880d014..53270ebe77 100644 --- a/test/Microsoft.AspNetCore.Rewrite.Tests/project.json +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/project.json @@ -1,7 +1,8 @@ { "version": "1.1.0-*", "buildOptions": { - "warningsAsErrors": true + "warningsAsErrors": true, + "keyFile": "../../tools/Key.snk" }, "dependencies": { "dotnet-test-xunit": "2.2.0-*", @@ -12,7 +13,12 @@ }, "frameworks": { "netcoreapp1.0": { + "imports": "dnxcore50", "dependencies": { + "Microsoft.CodeCoverage": { + "type": "build", + "version": "1.0.1" + }, "Microsoft.NETCore.App": { "version": "1.0.0-*", "type": "platform"