Implement PatternAsFile for StringMatcher (#651)

This commit is contained in:
Stef Heyenrath
2021-10-15 08:54:12 +02:00
committed by GitHub
parent a2a581c84b
commit 57cc616aa3
43 changed files with 817 additions and 491 deletions

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using WireMock.Handlers; using WireMock.Handlers;
@@ -80,6 +80,12 @@ namespace WireMock.Net.ConsoleApplication
return File.ReadAllBytes(AdjustPath(path)); return File.ReadAllBytes(AdjustPath(path));
} }
/// <inheritdoc cref="IFileSystemHandler.ReadFileAsString"/>
public string ReadFileAsString(string path)
{
return File.ReadAllText(path);
}
/// <summary> /// <summary>
/// Adjusts the path to the MappingFolder. /// Adjusts the path to the MappingFolder.
/// </summary> /// </summary>

View File

@@ -36,6 +36,9 @@
<ApplicationIcon>..\..\resources\WireMock.Net-Logo.ico</ApplicationIcon> <ApplicationIcon>..\..\resources\WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AnyOf, Version=0.2.0.0, Culture=neutral, PublicKeyToken=b35e6abbb527c6b1, processorArchitecture=MSIL">
<HintPath>..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll</HintPath>
</Reference>
<Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL"> <Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
<HintPath>..\..\packages\Handlebars.Net.2.0.4\lib\net452\Handlebars.dll</HintPath> <HintPath>..\..\packages\Handlebars.Net.2.0.4\lib\net452\Handlebars.dll</HintPath>
</Reference> </Reference>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AnyOf" version="0.2.0" targetFramework="net452" />
<package id="Handlebars.Net" version="2.0.4" targetFramework="net452" /> <package id="Handlebars.Net" version="2.0.4" targetFramework="net452" />
<package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net452" /> <package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net452" />
<package id="Handlebars.Net.Helpers.Core" version="2.1.1-preview-01" targetFramework="net452" /> <package id="Handlebars.Net.Helpers.Core" version="2.1.1-preview-01" targetFramework="net452" />

View File

@@ -35,6 +35,9 @@
<StartupObject>WireMock.Net.ConsoleApplication.Program</StartupObject> <StartupObject>WireMock.Net.ConsoleApplication.Program</StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AnyOf, Version=0.2.0.0, Culture=neutral, PublicKeyToken=b35e6abbb527c6b1, processorArchitecture=MSIL">
<HintPath>..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll</HintPath>
</Reference>
<Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL"> <Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
<HintPath>..\..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll</HintPath> <HintPath>..\..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll</HintPath>
</Reference> </Reference>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AnyOf" version="0.2.0" targetFramework="net461" />
<package id="Handlebars.Net" version="2.0.4" targetFramework="net461" /> <package id="Handlebars.Net" version="2.0.4" targetFramework="net461" />
<package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net461" /> <package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net461" />
<package id="Handlebars.Net.Helpers.Core" version="2.1.1-preview-01" targetFramework="net461" /> <package id="Handlebars.Net.Helpers.Core" version="2.1.1-preview-01" targetFramework="net461" />

View File

@@ -39,6 +39,9 @@
<StartupObject>WireMock.Net.ConsoleApplication.Program</StartupObject> <StartupObject>WireMock.Net.ConsoleApplication.Program</StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AnyOf, Version=0.2.0.0, Culture=neutral, PublicKeyToken=b35e6abbb527c6b1, processorArchitecture=MSIL">
<HintPath>..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll</HintPath>
</Reference>
<Reference Include="Fare, Version=2.1.0.0, Culture=neutral, PublicKeyToken=ea68d375bf33a7c8, processorArchitecture=MSIL"> <Reference Include="Fare, Version=2.1.0.0, Culture=neutral, PublicKeyToken=ea68d375bf33a7c8, processorArchitecture=MSIL">
<HintPath>..\..\packages\Fare.2.1.1\lib\net35\Fare.dll</HintPath> <HintPath>..\..\packages\Fare.2.1.1\lib\net35\Fare.dll</HintPath>
</Reference> </Reference>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AnyOf" version="0.2.0" targetFramework="net472" />
<package id="Fare" version="2.1.1" targetFramework="net472" /> <package id="Fare" version="2.1.1" targetFramework="net472" />
<package id="Handlebars.Net" version="2.0.4" targetFramework="net472" /> <package id="Handlebars.Net" version="2.0.4" targetFramework="net472" />
<package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net472" /> <package id="Handlebars.Net.Helpers" version="2.1.1-preview-01" targetFramework="net472" />

View File

@@ -1,4 +1,4 @@
namespace WireMock.Admin.Mappings namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// MatcherModel /// MatcherModel
@@ -12,15 +12,20 @@
public string Name { get; set; } public string Name { get; set; }
/// <summary> /// <summary>
/// Gets or sets the pattern. Can be a string (default) or an object; /// Gets or sets the pattern. Can be a string (default) or an object.
/// </summary> /// </summary>
public object Pattern { get; set; } public object Pattern { get; set; }
/// <summary> /// <summary>
/// Gets or sets the patterns. Can be array of strings (default) or an array of objects; /// Gets or sets the patterns. Can be array of strings (default) or an array of objects.
/// </summary> /// </summary>
public object[] Patterns { get; set; } public object[] Patterns { get; set; }
/// <summary>
/// Gets or sets the pattern as a file.
/// </summary>
public string PatternAsFile { get; set; }
/// <summary> /// <summary>
/// Gets or sets the ignore case. /// Gets or sets the ignore case.
/// </summary> /// </summary>

View File

@@ -1,4 +1,4 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Handlers namespace WireMock.Handlers
@@ -89,5 +89,12 @@ namespace WireMock.Handlers
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <returns>The file content as bytes.</returns> /// <returns>The file content as bytes.</returns>
byte[] ReadFile([NotNull] string filename); byte[] ReadFile([NotNull] string filename);
/// <summary>
/// Read a file as string.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>The file content as a string.</returns>
string ReadFileAsString([NotNull] string filename);
} }
} }

View File

@@ -1,10 +1,13 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using WireMock.Exceptions; using WireMock.Exceptions;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -33,13 +36,13 @@ namespace WireMock.Matchers
/// <inheritdoc cref="IMatcher.ThrowException"/> /// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; } public bool ThrowException { get; }
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="CSharpCodeMatcher"/> class. /// Initializes a new instance of the <see cref="CSharpCodeMatcher"/> class.
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public CSharpCodeMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) public CSharpCodeMatcher([NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{ {
} }
@@ -48,7 +51,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public CSharpCodeMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) public CSharpCodeMatcher(MatchBehaviour matchBehaviour, [NotNull] params AnyOf<string, StringPattern>[] patterns)
{ {
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
@@ -73,7 +76,7 @@ namespace WireMock.Matchers
if (input != null) if (input != null)
{ {
match = MatchScores.ToScore(_patterns.Select(pattern => IsMatch(input, pattern))); match = MatchScores.ToScore(_patterns.Select(pattern => IsMatch(input, pattern.GetPattern())));
} }
return MatchBehaviourHelper.Convert(MatchBehaviour, match); return MatchBehaviourHelper.Convert(MatchBehaviour, match);
@@ -214,7 +217,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using AnyOfTypes;
using WireMock.Models;
namespace WireMock.Extensions
{
internal static class AnyOfExtensions
{
public static string GetPattern(this AnyOf<string, StringPattern> value)
{
return value.IsFirst ? value.First : value.Second.Pattern;
}
public static AnyOf<string, StringPattern>[] ToAnyOfPatterns(this IEnumerable<string> patterns)
{
return patterns.Select(p => p.ToAnyOfPattern()).ToArray();
}
public static AnyOf<string, StringPattern> ToAnyOfPattern(this string pattern)
{
return new AnyOf<string, StringPattern>(pattern);
}
}
}

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using WireMock.Util; using WireMock.Util;
using WireMock.Validation; using WireMock.Validation;
@@ -130,6 +130,12 @@ namespace WireMock.Handlers
return File.ReadAllBytes(AdjustPath(filename)); return File.ReadAllBytes(AdjustPath(filename));
} }
/// <inheritdoc cref="IFileSystemHandler.ReadFileAsString"/>
public string ReadFileAsString(string filename)
{
return File.ReadAllText(AdjustPath(Check.NotNullOrEmpty(filename, nameof(filename))));
}
/// <summary> /// <summary>
/// Adjusts the path to the MappingFolder. /// Adjusts the path to the MappingFolder.
/// </summary> /// </summary>

View File

@@ -1,5 +1,7 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Models;
namespace WireMock.Matchers namespace WireMock.Matchers
{ {
@@ -9,14 +11,14 @@ namespace WireMock.Matchers
/// <seealso cref="RegexMatcher" /> /// <seealso cref="RegexMatcher" />
public class ContentTypeMatcher : WildcardMatcher public class ContentTypeMatcher : WildcardMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class. /// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param> /// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) public ContentTypeMatcher([NotNull] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{ {
} }
@@ -26,7 +28,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param> /// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{ {
} }
@@ -35,7 +37,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param> /// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) public ContentTypeMatcher([NotNull] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{ {
} }
@@ -46,7 +48,7 @@ namespace WireMock.Matchers
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param> /// <param name="ignoreCase">IgnoreCase (default false)</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) : public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, patterns, ignoreCase, throwException) base(matchBehaviour, patterns, ignoreCase, throwException)
{ {
_patterns = patterns; _patterns = patterns;
@@ -64,7 +66,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns() public override AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -1,5 +1,8 @@
using System.Linq; using System.Linq;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -10,7 +13,7 @@ namespace WireMock.Matchers
/// <seealso cref="IStringMatcher" /> /// <seealso cref="IStringMatcher" />
public class ExactMatcher : IStringMatcher public class ExactMatcher : IStringMatcher
{ {
private readonly string[] _values; private readonly AnyOf<string, StringPattern>[] _values;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; } public MatchBehaviour MatchBehaviour { get; }
@@ -22,7 +25,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="ExactMatcher"/> class. /// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary> /// </summary>
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
public ExactMatcher([NotNull] params string[] values) : this(MatchBehaviour.AcceptOnMatch, false, values) public ExactMatcher([NotNull] params AnyOf<string, StringPattern>[] values) : this(MatchBehaviour.AcceptOnMatch, false, values)
{ {
} }
@@ -32,9 +35,9 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
public ExactMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] values) public ExactMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] values)
{ {
Check.HasNoNulls(values, nameof(values)); Check.NotNull(values, nameof(values));
MatchBehaviour = matchBehaviour; MatchBehaviour = matchBehaviour;
ThrowException = throwException; ThrowException = throwException;
@@ -46,14 +49,14 @@ namespace WireMock.Matchers
{ {
if (_values.Length == 1) if (_values.Length == 1)
{ {
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0] == input)); return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0].GetPattern() == input));
} }
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Contains(input))); return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Select(v => v.GetPattern()).Contains(input)));
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _values; return _values;
} }

View File

@@ -1,4 +1,7 @@
namespace WireMock.Matchers using AnyOfTypes;
using WireMock.Models;
namespace WireMock.Matchers
{ {
/// <summary> /// <summary>
/// IStringMatcher /// IStringMatcher
@@ -17,6 +20,6 @@
/// Gets the patterns. /// Gets the patterns.
/// </summary> /// </summary>
/// <returns>Patterns</returns> /// <returns>Patterns</returns>
string[] GetPatterns(); AnyOf<string, StringPattern>[] GetPatterns();
} }
} }

View File

@@ -1,7 +1,10 @@
using System.Linq; using System.Linq;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
/// <seealso cref="IObjectMatcher" /> /// <seealso cref="IObjectMatcher" />
public class JsonPathMatcher : IStringMatcher, IObjectMatcher public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; } public MatchBehaviour MatchBehaviour { get; }
@@ -25,7 +28,15 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns) public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns.ToAnyOfPatterns())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{ {
} }
@@ -35,7 +46,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public JsonPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns) public JsonPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] patterns)
{ {
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
@@ -94,7 +105,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
@@ -104,7 +115,7 @@ namespace WireMock.Matchers
private double IsMatch(JToken jtoken) private double IsMatch(JToken jtoken)
{ {
return MatchScores.ToScore(_patterns.Select(pattern => jtoken.SelectToken(pattern) != null)); return MatchScores.ToScore(_patterns.Select(pattern => jtoken.SelectToken(pattern.GetPattern()) != null));
} }
} }
} }

View File

@@ -1,7 +1,10 @@
using DevLab.JmesPath; using AnyOfTypes;
using DevLab.JmesPath;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Linq; using System.Linq;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -11,7 +14,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
public class JmesPathMatcher : IStringMatcher, IObjectMatcher public class JmesPathMatcher : IStringMatcher, IObjectMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; } public MatchBehaviour MatchBehaviour { get; }
@@ -23,7 +26,15 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class. /// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns) public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns.ToAnyOfPatterns())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher([NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{ {
} }
@@ -32,7 +43,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public JmesPathMatcher(bool throwException = false, [NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, throwException, patterns) public JmesPathMatcher(bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, throwException, patterns)
{ {
} }
@@ -42,7 +53,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public JmesPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns) public JmesPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] patterns)
{ {
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
@@ -59,7 +70,7 @@ namespace WireMock.Matchers
{ {
try try
{ {
match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern)))); match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern.GetPattern()))));
} }
catch (JsonException) catch (JsonException)
{ {
@@ -89,7 +100,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -1,8 +1,12 @@
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Util; using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
{ {
@@ -13,7 +17,7 @@ namespace WireMock.Matchers
/// <inheritdoc cref="IStringMatcher"/> /// <inheritdoc cref="IStringMatcher"/>
public class LinqMatcher : IObjectMatcher, IStringMatcher public class LinqMatcher : IObjectMatcher, IStringMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; } public MatchBehaviour MatchBehaviour { get; }
@@ -24,7 +28,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="LinqMatcher"/> class. /// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
public LinqMatcher([NotNull] string pattern) : this(new[] { pattern }) public LinqMatcher([NotNull] AnyOf<string, StringPattern> pattern) : this(new[] { pattern })
{ {
} }
@@ -32,7 +36,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="LinqMatcher"/> class. /// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public LinqMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns) public LinqMatcher([NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{ {
} }
@@ -41,7 +45,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, false, pattern) public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern> pattern) : this(matchBehaviour, false, pattern)
{ {
} }
@@ -51,8 +55,10 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns) public LinqMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] patterns)
{ {
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour; MatchBehaviour = matchBehaviour;
ThrowException = throwException; ThrowException = throwException;
_patterns = patterns; _patterns = patterns;
@@ -69,7 +75,7 @@ namespace WireMock.Matchers
try try
{ {
// Use the Any(...) method to check if the result matches // Use the Any(...) method to check if the result matches
match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern))); match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern.GetPattern())));
return MatchBehaviourHelper.Convert(MatchBehaviour, match); return MatchBehaviourHelper.Convert(MatchBehaviour, match);
} }
@@ -129,7 +135,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -1,7 +1,10 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
/// <inheritdoc cref="IIgnoreCaseMatcher"/> /// <inheritdoc cref="IIgnoreCaseMatcher"/>
public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
private readonly Regex[] _expressions; private readonly Regex[] _expressions;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) public RegexMatcher([NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{ {
} }
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{ {
} }
@@ -46,7 +49,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) public RegexMatcher([NotNull, RegexPattern] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{ {
} }
@@ -57,7 +60,7 @@ namespace WireMock.Matchers
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false, bool throwException = false) public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false, bool throwException = false)
{ {
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
@@ -73,7 +76,7 @@ namespace WireMock.Matchers
options |= RegexOptions.IgnoreCase; options |= RegexOptions.IgnoreCase;
} }
_expressions = patterns.Select(p => new Regex(p, options)).ToArray(); _expressions = patterns.Select(p => new Regex(p.GetPattern(), options)).ToArray();
} }
/// <inheritdoc cref="IStringMatcher.IsMatch"/> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
@@ -99,7 +102,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public virtual string[] GetPatterns() public virtual AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -1,8 +1,11 @@
using System.Linq; using System.Linq;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using SimMetrics.Net; using SimMetrics.Net;
using SimMetrics.Net.API; using SimMetrics.Net.API;
using SimMetrics.Net.Metric; using SimMetrics.Net.Metric;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
/// <seealso cref="IStringMatcher" /> /// <seealso cref="IStringMatcher" />
public class SimMetricsMatcher : IStringMatcher public class SimMetricsMatcher : IStringMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
private readonly SimMetricType _simMetricType; private readonly SimMetricType _simMetricType;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param> /// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType) public SimMetricsMatcher([NotNull] AnyOf<string, StringPattern> pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType)
{ {
} }
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param> /// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType) public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern> pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType)
{ {
} }
@@ -46,7 +49,16 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param> /// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType) public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns.ToAnyOfPatterns(), simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] AnyOf<string, StringPattern>[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType)
{ {
} }
@@ -57,9 +69,9 @@ namespace WireMock.Matchers
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param> /// <param name="simMetricType">The SimMetric Type</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein, bool throwException = false) public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern>[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein, bool throwException = false)
{ {
Check.NotNullOrEmpty(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour; MatchBehaviour = matchBehaviour;
ThrowException = throwException; ThrowException = throwException;
@@ -71,9 +83,9 @@ namespace WireMock.Matchers
/// <inheritdoc cref="IStringMatcher.IsMatch"/> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input) public double IsMatch(string input)
{ {
IStringMetric m = GetStringMetricType(); IStringMetric stringmetricType = GetStringMetricType();
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input)))); return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => stringmetricType.GetSimilarity(p.GetPattern(), input))));
} }
private IStringMetric GetStringMetricType() private IStringMetric GetStringMetricType()
@@ -120,7 +132,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -1,6 +1,9 @@
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
namespace WireMock.Matchers namespace WireMock.Matchers
{ {
@@ -10,14 +13,14 @@ namespace WireMock.Matchers
/// <seealso cref="RegexMatcher" /> /// <seealso cref="RegexMatcher" />
public class WildcardMatcher : RegexMatcher public class WildcardMatcher : RegexMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class. /// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param> /// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) public WildcardMatcher([NotNull] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{ {
} }
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param> /// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{ {
} }
@@ -36,7 +39,7 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param> /// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) public WildcardMatcher([NotNull] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{ {
} }
@@ -47,19 +50,30 @@ namespace WireMock.Matchers
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param> /// <param name="ignoreCase">IgnoreCase</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) : public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase, throwException) base(matchBehaviour, CreateArray(patterns), ignoreCase, throwException)
{ {
_patterns = patterns; _patterns = patterns;
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns() public override AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <inheritdoc cref="IMatcher.Name"/> /// <inheritdoc cref="IMatcher.Name"/>
public override string Name => "WildcardMatcher"; public override string Name => "WildcardMatcher";
private static AnyOf<string, StringPattern>[] CreateArray(AnyOf<string, StringPattern>[] patterns)
{
return patterns.Select(pattern => new AnyOf<string, StringPattern>(
new StringPattern
{
Pattern = "^" + Regex.Escape(pattern.GetPattern()).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
PatternAsFile = pattern.IsSecond ? pattern.Second.PatternAsFile : null
}))
.ToArray();
}
} }
} }

View File

@@ -1,7 +1,10 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Validation; using WireMock.Validation;
#if !NETSTANDARD1_3 #if !NETSTANDARD1_3
using Wmhelp.XPath2; using Wmhelp.XPath2;
@@ -15,7 +18,7 @@ namespace WireMock.Matchers
/// <seealso cref="IStringMatcher" /> /// <seealso cref="IStringMatcher" />
public class XPathMatcher : IStringMatcher public class XPathMatcher : IStringMatcher
{ {
private readonly string[] _patterns; private readonly AnyOf<string, StringPattern>[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; } public MatchBehaviour MatchBehaviour { get; }
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="XPathMatcher"/> class. /// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns) public XPathMatcher([NotNull] params AnyOf<string, StringPattern>[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{ {
} }
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param> /// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
public XPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns) public XPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf<string, StringPattern>[] patterns)
{ {
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
@@ -56,9 +59,9 @@ namespace WireMock.Matchers
{ {
var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
#if NETSTANDARD1_3 #if NETSTANDARD1_3
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p.GetPattern()})"))));
#else #else
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))); match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p.GetPattern()})"))));
#endif #endif
} }
catch (Exception) catch (Exception)
@@ -74,7 +77,7 @@ namespace WireMock.Matchers
} }
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public AnyOf<string, StringPattern>[] GetPatterns()
{ {
return _patterns; return _patterns;
} }

View File

@@ -0,0 +1,18 @@
namespace WireMock.Models
{
/// <summary>
/// StringPattern which defines the Pattern as a string, and optionally the filepath pattern file.
/// </summary>
public struct StringPattern
{
/// <summary>
/// The pattern as string.
/// </summary>
public string Pattern { get; set; }
/// <summary>
/// The filepath (optionally)
/// </summary>
public string PatternAsFile { get; set; }
}
}

View File

@@ -1,13 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using SimMetrics.Net; using SimMetrics.Net;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Extensions;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models;
using WireMock.Plugin; using WireMock.Plugin;
using WireMock.Settings; using WireMock.Settings;
using WireMock.Validation;
namespace WireMock.Serialization namespace WireMock.Serialization
{ {
@@ -17,8 +19,7 @@ namespace WireMock.Serialization
public MatcherMapper(IWireMockServerSettings settings) public MatcherMapper(IWireMockServerSettings settings)
{ {
Check.NotNull(settings, nameof(settings)); _settings = settings ?? throw new ArgumentNullException(nameof(settings));
_settings = settings;
} }
public IMatcher[] Map([CanBeNull] IEnumerable<MatcherModel> matchers) public IMatcher[] Map([CanBeNull] IEnumerable<MatcherModel> matchers)
@@ -36,9 +37,8 @@ namespace WireMock.Serialization
string[] parts = matcher.Name.Split('.'); string[] parts = matcher.Name.Split('.');
string matcherName = parts[0]; string matcherName = parts[0];
string matcherType = parts.Length > 1 ? parts[1] : null; string matcherType = parts.Length > 1 ? parts[1] : null;
var stringPatterns = ParseStringPatterns(matcher);
string[] stringPatterns = (matcher.Patterns != null ? matcher.Patterns : new[] { matcher.Pattern }).OfType<string>().ToArray(); var matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
bool ignoreCase = matcher.IgnoreCase == true; bool ignoreCase = matcher.IgnoreCase == true;
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true; bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
@@ -68,12 +68,12 @@ namespace WireMock.Serialization
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails); return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonMatcher": case "JsonMatcher":
object value = matcher.Pattern ?? matcher.Patterns; object valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns;
return new JsonMatcher(matchBehaviour, value, ignoreCase, throwExceptionWhenMatcherFails); return new JsonMatcher(matchBehaviour, valueForJsonMatcher, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonPartialMatcher": case "JsonPartialMatcher":
object matcherValue = matcher.Pattern ?? matcher.Patterns; object valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns;
return new JsonPartialMatcher(matchBehaviour, matcherValue, ignoreCase, throwExceptionWhenMatcherFails); return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonPathMatcher": case "JsonPathMatcher":
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns); return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
@@ -116,45 +116,85 @@ namespace WireMock.Serialization
return null; return null;
} }
object[] patterns = new object[0]; // Default empty array bool? ignoreCase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)null;
bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : (bool?)null;
var model = new MatcherModel
{
RejectOnMatch = rejectOnMatch,
IgnoreCase = ignoreCase,
Name = matcher.Name
};
switch (matcher) switch (matcher)
{ {
// If the matcher is a IStringMatcher, get the patterns. // If the matcher is a IStringMatcher, get the patterns.
case IStringMatcher stringMatcher: case IStringMatcher stringMatcher:
patterns = stringMatcher.GetPatterns().Cast<object>().ToArray(); var stringPatterns = stringMatcher.GetPatterns();
if (stringPatterns.Length == 1)
{
if (stringPatterns[0].IsFirst)
{
model.Pattern = stringPatterns[0].First;
}
else
{
model.Pattern = stringPatterns[0].Second.Pattern;
model.PatternAsFile = stringPatterns[0].Second.PatternAsFile;
}
}
else
{
model.Patterns = stringPatterns.Select(p => p.GetPattern()).Cast<object>().ToArray();
}
break; break;
// If the matcher is a IValueMatcher, get the value (can be string or object). // If the matcher is a IValueMatcher, get the value (can be string or object).
case IValueMatcher valueMatcher: case IValueMatcher valueMatcher:
patterns = new[] { valueMatcher.Value }; model.Patterns = new[] { valueMatcher.Value };
break; break;
// If the matcher is a ExactObjectMatcher, get the ValueAsObject or ValueAsBytes. // If the matcher is a ExactObjectMatcher, get the ValueAsObject or ValueAsBytes.
case ExactObjectMatcher exactObjectMatcher: case ExactObjectMatcher exactObjectMatcher:
patterns = new[] { exactObjectMatcher.ValueAsObject ?? exactObjectMatcher.ValueAsBytes }; model.Patterns = new[] { exactObjectMatcher.ValueAsObject ?? exactObjectMatcher.ValueAsBytes };
break; break;
} }
bool? ignoreCase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)null; return model;
bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : (bool?)null;
return new MatcherModel
{
RejectOnMatch = rejectOnMatch,
IgnoreCase = ignoreCase,
Name = matcher.Name,
Pattern = patterns.Length == 1 ? patterns.First() : null,
Patterns = patterns.Length > 1 ? patterns : null
};
} }
private ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, string stringPattern, bool throwException) private AnyOf<string, StringPattern>[] ParseStringPatterns(MatcherModel matcher)
{
if (matcher.Pattern is string patternAsString)
{
return new[] { new AnyOf<string, StringPattern>(patternAsString) };
}
if (matcher.Pattern is IEnumerable<string> patternAsStringArray)
{
return patternAsStringArray.ToAnyOfPatterns();
}
if (matcher.Patterns?.OfType<string>() is IEnumerable<string> patternsAsStringArray)
{
return patternsAsStringArray.ToAnyOfPatterns();
}
if (!string.IsNullOrEmpty(matcher.PatternAsFile))
{
var pattern = _settings.FileSystemHandler.ReadFileAsString(matcher.PatternAsFile);
return new[] { new AnyOf<string, StringPattern>(new StringPattern { Pattern = pattern, PatternAsFile = matcher.PatternAsFile }) };
}
return new AnyOf<string, StringPattern>[0];
}
private ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, AnyOf<string, StringPattern> stringPattern, bool throwException)
{ {
byte[] bytePattern; byte[] bytePattern;
try try
{ {
bytePattern = Convert.FromBase64String(stringPattern); bytePattern = Convert.FromBase64String(stringPattern.GetPattern());
} }
catch catch
{ {

View File

@@ -260,11 +260,13 @@ namespace WireMock.Server
private void ProcessWireMockOrgJObjectAndUseIMatcher(JObject items, Action<IMatcher> action) private void ProcessWireMockOrgJObjectAndUseIMatcher(JObject items, Action<IMatcher> action)
{ {
IMatcher matcher = null; if (!(items.First is JProperty firstItem))
{
return;
}
var firstItem = items.First as JProperty; IMatcher matcher;
if (firstItem.Name == "equalToJson")
if (firstItem?.Name == "equalToJson")
{ {
matcher = new JsonMatcher(firstItem.Value); matcher = new JsonMatcher(firstItem.Value);
} }

View File

@@ -1,137 +1,138 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description> <Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
<AssemblyTitle>WireMock.Net</AssemblyTitle> <AssemblyTitle>WireMock.Net</AssemblyTitle>
<Authors>Stef Heyenrath</Authors> <Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net452;net46;net461;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks> <TargetFrameworks>net451;net452;net46;net461;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>WireMock.Net</AssemblyName> <AssemblyName>WireMock.Net</AssemblyName>
<PackageId>WireMock.Net</PackageId> <PackageId>WireMock.Net</PackageId>
<PackageTags>tdd;mock;http;wiremock;test;server;unittest</PackageTags> <PackageTags>tdd;mock;http;wiremock;test;server;unittest</PackageTags>
<RootNamespace>WireMock</RootNamespace> <RootNamespace>WireMock</RootNamespace>
<ProjectGuid>{D3804228-91F4-4502-9595-39584E5A01AD}</ProjectGuid> <ProjectGuid>{D3804228-91F4-4502-9595-39584E5A01AD}</ProjectGuid>
<PublishRepositoryUrl>true</PublishRepositoryUrl> <PublishRepositoryUrl>true</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources> <EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <!--<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>--> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>-->
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>WireMock.Net.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>WireMock.Net.snk</AssemblyOriginatorKeyFile>
<!--<DelaySign>true</DelaySign>--> <!--<DelaySign>true</DelaySign>-->
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> <PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup> </PropertyGroup>
<!--<ItemGroup> <!--<ItemGroup>
<None Include="WireMock.Net-Logo.png" Pack="true" PackagePath="../../"/> <None Include="WireMock.Net-Logo.png" Pack="true" PackagePath="../../"/>
</ItemGroup>--> </ItemGroup>-->
<!-- https://github.com/aspnet/RoslynCodeDomProvider/issues/51 --> <!-- https://github.com/aspnet/RoslynCodeDomProvider/issues/51 -->
<!-- This is needed else we cannot build net452 in Azure DevOps Pipeline --> <!-- This is needed else we cannot build net452 in Azure DevOps Pipeline -->
<!--<Target Name="CheckIfShouldKillVBCSCompiler" />--> <!--<Target Name="CheckIfShouldKillVBCSCompiler" />-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> <PropertyGroup Condition="'$(Configuration)' == 'Release'">
<!--<PathMap>$(MSBuildProjectDirectory)=/</PathMap>--> <!--<PathMap>$(MSBuildProjectDirectory)=/</PathMap>-->
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug - Sonar'"> <PropertyGroup Condition="'$(Configuration)' == 'Debug - Sonar'">
<CodeAnalysisRuleSet>WireMock.Net.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>WireMock.Net.ruleset</CodeAnalysisRuleSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'"> <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>NETSTANDARD;USE_ASPNETCORE</DefineConstants> <DefineConstants>NETSTANDARD;USE_ASPNETCORE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' or '$(TargetFramework)' == 'netcoreapp2.2' or '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0'"> <PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' or '$(TargetFramework)' == 'netcoreapp2.2' or '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0'">
<DefineConstants>USE_ASPNETCORE</DefineConstants> <DefineConstants>USE_ASPNETCORE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'"> <PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
<DefineConstants>USE_ASPNETCORE;NET46</DefineConstants> <DefineConstants>USE_ASPNETCORE;NET46</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" /> <PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" /> <PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" /> <PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.12" /> <PackageReference Include="RandomDataGenerator.Net" Version="1.0.12" />
<PackageReference Include="JmesPath.Net" Version="1.0.125" /> <PackageReference Include="JmesPath.Net" Version="1.0.125" />
</ItemGroup> <PackageReference Include="AnyOf" Version="0.2.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug - Sonar'"> <ItemGroup Condition="'$(Configuration)' == 'Debug - Sonar'">
<PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320"> <PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' "> <ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
<PackageReference Include="XPath2.Extensions" Version="1.1.0" /> <PackageReference Include="XPath2.Extensions" Version="1.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' or '$(TargetFramework)' == 'net452' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net451' or '$(TargetFramework)' == 'net452' ">
<!-- Required for WebRequestHandler --> <!-- Required for WebRequestHandler -->
<Reference Include="System.Net.Http.WebRequest" /> <Reference Include="System.Net.Http.WebRequest" />
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" /> <PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Scriban.Signed" Version="2.1.4" /> <PackageReference Include="Scriban.Signed" Version="2.1.4" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" /> <PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
<PackageReference Include="Microsoft.Owin" Version="4.0.0" /> <PackageReference Include="Microsoft.Owin" Version="4.0.0" />
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="4.0.0" /> <PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="4.0.0" />
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.0.0" /> <PackageReference Include="Microsoft.Owin.Hosting" Version="4.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Scriban.Signed" Version="2.1.4" /> <PackageReference Include="Scriban.Signed" Version="2.1.4" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<PackageReference Include="Scriban.Signed" Version="2.1.4" /> <PackageReference Include="Scriban.Signed" Version="2.1.4" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<!-- https://github.com/WireMock-Net/WireMock.Net/issues/507 --> <!-- https://github.com/WireMock-Net/WireMock.Net/issues/507 -->
<PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" /> <PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' "> <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.7" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.1.3" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.1.3" />
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" /> <PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" /> <PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Scriban.Signed" Version="2.1.4" /> <PackageReference Include="Scriban.Signed" Version="2.1.4" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' "> <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="Scriban.Signed" Version="3.3.2" /> <PackageReference Include="Scriban.Signed" Version="3.3.2" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<!-- https://github.com/WireMock-Net/WireMock.Net/issues/507 --> <!-- https://github.com/WireMock-Net/WireMock.Net/issues/507 -->
<PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" /> <PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0'"> <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Scriban.Signed" Version="3.3.2" /> <PackageReference Include="Scriban.Signed" Version="3.3.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Handlebars.Net.Helpers" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.2.1" />
<PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.2.1" /> <PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.2.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" /> <ProjectReference Include="..\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
<ProjectReference Include="..\WireMock.Org.Abstractions\WireMock.Org.Abstractions.csproj" /> <ProjectReference Include="..\WireMock.Org.Abstractions\WireMock.Org.Abstractions.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Description>Commonly used interfaces, models, enumerations and types.</Description> <Description>Commonly used interfaces, models, enumerations and types.</Description>
@@ -33,7 +33,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!--<PackageReference Include="AnyOf" Version="0.0.23" />--> <!--<PackageReference Include="AnyOf" Version="0.1.0" />-->
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" /> <PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup> </ItemGroup>

View File

@@ -1,6 +1,6 @@
using NFluent;
using System; using System;
using System.IO; using System.IO;
using NFluent;
using WireMock.Handlers; using WireMock.Handlers;
using Xunit; using Xunit;
@@ -65,6 +65,13 @@ namespace WireMock.Net.Tests.Handlers
Check.ThatCode(() => _sut.ReadFile(null)).Throws<ArgumentNullException>(); Check.ThatCode(() => _sut.ReadFile(null)).Throws<ArgumentNullException>();
} }
[Fact]
public void LocalFileSystemHandler_ReadFileAsString_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFileAsString(null)).Throws<ArgumentNullException>();
}
[Fact] [Fact]
public void LocalFileSystemHandler_WriteFile_ThrowsArgumentNullException() public void LocalFileSystemHandler_WriteFile_ThrowsArgumentNullException()
{ {

View File

@@ -1,4 +1,4 @@
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -83,7 +83,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new CSharpCodeMatcher("x"); var matcher = new CSharpCodeMatcher("x");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("x"); Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,5 @@
using NFluent; using AnyOfTypes;
using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -49,7 +50,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new ContentTypeMatcher("x"); var matcher = new ContentTypeMatcher("x");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("x"); Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,4 @@
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new ExactMatcher("X"); var matcher = new ExactMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -27,7 +27,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JmesPathMatcher("X"); var matcher = new JmesPathMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -27,7 +27,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPathMatcher("X"); var matcher = new JsonPathMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -103,7 +103,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new LinqMatcher("x"); var matcher = new LinqMatcher("x");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("x"); Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,4 @@
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new RegexMatcher("X"); var matcher = new RegexMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new SimMetricsMatcher("X"); var matcher = new SimMetricsMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,35 +1,55 @@
using NFluent; using AnyOfTypes;
using FluentAssertions;
using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models;
using Xunit; using Xunit;
namespace WireMock.Net.Tests.Matchers namespace WireMock.Net.Tests.Matchers
{ {
public class WildcardMatcherTest public class WildcardMatcherTest
{ {
[Fact]
public void WildcardMatcher_IsMatch_With_StringMatcher_And_StringPattern()
{
// Arrange
var pattern = new StringPattern
{
Pattern = "*",
PatternAsFile = "pf"
};
// Act
var matcher = new WildcardMatcher(pattern);
// Assert
matcher.IsMatch("a").Should().Be(1.0d);
}
[Fact] [Fact]
public void WildcardMatcher_IsMatch_Positive() public void WildcardMatcher_IsMatch_Positive()
{ {
var tests = new[] var tests = new[]
{ {
new {p = "*", i = ""}, new { p = "*", i = "" },
new {p = "?", i = " "}, new { p = "?", i = " "},
new {p = "*", i = "a"}, new { p = "*", i = "a "},
new {p = "*", i = "ab"}, new { p = "*", i = "ab" },
new {p = "?", i = "a"}, new { p = "?", i = "a" },
new {p = "*?", i = "abc"}, new { p = "*?", i = "abc" },
new {p = "?*", i = "abc"}, new { p = "?*", i = "abc" },
new {p = "abc", i = "abc"}, new { p = "abc", i = "abc" },
new {p = "abc*", i = "abc"}, new { p = "abc*", i = "abc" },
new {p = "abc*", i = "abcd"}, new { p = "abc*", i = "abcd" },
new {p = "*abc*", i = "abc"}, new { p = "*abc*", i = "abc" },
new {p = "*a*bc*", i = "abc"}, new { p = "*a*bc*", i = "abc" },
new {p = "*a*b?", i = "aXXXbc"} new { p = "*a*b?", i = "aXXXbc" }
}; };
foreach (var test in tests) foreach (var test in tests)
{ {
var matcher = new WildcardMatcher(test.p); var matcher = new WildcardMatcher(test.p);
Check.That(matcher.IsMatch(test.i)).IsEqualTo(1.0d); matcher.IsMatch(test.i).Should().Be(1.0d, $"Pattern '{test.p}' with value '{test.i}' should be 1.0");
} }
} }
@@ -38,17 +58,17 @@ namespace WireMock.Net.Tests.Matchers
{ {
var tests = new[] var tests = new[]
{ {
new {p = "*a", i = ""}, new { p = "*a", i = "" },
new {p = "a*", i = ""}, new { p = "a*", i = "" },
new {p = "?", i = ""}, new { p = "?", i = "" },
new {p = "*b*", i = "a"}, new { p = "*b*", i = "a" },
new {p = "b*a", i = "ab"}, new { p = "b*a", i = "ab" },
new {p = "??", i = "a"}, new { p = "??", i = "a" },
new {p = "*?", i = ""}, new { p = "*?", i = "" },
new {p = "??*", i = "a"}, new { p = "??*", i = "a" },
new {p = "*abc", i = "abX"}, new { p = "*abc", i = "abX" },
new {p = "*abc*", i = "Xbc"}, new { p = "*abc*", i = "Xbc" },
new {p = "*a*bc*", i = "ac"} new { p = "*a*bc*", i = "ac" }
}; };
foreach (var test in tests) foreach (var test in tests)
@@ -78,10 +98,10 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new WildcardMatcher("x"); var matcher = new WildcardMatcher("x");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("x"); Check.That(patterns).ContainsExactly(new AnyOf<string, StringPattern>("x"));
} }
[Fact] [Fact]

View File

@@ -1,4 +1,4 @@
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new XPathMatcher("X"); var matcher = new XPathMatcher("X");
// Act // Act
string[] patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();
// Assert // Assert
Check.That(patterns).ContainsExactly("X"); Check.That(patterns).ContainsExactly("X");

View File

@@ -1,6 +1,8 @@
using System; using System;
using AnyOfTypes;
using FluentAssertions; using FluentAssertions;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models;
using WireMock.Plugin; using WireMock.Plugin;
using Xunit; using Xunit;
@@ -16,7 +18,8 @@ namespace WireMock.Net.Tests.Plugin
public void Load_Valid() public void Load_Valid()
{ {
// Act // Act
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, "x"); AnyOf<string, StringPattern> pattern = "x";
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, pattern);
// Assert // Assert
result.Should().NotBeNull(); result.Should().NotBeNull();

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using Xunit; using Xunit;
@@ -88,7 +88,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public void Request_WithPathRegexMatcher() public void Request_WithPathRegexMatcher_HasMatch()
{ {
// given // given
var spec = Request.Create().WithPath(new RegexMatcher("^/foo")); var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
@@ -102,7 +102,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public void Request_WithPathRegex_false() public void Request_WithPathRegexMatcher_HasNoMatch()
{ {
// given // given
var spec = Request.Create().WithPath("/foo"); var spec = Request.Create().WithPath("/foo");
@@ -115,6 +115,25 @@ namespace WireMock.Net.Tests
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
} }
[Fact]
public void Request_WithPathRegexMatcher_WithPatternAsFile_HasMatch()
{
// Arrange
var pattern = new StringPattern
{
Pattern = "^/foo",
PatternAsFile = "c:\\x.txt"
};
var spec = Request.Create().WithPath(new RegexMatcher(pattern));
// when
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact] [Fact]
public void Should_specify_requests_matching_given_path_and_method_delete() public void Should_specify_requests_matching_given_path_and_method_delete()
{ {

View File

@@ -1,9 +1,11 @@
using System; using System;
using AnyOfTypes;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NFluent; using NFluent;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models;
using WireMock.Serialization; using WireMock.Serialization;
using WireMock.Settings; using WireMock.Settings;
using Xunit; using Xunit;
@@ -60,7 +62,7 @@ namespace WireMock.Net.Tests.Serialization
// Assign // Assign
var matcherMock = new Mock<IStringMatcher>(); var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test"); matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new[] { "p1", "p2" }); matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { "p1", "p2" });
// Act // Act
var model = _sut.Map(matcherMock.Object); var model = _sut.Map(matcherMock.Object);
@@ -69,7 +71,30 @@ namespace WireMock.Net.Tests.Serialization
model.IgnoreCase.Should().BeNull(); model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test"); model.Name.Should().Be("test");
model.Pattern.Should().BeNull(); model.Pattern.Should().BeNull();
model.Patterns.Should().Contain("p1", "p2"); model.Patterns.Should().HaveCount(2)
.And.Contain("p1")
.And.Contain("p2");
}
[Fact]
public void MatcherMapper_Map_IStringMatcher_With_PatternAsFile()
{
// Arrange
var pattern = new StringPattern { Pattern = "p", PatternAsFile = "pf" };
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { pattern });
// Act
var model = _sut.Map(matcherMock.Object);
// Assert
model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test");
model.Pattern.Should().Be("p");
model.Patterns.Should().BeNull();
model.PatternAsFile.Should().Be("pf");
} }
[Fact] [Fact]
@@ -301,5 +326,24 @@ namespace WireMock.Net.Tests.Serialization
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns); matcher.Value.Should().BeEquivalentTo(patterns);
} }
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_StringPattern_With_PatternAsFile()
{
// Assign
var pattern = new StringPattern { Pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }", PatternAsFile = "pf" };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
} }
} }

View File

@@ -1,8 +1,12 @@
using System; using System;
using AnyOfTypes;
using FluentAssertions; using FluentAssertions;
using Moq;
using NFluent; using NFluent;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Handlers;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models;
using WireMock.Serialization; using WireMock.Serialization;
using WireMock.Settings; using WireMock.Settings;
using Xunit; using Xunit;
@@ -193,7 +197,7 @@ namespace WireMock.Net.Tests.Serialization
} }
[Fact] [Fact]
public void MatcherModelMapper_Map_WildcardMatcher() public void MatcherModelMapper_Map_WildcardMatcher_IgnoreCase()
{ {
// Assign // Assign
var model = new MatcherModel var model = new MatcherModel
@@ -211,6 +215,40 @@ namespace WireMock.Net.Tests.Serialization
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d); Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
} }
[Fact]
public void MatcherModelMapper_Map_WildcardMatcher_With_PatternAsFile()
{
// Arrange
var file = "c:\\test.txt";
var fileContent = "c";
var stringPattern = new StringPattern
{
Pattern = fileContent,
PatternAsFile = file
};
var fileSystemHandleMock = new Mock<IFileSystemHandler>();
fileSystemHandleMock.Setup(f => f.ReadFileAsString(file)).Returns(fileContent);
var model = new MatcherModel
{
Name = "WildcardMatcher",
PatternAsFile = file
};
var settings = new WireMockServerSettings
{
FileSystemHandler = fileSystemHandleMock.Object
};
var sut = new MatcherMapper(settings);
// Act
var matcher = (WildcardMatcher)sut.Map(model);
// Assert
matcher.GetPatterns().Should().HaveCount(1).And.Contain(new AnyOf<string, StringPattern>(stringPattern));
matcher.IsMatch("c").Should().Be(1.0d);
}
[Fact] [Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher() public void MatcherModelMapper_Map_SimMetricsMatcher()
{ {

View File

@@ -60,6 +60,7 @@
<PackageReference Include="SimMetrics.Net" Version="1.0.5" /> <PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" /> <PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" />
<!--<PackageReference Include="StrongNamer" Version="0.0.8" />--> <!--<PackageReference Include="StrongNamer" Version="0.0.8" />-->
<PackageReference Include="AnyOf" Version="0.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net461'"> <ItemGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net461'">