support multiple patterns

This commit is contained in:
Stef Heyenrath
2017-02-08 19:55:45 +01:00
parent a9a46057be
commit 4919e32264
11 changed files with 171 additions and 111 deletions
@@ -71,7 +71,7 @@ namespace WireMock.Net.ConsoleApplication
.WithStatusCode(200)); .WithStatusCode(200));
server server
.Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher("cat"))) .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new [] { "cat", "dog" })))
.RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200"));
// http://localhost:8080/any/any?start=1000&stop=1&stop=2 // http://localhost:8080/any/any?start=1000&stop=1&stop=2
@@ -21,6 +21,14 @@
/// </value> /// </value>
public string Pattern { get; set; } public string Pattern { get; set; }
/// <summary>
/// Gets or sets the patterns.
/// </summary>
/// <value>
/// The patterns.
/// </value>
public string[] Patterns { get; set; }
/// <summary> /// <summary>
/// Gets or sets the ignore case. /// Gets or sets the ignore case.
/// </summary> /// </summary>
+11 -10
View File
@@ -1,4 +1,5 @@
using JetBrains.Annotations; using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -9,17 +10,17 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class ExactMatcher : IMatcher public class ExactMatcher : IMatcher
{ {
private readonly string _value; private readonly string[] _values;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class. /// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary> /// </summary>
/// <param name="value">The value.</param> /// <param name="values">The values.</param>
public ExactMatcher([NotNull] string value) public ExactMatcher([NotNull] params string[] values)
{ {
Check.NotNull(value, nameof(value)); Check.NotNull(values, nameof(values));
_value = value; _values = values;
} }
/// <summary> /// <summary>
@@ -29,16 +30,16 @@ namespace WireMock.Matchers
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns> /// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input) public double IsMatch(string input)
{ {
return MatchScores.ToScore(_value.Equals(input)); return MatchScores.ToScore(_values.Select(value => value.Equals(input)));
} }
/// <summary> /// <summary>
/// Gets the value. /// Gets the value.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Patterns</returns>
public string GetPattern() public string[] GetPatterns()
{ {
return _value; return _values;
} }
/// <summary> /// <summary>
+3 -3
View File
@@ -13,10 +13,10 @@
double IsMatch(string input); double IsMatch(string input);
/// <summary> /// <summary>
/// Gets the pattern. /// Gets the patterns.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Patterns</returns>
string GetPattern(); string[] GetPatterns();
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
+10 -10
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using WireMock.Validation; using WireMock.Validation;
@@ -11,17 +12,17 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class JsonPathMatcher : IMatcher public class JsonPathMatcher : IMatcher
{ {
private readonly string _pattern; private readonly string[] _patterns;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] string pattern) public JsonPathMatcher([NotNull] params string[] patterns)
{ {
Check.NotNull(pattern, nameof(pattern)); Check.NotNull(patterns, nameof(patterns));
_pattern = pattern; _patterns = patterns;
} }
/// <summary> /// <summary>
@@ -37,9 +38,8 @@ namespace WireMock.Matchers
try try
{ {
JObject o = JObject.Parse(input); JObject o = JObject.Parse(input);
JToken token = o.SelectToken(_pattern);
return MatchScores.ToScore(token != null); return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null));
} }
catch (Exception) catch (Exception)
{ {
@@ -48,12 +48,12 @@ namespace WireMock.Matchers
} }
/// <summary> /// <summary>
/// Gets the pattern. /// Gets the patterns.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Pattern</returns>
public string GetPattern() public string[] GetPatterns()
{ {
return _pattern; return _patterns;
} }
/// <summary> /// <summary>
+27 -1
View File
@@ -1,4 +1,8 @@
namespace WireMock.Matchers using System;
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers
{ {
/// <summary> /// <summary>
/// MatchScores /// MatchScores
@@ -29,5 +33,27 @@
{ {
return value ? Perfect : Mismatch; return value ? Perfect : Mismatch;
} }
/// <summary>
/// Calculates the score from multiple funcs.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>score</returns>
public static double ToScore(IEnumerable<bool> values)
{
var list = values.Select(ToScore).ToList();
return list.Sum() / list.Count;
}
/// <summary>
/// Calculates the score from multiple funcs.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>score</returns>
public static double ToScore(IEnumerable<double> values)
{
var list = values.ToList();
return list.Sum() / list.Count;
}
} }
} }
+20 -10
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
@@ -11,25 +12,34 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class RegexMatcher : IMatcher public class RegexMatcher : IMatcher
{ {
private readonly string _pattern; private readonly string[] _patterns;
private readonly Regex _expression; private readonly Regex[] _expressions;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class. /// Initializes a new instance of the <see cref="RegexMatcher"/> 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 RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new [] { pattern }, ignoreCase )
{ {
Check.NotNull(pattern, nameof(pattern)); }
_pattern = pattern; /// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false)
{
Check.NotNull(patterns, nameof(patterns));
_patterns = patterns;
RegexOptions options = RegexOptions.Compiled; RegexOptions options = RegexOptions.Compiled;
if (ignoreCase) if (ignoreCase)
options |= RegexOptions.IgnoreCase; options |= RegexOptions.IgnoreCase;
_expression = new Regex(_pattern, options); _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
} }
/// <summary> /// <summary>
@@ -44,7 +54,7 @@ namespace WireMock.Matchers
try try
{ {
return MatchScores.ToScore(_expression.IsMatch(input)); return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
} }
catch (Exception) catch (Exception)
{ {
@@ -53,12 +63,12 @@ namespace WireMock.Matchers
} }
/// <summary> /// <summary>
/// Gets the pattern. /// Gets the patterns.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Pattern</returns>
public virtual string GetPattern() public virtual string[] GetPatterns()
{ {
return _pattern; return _patterns;
} }
/// <summary> /// <summary>
+43 -43
View File
@@ -1,5 +1,7 @@
using JetBrains.Annotations; using System.Linq;
using JetBrains.Annotations;
using SimMetrics.Net; using SimMetrics.Net;
using SimMetrics.Net.API;
using SimMetrics.Net.Metric; using SimMetrics.Net.Metric;
using WireMock.Validation; using WireMock.Validation;
@@ -11,7 +13,7 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class SimMetricsMatcher : IMatcher public class SimMetricsMatcher : IMatcher
{ {
private readonly string _pattern; private readonly string[] _patterns;
private readonly SimMetricType _simMetricType; private readonly SimMetricType _simMetricType;
/// <summary> /// <summary>
@@ -19,11 +21,20 @@ 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) public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new [] { pattern }, simMetricType)
{ {
Check.NotNull(pattern, nameof(pattern)); }
_pattern = pattern; /// <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] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein)
{
Check.NotEmpty(patterns, nameof(patterns));
_patterns = patterns;
_simMetricType = simMetricType; _simMetricType = simMetricType;
} }
@@ -33,63 +44,52 @@ namespace WireMock.Matchers
/// <param name="input">The input string</param> /// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns> /// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input) public double IsMatch(string input)
{
IStringMetric m = GetStringMetricType();
return MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input)));
}
private IStringMetric GetStringMetricType()
{ {
switch (_simMetricType) switch (_simMetricType)
{ {
case SimMetricType.BlockDistance: case SimMetricType.BlockDistance:
var sim2 = new BlockDistance(); return new BlockDistance();
return sim2.GetSimilarity(_pattern, input);
case SimMetricType.ChapmanLengthDeviation: case SimMetricType.ChapmanLengthDeviation:
var sim3 = new ChapmanLengthDeviation(); return new ChapmanLengthDeviation();
return sim3.GetSimilarity(_pattern, input);
case SimMetricType.CosineSimilarity: case SimMetricType.CosineSimilarity:
var sim4 = new CosineSimilarity(); return new CosineSimilarity();
return sim4.GetSimilarity(_pattern, input);
case SimMetricType.DiceSimilarity: case SimMetricType.DiceSimilarity:
var sim5 = new DiceSimilarity(); return new DiceSimilarity();
return sim5.GetSimilarity(_pattern, input);
case SimMetricType.EuclideanDistance: case SimMetricType.EuclideanDistance:
var sim6 = new EuclideanDistance(); return new EuclideanDistance();
return sim6.GetSimilarity(_pattern, input);
case SimMetricType.JaccardSimilarity: case SimMetricType.JaccardSimilarity:
var sim7 = new JaccardSimilarity(); return new JaccardSimilarity();
return sim7.GetSimilarity(_pattern, input);
case SimMetricType.Jaro: case SimMetricType.Jaro:
var sim8 = new Jaro(); return new Jaro();
return sim8.GetSimilarity(_pattern, input);
case SimMetricType.JaroWinkler: case SimMetricType.JaroWinkler:
var sim9 = new JaroWinkler(); return new JaroWinkler();
return sim9.GetSimilarity(_pattern, input);
case SimMetricType.MatchingCoefficient: case SimMetricType.MatchingCoefficient:
var sim10 = new MatchingCoefficient(); return new MatchingCoefficient();
return sim10.GetSimilarity(_pattern, input);
case SimMetricType.MongeElkan: case SimMetricType.MongeElkan:
var sim11 = new MongeElkan(); return new MongeElkan();
return sim11.GetSimilarity(_pattern, input);
case SimMetricType.NeedlemanWunch: case SimMetricType.NeedlemanWunch:
var sim12 = new NeedlemanWunch(); return new NeedlemanWunch();
return sim12.GetSimilarity(_pattern, input);
case SimMetricType.OverlapCoefficient: case SimMetricType.OverlapCoefficient:
var sim13 = new OverlapCoefficient(); return new OverlapCoefficient();
return sim13.GetSimilarity(_pattern, input);
case SimMetricType.QGramsDistance: case SimMetricType.QGramsDistance:
var sim14 = new QGramsDistance(); return new QGramsDistance();
return sim14.GetSimilarity(_pattern, input);
case SimMetricType.SmithWaterman: case SimMetricType.SmithWaterman:
var sim15 = new SmithWaterman(); return new SmithWaterman();
return sim15.GetSimilarity(_pattern, input);
case SimMetricType.SmithWatermanGotoh: case SimMetricType.SmithWatermanGotoh:
var sim16 = new SmithWatermanGotoh(); return new SmithWatermanGotoh();
return sim16.GetSimilarity(_pattern, input);
case SimMetricType.SmithWatermanGotohWindowedAffine: case SimMetricType.SmithWatermanGotohWindowedAffine:
var sim17 = new SmithWatermanGotohWindowedAffine(); return new SmithWatermanGotohWindowedAffine();
return sim17.GetSimilarity(_pattern, input);
case SimMetricType.ChapmanMeanLength: case SimMetricType.ChapmanMeanLength:
var sim18 = new ChapmanMeanLength(); return new ChapmanMeanLength();
return sim18.GetSimilarity(_pattern, input);
default: default:
var sim1 = new Levenstein(); return new Levenstein();
return sim1.GetSimilarity(_pattern, input);
} }
} }
@@ -97,9 +97,9 @@ namespace WireMock.Matchers
/// Gets the pattern. /// Gets the pattern.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Pattern</returns>
public string GetPattern() public string[] GetPatterns()
{ {
return _pattern; return _patterns;
} }
/// <summary> /// <summary>
+16 -6
View File
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions; using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace WireMock.Matchers namespace WireMock.Matchers
@@ -9,25 +10,34 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class WildcardMatcher : RegexMatcher public class WildcardMatcher : RegexMatcher
{ {
private readonly string _pattern; private readonly string[] _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) : base("^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", ignoreCase) public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new [] { pattern }, ignoreCase)
{ {
_pattern = pattern; }
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : base(patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase)
{
_patterns = patterns;
} }
/// <summary> /// <summary>
/// Gets the pattern. /// Gets the pattern.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Pattern</returns>
public override string GetPattern() public override string[] GetPatterns()
{ {
return _pattern; return _patterns;
} }
/// <summary> /// <summary>
+11 -11
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Xml; using System.Xml;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
@@ -12,17 +13,17 @@ namespace WireMock.Matchers
/// <seealso cref="WireMock.Matchers.IMatcher" /> /// <seealso cref="WireMock.Matchers.IMatcher" />
public class XPathMatcher : IMatcher public class XPathMatcher : IMatcher
{ {
private readonly string _pattern; private readonly string[] _patterns;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class. /// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="patterns">The patterns.</param>
public XPathMatcher([NotNull] string pattern) public XPathMatcher([NotNull] params string[] patterns)
{ {
Check.NotNull(pattern, nameof(pattern)); Check.NotNull(patterns, nameof(patterns));
_pattern = pattern; _patterns = patterns;
} }
/// <summary> /// <summary>
@@ -38,9 +39,8 @@ namespace WireMock.Matchers
try try
{ {
var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
object result = nav.XPath2Evaluate($"boolean({_pattern})");
return MatchScores.ToScore(true.Equals(result)); return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
} }
catch (Exception) catch (Exception)
{ {
@@ -49,12 +49,12 @@ namespace WireMock.Matchers
} }
/// <summary> /// <summary>
/// Gets the pattern. /// Gets the patterns.
/// </summary> /// </summary>
/// <returns>Pattern</returns> /// <returns>Patterns</returns>
public string GetPattern() public string[] GetPatterns()
{ {
return _pattern; return _patterns;
} }
/// <summary> /// <summary>
@@ -334,40 +334,40 @@ namespace WireMock.Server
Priority = mapping.Priority, Priority = mapping.Priority,
Request = new RequestModel Request = new RequestModel
{ {
Path = pathMatchers != null ? new PathModel Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
{ {
Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)), Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs)) Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : null, } : null,
Url = urlMatchers != null ? new UrlModel Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
{ {
Matchers = Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)), Matchers = Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs)) Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : null, } : null,
Methods = methodMatcher != null ? methodMatcher.Methods : new[] { "any" }, Methods = methodMatcher?.Methods,
Headers = headerMatchers?.Select(hm => new HeaderModel Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers?.Select(hm => new HeaderModel
{ {
Name = hm.Name, Name = hm.Name,
Matchers = Map(hm.Matchers), Matchers = Map(hm.Matchers),
Funcs = Map(hm.Funcs) Funcs = Map(hm.Funcs)
}).ToList(), }).ToList() : null,
Cookies = cookieMatchers?.Select(cm => new CookieModel Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers?.Select(cm => new CookieModel
{ {
Name = cm.Name, Name = cm.Name,
Matchers = Map(cm.Matchers), Matchers = Map(cm.Matchers),
Funcs = Map(cm.Funcs) Funcs = Map(cm.Funcs)
}).ToList(), }).ToList() : null,
Params = paramsMatchers?.Select(pm => new ParamModel Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers?.Select(pm => new ParamModel
{ {
Name = pm.Key, Name = pm.Key,
Values = pm.Values?.ToList(), Values = pm.Values?.ToList(),
Funcs = Map(pm.Funcs) Funcs = Map(pm.Funcs)
}).ToList(), }).ToList() : null,
Body = new BodyModel Body = new BodyModel
{ {
@@ -400,10 +400,13 @@ namespace WireMock.Server
if (matcher == null) if (matcher == null)
return null; return null;
var patterns = matcher.GetPatterns();
return new MatcherModel return new MatcherModel
{ {
Name = matcher.GetName(), Name = matcher.GetName(),
Pattern = matcher.GetPattern() Pattern = patterns.Length == 1 ? patterns.First() : null,
Patterns = patterns.Length > 1 ? patterns : null
}; };
} }
@@ -429,22 +432,24 @@ namespace WireMock.Server
string matcherName = parts[0]; string matcherName = parts[0];
string matcherType = parts.Length > 1 ? parts[1] : null; string matcherType = parts.Length > 1 ? parts[1] : null;
string[] patterns = matcher.Patterns ?? new[] { matcher.Pattern };
switch (matcherName) switch (matcherName)
{ {
case "ExactMatcher": case "ExactMatcher":
return new ExactMatcher(matcher.Pattern); return new ExactMatcher(patterns);
case "RegexMatcher": case "RegexMatcher":
return new RegexMatcher(matcher.Pattern); return new RegexMatcher(patterns);
case "JsonPathMatcher": case "JsonPathMatcher":
return new JsonPathMatcher(matcher.Pattern); return new JsonPathMatcher(patterns);
case "XPathMatcher": case "XPathMatcher":
return new XPathMatcher(matcher.Pattern); return new XPathMatcher(matcher.Pattern);
case "WildcardMatcher": case "WildcardMatcher":
return new WildcardMatcher(matcher.Pattern, matcher.IgnoreCase == true); return new WildcardMatcher(patterns, matcher.IgnoreCase == true);
case "SimMetricsMatcher": case "SimMetricsMatcher":
SimMetricType type = SimMetricType.Levenstein; SimMetricType type = SimMetricType.Levenstein;