diff --git a/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs b/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs
index 012b3350..45e06730 100644
--- a/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs
+++ b/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs
@@ -1,4 +1,4 @@
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.IO;
using WireMock.Handlers;
@@ -80,6 +80,12 @@ namespace WireMock.Net.ConsoleApplication
return File.ReadAllBytes(AdjustPath(path));
}
+ ///
+ public string ReadFileAsString(string path)
+ {
+ return File.ReadAllText(path);
+ }
+
///
/// Adjusts the path to the MappingFolder.
///
diff --git a/examples/WireMock.Net.Console.Net452.Classic/WireMock.Net.Console.Net452.Classic.csproj b/examples/WireMock.Net.Console.Net452.Classic/WireMock.Net.Console.Net452.Classic.csproj
index 3a39c4e7..70d40b5b 100644
--- a/examples/WireMock.Net.Console.Net452.Classic/WireMock.Net.Console.Net452.Classic.csproj
+++ b/examples/WireMock.Net.Console.Net452.Classic/WireMock.Net.Console.Net452.Classic.csproj
@@ -36,6 +36,9 @@
..\..\resources\WireMock.Net-Logo.ico
+
+ ..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll
+
..\..\packages\Handlebars.Net.2.0.4\lib\net452\Handlebars.dll
diff --git a/examples/WireMock.Net.Console.Net452.Classic/packages.config b/examples/WireMock.Net.Console.Net452.Classic/packages.config
index 8d57f57b..5aef79f8 100644
--- a/examples/WireMock.Net.Console.Net452.Classic/packages.config
+++ b/examples/WireMock.Net.Console.Net452.Classic/packages.config
@@ -1,5 +1,6 @@
+
diff --git a/examples/WireMock.Net.Console.Net461.Classic/WireMock.Net.Console.Net461.Classic.csproj b/examples/WireMock.Net.Console.Net461.Classic/WireMock.Net.Console.Net461.Classic.csproj
index c166cc39..312a39e7 100644
--- a/examples/WireMock.Net.Console.Net461.Classic/WireMock.Net.Console.Net461.Classic.csproj
+++ b/examples/WireMock.Net.Console.Net461.Classic/WireMock.Net.Console.Net461.Classic.csproj
@@ -35,6 +35,9 @@
WireMock.Net.ConsoleApplication.Program
+
+ ..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll
+
..\..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll
diff --git a/examples/WireMock.Net.Console.Net461.Classic/packages.config b/examples/WireMock.Net.Console.Net461.Classic/packages.config
index ff8582dd..eeff0304 100644
--- a/examples/WireMock.Net.Console.Net461.Classic/packages.config
+++ b/examples/WireMock.Net.Console.Net461.Classic/packages.config
@@ -1,5 +1,6 @@
+
diff --git a/examples/WireMock.Net.Console.Net472.Classic/WireMock.Net.Console.Net472.Classic.csproj b/examples/WireMock.Net.Console.Net472.Classic/WireMock.Net.Console.Net472.Classic.csproj
index eba05faf..b6aa7714 100644
--- a/examples/WireMock.Net.Console.Net472.Classic/WireMock.Net.Console.Net472.Classic.csproj
+++ b/examples/WireMock.Net.Console.Net472.Classic/WireMock.Net.Console.Net472.Classic.csproj
@@ -39,6 +39,9 @@
WireMock.Net.ConsoleApplication.Program
+
+ ..\..\packages\AnyOf.0.2.0\lib\net45\AnyOf.dll
+
..\..\packages\Fare.2.1.1\lib\net35\Fare.dll
diff --git a/examples/WireMock.Net.Console.Net472.Classic/packages.config b/examples/WireMock.Net.Console.Net472.Classic/packages.config
index 04812545..955115a7 100644
--- a/examples/WireMock.Net.Console.Net472.Classic/packages.config
+++ b/examples/WireMock.Net.Console.Net472.Classic/packages.config
@@ -1,5 +1,6 @@
+
diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs
index be5fdc51..38bf9948 100644
--- a/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs
+++ b/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs
@@ -1,4 +1,4 @@
-namespace WireMock.Admin.Mappings
+namespace WireMock.Admin.Mappings
{
///
/// MatcherModel
@@ -12,15 +12,20 @@
public string Name { get; set; }
///
- /// 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.
///
public object Pattern { get; set; }
///
- /// 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.
///
public object[] Patterns { get; set; }
+ ///
+ /// Gets or sets the pattern as a file.
+ ///
+ public string PatternAsFile { get; set; }
+
///
/// Gets or sets the ignore case.
///
diff --git a/src/WireMock.Net.Abstractions/Handlers/IFileSystemHandler.cs b/src/WireMock.Net.Abstractions/Handlers/IFileSystemHandler.cs
index 498c5826..ffeb06ec 100644
--- a/src/WireMock.Net.Abstractions/Handlers/IFileSystemHandler.cs
+++ b/src/WireMock.Net.Abstractions/Handlers/IFileSystemHandler.cs
@@ -1,4 +1,4 @@
-using JetBrains.Annotations;
+using JetBrains.Annotations;
using System.Collections.Generic;
namespace WireMock.Handlers
@@ -89,5 +89,12 @@ namespace WireMock.Handlers
/// The filename.
/// The file content as bytes.
byte[] ReadFile([NotNull] string filename);
+
+ ///
+ /// Read a file as string.
+ ///
+ /// The filename.
+ /// The file content as a string.
+ string ReadFileAsString([NotNull] string filename);
}
}
\ No newline at end of file
diff --git a/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs
index 70926936..a92f3771 100644
--- a/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs
+++ b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs
@@ -1,225 +1,228 @@
-using System;
-using System.Linq;
+using System;
+using System.Linq;
using System.Reflection;
using System.Text;
-using JetBrains.Annotations;
-using Newtonsoft.Json.Linq;
-using WireMock.Exceptions;
-using WireMock.Validation;
-
-namespace WireMock.Matchers
-{
- ///
- /// CSharpCode / CS-Script Matcher
- ///
- ///
- internal class CSharpCodeMatcher : ICSharpCodeMatcher
- {
- private const string TemplateForIsMatchWithString = "public class CodeHelper {{ public bool IsMatch(string it) {{ {0} }} }}";
-
- private const string TemplateForIsMatchWithDynamic = "public class CodeHelper {{ public bool IsMatch(dynamic it) {{ {0} }} }}";
-
- private readonly string[] _usings =
- {
- "System",
- "System.Linq",
- "System.Collections.Generic",
- "Microsoft.CSharp",
- "Newtonsoft.Json.Linq"
- };
-
- public MatchBehaviour MatchBehaviour { get; }
-
- ///
- public bool ThrowException { get; }
-
- private readonly string[] _patterns;
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The patterns.
- public CSharpCodeMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The match behaviour.
- /// The patterns.
- public CSharpCodeMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns)
- {
- Check.NotNull(patterns, nameof(patterns));
-
- MatchBehaviour = matchBehaviour;
- ThrowException = false;
- _patterns = patterns;
- }
-
- public double IsMatch(string input)
- {
- return IsMatchInternal(input);
- }
-
- public double IsMatch(object input)
- {
- return IsMatchInternal(input);
- }
-
- public double IsMatchInternal(object input)
- {
- double match = MatchScores.Mismatch;
-
- if (input != null)
- {
- match = MatchScores.ToScore(_patterns.Select(pattern => IsMatch(input, pattern)));
- }
-
- return MatchBehaviourHelper.Convert(MatchBehaviour, match);
- }
-
- private bool IsMatch(dynamic input, string pattern)
- {
- bool isMatchWithString = input is string;
- var inputValue = isMatchWithString ? input : JObject.FromObject(input);
- string source = GetSourceForIsMatchWithString(pattern, isMatchWithString);
-
- object result = null;
-
-#if (NET451 || NET452)
- var compilerParams = new System.CodeDom.Compiler.CompilerParameters
- {
- GenerateInMemory = true,
- GenerateExecutable = false,
- ReferencedAssemblies =
- {
- "System.dll",
- "System.Core.dll",
- "Microsoft.CSharp.dll",
- "Newtonsoft.Json.dll"
- }
- };
-
- using (var codeProvider = new Microsoft.CSharp.CSharpCodeProvider())
- {
- var compilerResults = codeProvider.CompileAssemblyFromSource(compilerParams, source);
-
- if (compilerResults.Errors.Count != 0)
- {
- var errors = from System.CodeDom.Compiler.CompilerError er in compilerResults.Errors select er.ToString();
- throw new WireMockException(string.Join(", ", errors));
- }
-
- object helper = compilerResults.CompiledAssembly.CreateInstance("CodeHelper");
- if (helper == null)
- {
- throw new WireMockException("CSharpCodeMatcher: Unable to create instance from WireMock.CodeHelper");
- }
-
- var methodInfo = helper.GetType().GetMethod("IsMatch");
- if (methodInfo == null)
- {
- throw new WireMockException("CSharpCodeMatcher: Unable to find method 'IsMatch' in WireMock.CodeHelper");
- }
-
- try
- {
- result = methodInfo.Invoke(helper, new[] { inputValue });
- }
- catch (Exception ex)
- {
- throw new WireMockException("CSharpCodeMatcher: Unable to call method 'IsMatch' in WireMock.CodeHelper", ex);
- }
- }
-#elif (NET46 || NET461)
- dynamic script;
- try
- {
- script = CSScriptLibrary.CSScript.Evaluator.CompileCode(source).CreateObject("*");
- }
- catch (Exception ex)
- {
- throw new WireMockException("CSharpCodeMatcher: Unable to create compiler for WireMock.CodeHelper", ex);
- }
-
- try
- {
- result = script.IsMatch(inputValue);
- }
- catch (Exception ex)
- {
- throw new WireMockException("CSharpCodeMatcher: Problem calling method 'IsMatch' in WireMock.CodeHelper", ex);
- }
-
-#elif (NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1 || NET5_0)
- Assembly assembly;
- try
+using AnyOfTypes;
+using JetBrains.Annotations;
+using Newtonsoft.Json.Linq;
+using WireMock.Exceptions;
+using WireMock.Extensions;
+using WireMock.Models;
+using WireMock.Validation;
+
+namespace WireMock.Matchers
+{
+ ///
+ /// CSharpCode / CS-Script Matcher
+ ///
+ ///
+ internal class CSharpCodeMatcher : ICSharpCodeMatcher
+ {
+ private const string TemplateForIsMatchWithString = "public class CodeHelper {{ public bool IsMatch(string it) {{ {0} }} }}";
+
+ private const string TemplateForIsMatchWithDynamic = "public class CodeHelper {{ public bool IsMatch(dynamic it) {{ {0} }} }}";
+
+ private readonly string[] _usings =
+ {
+ "System",
+ "System.Linq",
+ "System.Collections.Generic",
+ "Microsoft.CSharp",
+ "Newtonsoft.Json.Linq"
+ };
+
+ public MatchBehaviour MatchBehaviour { get; }
+
+ ///
+ public bool ThrowException { get; }
+
+ private readonly AnyOf[] _patterns;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The patterns.
+ public CSharpCodeMatcher([NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The match behaviour.
+ /// The patterns.
+ public CSharpCodeMatcher(MatchBehaviour matchBehaviour, [NotNull] params AnyOf[] patterns)
+ {
+ Check.NotNull(patterns, nameof(patterns));
+
+ MatchBehaviour = matchBehaviour;
+ ThrowException = false;
+ _patterns = patterns;
+ }
+
+ public double IsMatch(string input)
+ {
+ return IsMatchInternal(input);
+ }
+
+ public double IsMatch(object input)
+ {
+ return IsMatchInternal(input);
+ }
+
+ public double IsMatchInternal(object input)
+ {
+ double match = MatchScores.Mismatch;
+
+ if (input != null)
{
- assembly = CSScriptLib.CSScript.Evaluator.CompileCode(source);
- }
- catch (Exception ex)
- {
- throw new WireMockException($"CSharpCodeMatcher: Unable to compile code `{source}` for WireMock.CodeHelper", ex);
- }
-
- dynamic script;
+ match = MatchScores.ToScore(_patterns.Select(pattern => IsMatch(input, pattern.GetPattern())));
+ }
+
+ return MatchBehaviourHelper.Convert(MatchBehaviour, match);
+ }
+
+ private bool IsMatch(dynamic input, string pattern)
+ {
+ bool isMatchWithString = input is string;
+ var inputValue = isMatchWithString ? input : JObject.FromObject(input);
+ string source = GetSourceForIsMatchWithString(pattern, isMatchWithString);
+
+ object result = null;
+
+#if (NET451 || NET452)
+ var compilerParams = new System.CodeDom.Compiler.CompilerParameters
+ {
+ GenerateInMemory = true,
+ GenerateExecutable = false,
+ ReferencedAssemblies =
+ {
+ "System.dll",
+ "System.Core.dll",
+ "Microsoft.CSharp.dll",
+ "Newtonsoft.Json.dll"
+ }
+ };
+
+ using (var codeProvider = new Microsoft.CSharp.CSharpCodeProvider())
+ {
+ var compilerResults = codeProvider.CompileAssemblyFromSource(compilerParams, source);
+
+ if (compilerResults.Errors.Count != 0)
+ {
+ var errors = from System.CodeDom.Compiler.CompilerError er in compilerResults.Errors select er.ToString();
+ throw new WireMockException(string.Join(", ", errors));
+ }
+
+ object helper = compilerResults.CompiledAssembly.CreateInstance("CodeHelper");
+ if (helper == null)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Unable to create instance from WireMock.CodeHelper");
+ }
+
+ var methodInfo = helper.GetType().GetMethod("IsMatch");
+ if (methodInfo == null)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Unable to find method 'IsMatch' in WireMock.CodeHelper");
+ }
+
+ try
+ {
+ result = methodInfo.Invoke(helper, new[] { inputValue });
+ }
+ catch (Exception ex)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Unable to call method 'IsMatch' in WireMock.CodeHelper", ex);
+ }
+ }
+#elif (NET46 || NET461)
+ dynamic script;
try
{
-#if NETSTANDARD2_0
- script = csscript.GenericExtensions.CreateObject(assembly, "*");
-#else
- script = CSScriptLib.ReflectionExtensions.CreateObject(assembly, "*");
+ script = CSScriptLibrary.CSScript.Evaluator.CompileCode(source).CreateObject("*");
+ }
+ catch (Exception ex)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Unable to create compiler for WireMock.CodeHelper", ex);
+ }
+
+ try
+ {
+ result = script.IsMatch(inputValue);
+ }
+ catch (Exception ex)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Problem calling method 'IsMatch' in WireMock.CodeHelper", ex);
+ }
+
+#elif (NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1 || NET5_0)
+ Assembly assembly;
+ try
+ {
+ assembly = CSScriptLib.CSScript.Evaluator.CompileCode(source);
+ }
+ catch (Exception ex)
+ {
+ throw new WireMockException($"CSharpCodeMatcher: Unable to compile code `{source}` for WireMock.CodeHelper", ex);
+ }
+
+ dynamic script;
+ try
+ {
+#if NETSTANDARD2_0
+ script = csscript.GenericExtensions.CreateObject(assembly, "*");
+#else
+ script = CSScriptLib.ReflectionExtensions.CreateObject(assembly, "*");
#endif
- }
- catch (Exception ex)
+ }
+ catch (Exception ex)
{
throw new WireMockException("CSharpCodeMatcher: Unable to create object from assembly", ex);
- }
-
- try
- {
- result = script.IsMatch(inputValue);
- }
- catch (Exception ex)
- {
- throw new WireMockException("CSharpCodeMatcher: Problem calling method 'IsMatch' in WireMock.CodeHelper", ex);
}
-#else
- throw new NotSupportedException("The 'CSharpCodeMatcher' cannot be used in netstandard 1.3");
-#endif
- try
- {
- return (bool)result;
- }
- catch
- {
- throw new WireMockException($"Unable to cast result '{result}' to bool");
- }
- }
-
- private string GetSourceForIsMatchWithString(string pattern, bool isMatchWithString)
- {
- string template = isMatchWithString ? TemplateForIsMatchWithString : TemplateForIsMatchWithDynamic;
-
- var stringBuilder = new StringBuilder();
+
+ try
+ {
+ result = script.IsMatch(inputValue);
+ }
+ catch (Exception ex)
+ {
+ throw new WireMockException("CSharpCodeMatcher: Problem calling method 'IsMatch' in WireMock.CodeHelper", ex);
+ }
+#else
+ throw new NotSupportedException("The 'CSharpCodeMatcher' cannot be used in netstandard 1.3");
+#endif
+ try
+ {
+ return (bool)result;
+ }
+ catch
+ {
+ throw new WireMockException($"Unable to cast result '{result}' to bool");
+ }
+ }
+
+ private string GetSourceForIsMatchWithString(string pattern, bool isMatchWithString)
+ {
+ string template = isMatchWithString ? TemplateForIsMatchWithString : TemplateForIsMatchWithDynamic;
+
+ var stringBuilder = new StringBuilder();
foreach (string @using in _usings)
{
stringBuilder.AppendLine($"using {@using};");
- }
- stringBuilder.AppendLine();
- stringBuilder.AppendFormat(template, pattern);
-
- return stringBuilder.ToString();
- }
-
- ///
- public string[] GetPatterns()
- {
- return _patterns;
- }
-
- ///
- public string Name => "CSharpCodeMatcher";
- }
+ }
+ stringBuilder.AppendLine();
+ stringBuilder.AppendFormat(template, pattern);
+
+ return stringBuilder.ToString();
+ }
+
+ ///
+ public AnyOf[] GetPatterns()
+ {
+ return _patterns;
+ }
+
+ ///
+ public string Name => "CSharpCodeMatcher";
+ }
}
\ No newline at end of file
diff --git a/src/WireMock.Net/Extensions/AnyOfExtensions.cs b/src/WireMock.Net/Extensions/AnyOfExtensions.cs
new file mode 100644
index 00000000..015690e2
--- /dev/null
+++ b/src/WireMock.Net/Extensions/AnyOfExtensions.cs
@@ -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 value)
+ {
+ return value.IsFirst ? value.First : value.Second.Pattern;
+ }
+
+ public static AnyOf[] ToAnyOfPatterns(this IEnumerable patterns)
+ {
+ return patterns.Select(p => p.ToAnyOfPattern()).ToArray();
+ }
+
+ public static AnyOf ToAnyOfPattern(this string pattern)
+ {
+ return new AnyOf(pattern);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs b/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs
index 2dd7e5e0..467701d3 100644
--- a/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs
+++ b/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs
@@ -1,4 +1,4 @@
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.IO;
using WireMock.Util;
using WireMock.Validation;
@@ -130,6 +130,12 @@ namespace WireMock.Handlers
return File.ReadAllBytes(AdjustPath(filename));
}
+ ///
+ public string ReadFileAsString(string filename)
+ {
+ return File.ReadAllText(AdjustPath(Check.NotNullOrEmpty(filename, nameof(filename))));
+ }
+
///
/// Adjusts the path to the MappingFolder.
///
@@ -140,4 +146,4 @@ namespace WireMock.Handlers
return Path.Combine(GetMappingFolder(), filename);
}
}
-}
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/ContentTypeMatcher.cs b/src/WireMock.Net/Matchers/ContentTypeMatcher.cs
index 7c9ba1a1..d4ac069e 100644
--- a/src/WireMock.Net/Matchers/ContentTypeMatcher.cs
+++ b/src/WireMock.Net/Matchers/ContentTypeMatcher.cs
@@ -1,5 +1,7 @@
-using System.Net.Http.Headers;
+using System.Net.Http.Headers;
+using AnyOfTypes;
using JetBrains.Annotations;
+using WireMock.Models;
namespace WireMock.Matchers
{
@@ -9,14 +11,14 @@ namespace WireMock.Matchers
///
public class ContentTypeMatcher : WildcardMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
/// Initializes a new instance of the class.
///
/// The pattern.
/// IgnoreCase (default false)
- public ContentTypeMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
+ public ContentTypeMatcher([NotNull] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
@@ -26,7 +28,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// The pattern.
/// IgnoreCase (default false)
- public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
+ public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
@@ -35,7 +37,7 @@ namespace WireMock.Matchers
///
/// The patterns.
/// IgnoreCase (default false)
- public ContentTypeMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
+ public ContentTypeMatcher([NotNull] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
@@ -46,7 +48,7 @@ namespace WireMock.Matchers
/// The patterns.
/// IgnoreCase (default false)
/// Throw an exception when the internal matching fails because of invalid input.
- public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) :
+ public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, patterns, ignoreCase, throwException)
{
_patterns = patterns;
@@ -64,7 +66,7 @@ namespace WireMock.Matchers
}
///
- public override string[] GetPatterns()
+ public override AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Matchers/ExactMatcher.cs b/src/WireMock.Net/Matchers/ExactMatcher.cs
index 4ceee72f..4568544e 100644
--- a/src/WireMock.Net/Matchers/ExactMatcher.cs
+++ b/src/WireMock.Net/Matchers/ExactMatcher.cs
@@ -1,5 +1,8 @@
-using System.Linq;
+using System.Linq;
+using AnyOfTypes;
using JetBrains.Annotations;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -10,7 +13,7 @@ namespace WireMock.Matchers
///
public class ExactMatcher : IStringMatcher
{
- private readonly string[] _values;
+ private readonly AnyOf[] _values;
///
public MatchBehaviour MatchBehaviour { get; }
@@ -22,7 +25,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The values.
- public ExactMatcher([NotNull] params string[] values) : this(MatchBehaviour.AcceptOnMatch, false, values)
+ public ExactMatcher([NotNull] params AnyOf[] values) : this(MatchBehaviour.AcceptOnMatch, false, values)
{
}
@@ -32,9 +35,9 @@ namespace WireMock.Matchers
/// The match behaviour.
/// Throw an exception when the internal matching fails because of invalid input.
/// The values.
- public ExactMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] values)
+ public ExactMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf[] values)
{
- Check.HasNoNulls(values, nameof(values));
+ Check.NotNull(values, nameof(values));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
@@ -46,14 +49,14 @@ namespace WireMock.Matchers
{
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)));
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _values;
}
diff --git a/src/WireMock.Net/Matchers/IStringMatcher.cs b/src/WireMock.Net/Matchers/IStringMatcher.cs
index c580a33e..126373fe 100644
--- a/src/WireMock.Net/Matchers/IStringMatcher.cs
+++ b/src/WireMock.Net/Matchers/IStringMatcher.cs
@@ -1,4 +1,7 @@
-namespace WireMock.Matchers
+using AnyOfTypes;
+using WireMock.Models;
+
+namespace WireMock.Matchers
{
///
/// IStringMatcher
@@ -17,6 +20,6 @@
/// Gets the patterns.
///
/// Patterns
- string[] GetPatterns();
+ AnyOf[] GetPatterns();
}
}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs
index 65937497..6d6de3f2 100644
--- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs
+++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs
@@ -1,7 +1,10 @@
-using System.Linq;
+using System.Linq;
+using AnyOfTypes;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
///
public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
public MatchBehaviour MatchBehaviour { get; }
@@ -25,7 +28,15 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The patterns.
- public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
+ public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns.ToAnyOfPatterns())
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The patterns.
+ public JsonPathMatcher([NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
@@ -35,7 +46,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// Throw an exception when the internal matching fails because of invalid input.
/// The patterns.
- public JsonPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
+ public JsonPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
@@ -94,7 +105,7 @@ namespace WireMock.Matchers
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _patterns;
}
@@ -104,7 +115,7 @@ namespace WireMock.Matchers
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));
}
}
}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/JmesPathMatcher.cs b/src/WireMock.Net/Matchers/JmesPathMatcher.cs
index 7f916746..368d42b4 100644
--- a/src/WireMock.Net/Matchers/JmesPathMatcher.cs
+++ b/src/WireMock.Net/Matchers/JmesPathMatcher.cs
@@ -1,7 +1,10 @@
-using DevLab.JmesPath;
+using AnyOfTypes;
+using DevLab.JmesPath;
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Linq;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -11,7 +14,7 @@ namespace WireMock.Matchers
///
public class JmesPathMatcher : IStringMatcher, IObjectMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
public MatchBehaviour MatchBehaviour { get; }
@@ -23,7 +26,15 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The patterns.
- public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
+ public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns.ToAnyOfPatterns())
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The patterns.
+ public JmesPathMatcher([NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
@@ -32,7 +43,7 @@ namespace WireMock.Matchers
///
/// Throw an exception when the internal matching fails because of invalid input.
/// The patterns.
- public JmesPathMatcher(bool throwException = false, [NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, throwException, patterns)
+ public JmesPathMatcher(bool throwException = false, [NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, throwException, patterns)
{
}
@@ -42,7 +53,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// Throw an exception when the internal matching fails because of invalid input.
/// The patterns.
- public JmesPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
+ public JmesPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
@@ -59,7 +70,7 @@ namespace WireMock.Matchers
{
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)
{
@@ -89,7 +100,7 @@ namespace WireMock.Matchers
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Matchers/LinqMatcher.cs b/src/WireMock.Net/Matchers/LinqMatcher.cs
index b3134a03..19efe9ae 100644
--- a/src/WireMock.Net/Matchers/LinqMatcher.cs
+++ b/src/WireMock.Net/Matchers/LinqMatcher.cs
@@ -1,8 +1,12 @@
-using System.Linq;
+using System.Linq;
using System.Linq.Dynamic.Core;
+using AnyOfTypes;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Util;
+using WireMock.Validation;
namespace WireMock.Matchers
{
@@ -13,7 +17,7 @@ namespace WireMock.Matchers
///
public class LinqMatcher : IObjectMatcher, IStringMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
public MatchBehaviour MatchBehaviour { get; }
@@ -24,7 +28,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The pattern.
- public LinqMatcher([NotNull] string pattern) : this(new[] { pattern })
+ public LinqMatcher([NotNull] AnyOf pattern) : this(new[] { pattern })
{
}
@@ -32,7 +36,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The patterns.
- public LinqMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
+ public LinqMatcher([NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
@@ -41,7 +45,7 @@ namespace WireMock.Matchers
///
/// The match behaviour.
/// The pattern.
- public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, false, pattern)
+ public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern) : this(matchBehaviour, false, pattern)
{
}
@@ -51,8 +55,10 @@ namespace WireMock.Matchers
/// The match behaviour.
/// The patterns.
/// Throw an exception when the internal matching fails because of invalid input.
- public LinqMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
+ public LinqMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf[] patterns)
{
+ Check.NotNull(patterns, nameof(patterns));
+
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
@@ -69,7 +75,7 @@ namespace WireMock.Matchers
try
{
// 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);
}
@@ -129,7 +135,7 @@ namespace WireMock.Matchers
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Matchers/RegexMatcher.cs b/src/WireMock.Net/Matchers/RegexMatcher.cs
index f0c22bac..5f2d6e57 100644
--- a/src/WireMock.Net/Matchers/RegexMatcher.cs
+++ b/src/WireMock.Net/Matchers/RegexMatcher.cs
@@ -1,7 +1,10 @@
-using System;
+using System;
using System.Linq;
using System.Text.RegularExpressions;
+using AnyOfTypes;
using JetBrains.Annotations;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
///
public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
private readonly Regex[] _expressions;
///
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
///
/// The pattern.
/// Ignore the case from the pattern.
- public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
+ public RegexMatcher([NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// The pattern.
/// Ignore the case from the pattern.
- public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
+ public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
@@ -46,7 +49,7 @@ namespace WireMock.Matchers
///
/// The patterns.
/// Ignore the case from the pattern.
- public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
+ public RegexMatcher([NotNull, RegexPattern] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
@@ -57,7 +60,7 @@ namespace WireMock.Matchers
/// The patterns.
/// Ignore the case from the pattern.
/// Throw an exception when the internal matching fails because of invalid input.
- public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false, bool throwException = false)
+ public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false)
{
Check.NotNull(patterns, nameof(patterns));
@@ -73,7 +76,7 @@ namespace WireMock.Matchers
options |= RegexOptions.IgnoreCase;
}
- _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
+ _expressions = patterns.Select(p => new Regex(p.GetPattern(), options)).ToArray();
}
///
@@ -99,7 +102,7 @@ namespace WireMock.Matchers
}
///
- public virtual string[] GetPatterns()
+ public virtual AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Matchers/SimMetricsMatcher.cs b/src/WireMock.Net/Matchers/SimMetricsMatcher.cs
index 95125cda..c02754d5 100644
--- a/src/WireMock.Net/Matchers/SimMetricsMatcher.cs
+++ b/src/WireMock.Net/Matchers/SimMetricsMatcher.cs
@@ -1,8 +1,11 @@
-using System.Linq;
+using System.Linq;
+using AnyOfTypes;
using JetBrains.Annotations;
using SimMetrics.Net;
using SimMetrics.Net.API;
using SimMetrics.Net.Metric;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -13,7 +16,7 @@ namespace WireMock.Matchers
///
public class SimMetricsMatcher : IStringMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
private readonly SimMetricType _simMetricType;
///
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
///
/// The pattern.
/// The SimMetric Type
- public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType)
+ public SimMetricsMatcher([NotNull] AnyOf pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType)
{
}
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// The pattern.
/// The SimMetric Type
- public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType)
+ public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType)
{
}
@@ -46,7 +49,16 @@ namespace WireMock.Matchers
///
/// The patterns.
/// The SimMetric Type
- 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)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The patterns.
+ /// The SimMetric Type
+ public SimMetricsMatcher([NotNull] AnyOf[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType)
{
}
@@ -57,9 +69,9 @@ namespace WireMock.Matchers
/// The patterns.
/// The SimMetric Type
/// Throw an exception when the internal matching fails because of invalid input.
- public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein, bool throwException = false)
+ public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein, bool throwException = false)
{
- Check.NotNullOrEmpty(patterns, nameof(patterns));
+ Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
@@ -71,9 +83,9 @@ namespace WireMock.Matchers
///
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()
@@ -120,7 +132,7 @@ namespace WireMock.Matchers
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Matchers/WildcardMatcher.cs b/src/WireMock.Net/Matchers/WildcardMatcher.cs
index 6e1b534b..99423d63 100644
--- a/src/WireMock.Net/Matchers/WildcardMatcher.cs
+++ b/src/WireMock.Net/Matchers/WildcardMatcher.cs
@@ -1,6 +1,9 @@
-using System.Linq;
+using System.Linq;
using System.Text.RegularExpressions;
+using AnyOfTypes;
using JetBrains.Annotations;
+using WireMock.Extensions;
+using WireMock.Models;
namespace WireMock.Matchers
{
@@ -10,14 +13,14 @@ namespace WireMock.Matchers
///
public class WildcardMatcher : RegexMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
/// Initializes a new instance of the class.
///
/// The pattern.
/// IgnoreCase
- public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
+ public WildcardMatcher([NotNull] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// The pattern.
/// IgnoreCase
- public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
+ public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
@@ -36,7 +39,7 @@ namespace WireMock.Matchers
///
/// The patterns.
/// IgnoreCase
- public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
+ public WildcardMatcher([NotNull] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
@@ -47,19 +50,30 @@ namespace WireMock.Matchers
/// The patterns.
/// IgnoreCase
/// Throw an exception when the internal matching fails because of invalid input.
- public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) :
- base(matchBehaviour, patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase, throwException)
+ public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false) :
+ base(matchBehaviour, CreateArray(patterns), ignoreCase, throwException)
{
_patterns = patterns;
}
///
- public override string[] GetPatterns()
+ public override AnyOf[] GetPatterns()
{
return _patterns;
}
///
public override string Name => "WildcardMatcher";
+
+ private static AnyOf[] CreateArray(AnyOf[] patterns)
+ {
+ return patterns.Select(pattern => new AnyOf(
+ new StringPattern
+ {
+ Pattern = "^" + Regex.Escape(pattern.GetPattern()).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
+ PatternAsFile = pattern.IsSecond ? pattern.Second.PatternAsFile : null
+ }))
+ .ToArray();
+ }
}
}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/XPathMatcher.cs b/src/WireMock.Net/Matchers/XPathMatcher.cs
index ae8df174..3bfa7352 100644
--- a/src/WireMock.Net/Matchers/XPathMatcher.cs
+++ b/src/WireMock.Net/Matchers/XPathMatcher.cs
@@ -1,7 +1,10 @@
-using System;
+using System;
using System.Linq;
using System.Xml;
+using AnyOfTypes;
using JetBrains.Annotations;
+using WireMock.Extensions;
+using WireMock.Models;
using WireMock.Validation;
#if !NETSTANDARD1_3
using Wmhelp.XPath2;
@@ -15,7 +18,7 @@ namespace WireMock.Matchers
///
public class XPathMatcher : IStringMatcher
{
- private readonly string[] _patterns;
+ private readonly AnyOf[] _patterns;
///
public MatchBehaviour MatchBehaviour { get; }
@@ -27,7 +30,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the class.
///
/// The patterns.
- public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
+ public XPathMatcher([NotNull] params AnyOf[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
@@ -37,7 +40,7 @@ namespace WireMock.Matchers
/// The match behaviour.
/// Throw an exception when the internal matching fails because of invalid input.
/// The patterns.
- public XPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
+ public XPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params AnyOf[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
@@ -56,9 +59,9 @@ namespace WireMock.Matchers
{
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
#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
- 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
}
catch (Exception)
@@ -74,7 +77,7 @@ namespace WireMock.Matchers
}
///
- public string[] GetPatterns()
+ public AnyOf[] GetPatterns()
{
return _patterns;
}
diff --git a/src/WireMock.Net/Models/StringPattern.cs b/src/WireMock.Net/Models/StringPattern.cs
new file mode 100644
index 00000000..5a8a7be5
--- /dev/null
+++ b/src/WireMock.Net/Models/StringPattern.cs
@@ -0,0 +1,18 @@
+namespace WireMock.Models
+{
+ ///
+ /// StringPattern which defines the Pattern as a string, and optionally the filepath pattern file.
+ ///
+ public struct StringPattern
+ {
+ ///
+ /// The pattern as string.
+ ///
+ public string Pattern { get; set; }
+
+ ///
+ /// The filepath (optionally)
+ ///
+ public string PatternAsFile { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs
index c53d3099..38b311c7 100644
--- a/src/WireMock.Net/Serialization/MatcherMapper.cs
+++ b/src/WireMock.Net/Serialization/MatcherMapper.cs
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using AnyOfTypes;
using JetBrains.Annotations;
using SimMetrics.Net;
using WireMock.Admin.Mappings;
+using WireMock.Extensions;
using WireMock.Matchers;
+using WireMock.Models;
using WireMock.Plugin;
using WireMock.Settings;
-using WireMock.Validation;
namespace WireMock.Serialization
{
@@ -17,8 +19,7 @@ namespace WireMock.Serialization
public MatcherMapper(IWireMockServerSettings settings)
{
- Check.NotNull(settings, nameof(settings));
- _settings = settings;
+ _settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public IMatcher[] Map([CanBeNull] IEnumerable matchers)
@@ -36,9 +37,8 @@ namespace WireMock.Serialization
string[] parts = matcher.Name.Split('.');
string matcherName = parts[0];
string matcherType = parts.Length > 1 ? parts[1] : null;
-
- string[] stringPatterns = (matcher.Patterns != null ? matcher.Patterns : new[] { matcher.Pattern }).OfType().ToArray();
- MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
+ var stringPatterns = ParseStringPatterns(matcher);
+ var matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
bool ignoreCase = matcher.IgnoreCase == true;
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
@@ -68,12 +68,12 @@ namespace WireMock.Serialization
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonMatcher":
- object value = matcher.Pattern ?? matcher.Patterns;
- return new JsonMatcher(matchBehaviour, value, ignoreCase, throwExceptionWhenMatcherFails);
+ object valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns;
+ return new JsonMatcher(matchBehaviour, valueForJsonMatcher, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonPartialMatcher":
- object matcherValue = matcher.Pattern ?? matcher.Patterns;
- return new JsonPartialMatcher(matchBehaviour, matcherValue, ignoreCase, throwExceptionWhenMatcherFails);
+ object valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns;
+ return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonPathMatcher":
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
@@ -116,45 +116,85 @@ namespace WireMock.Serialization
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)
{
// If the matcher is a IStringMatcher, get the patterns.
case IStringMatcher stringMatcher:
- patterns = stringMatcher.GetPatterns().Cast