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

View File

@@ -71,7 +71,7 @@ namespace WireMock.Net.ConsoleApplication
.WithStatusCode(200));
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"));
// http://localhost:8080/any/any?start=1000&stop=1&stop=2

View File

@@ -21,6 +21,14 @@
/// </value>
public string Pattern { get; set; }
/// <summary>
/// Gets or sets the patterns.
/// </summary>
/// <value>
/// The patterns.
/// </value>
public string[] Patterns { get; set; }
/// <summary>
/// Gets or sets the ignore case.
/// </summary>

View File

@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -9,17 +10,17 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" />
public class ExactMatcher : IMatcher
{
private readonly string _value;
private readonly string[] _values;
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactMatcher([NotNull] string value)
/// <param name="values">The values.</param>
public ExactMatcher([NotNull] params string[] values)
{
Check.NotNull(value, nameof(value));
Check.NotNull(values, nameof(values));
_value = value;
_values = values;
}
/// <summary>
@@ -29,16 +30,16 @@ namespace WireMock.Matchers
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input)
{
return MatchScores.ToScore(_value.Equals(input));
return MatchScores.ToScore(_values.Select(value => value.Equals(input)));
}
/// <summary>
/// Gets the value.
/// </summary>
/// <returns>Pattern</returns>
public string GetPattern()
/// <returns>Patterns</returns>
public string[] GetPatterns()
{
return _value;
return _values;
}
/// <summary>

View File

@@ -13,10 +13,10 @@
double IsMatch(string input);
/// <summary>
/// Gets the pattern.
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
string GetPattern();
/// <returns>Patterns</returns>
string[] GetPatterns();
/// <summary>
/// Gets the name.

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using WireMock.Validation;
@@ -11,17 +12,17 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" />
public class JsonPathMatcher : IMatcher
{
private readonly string _pattern;
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public JsonPathMatcher([NotNull] string pattern)
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] params string[] patterns)
{
Check.NotNull(pattern, nameof(pattern));
Check.NotNull(patterns, nameof(patterns));
_pattern = pattern;
_patterns = patterns;
}
/// <summary>
@@ -37,9 +38,8 @@ namespace WireMock.Matchers
try
{
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)
{
@@ -48,12 +48,12 @@ namespace WireMock.Matchers
}
/// <summary>
/// Gets the pattern.
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
public string GetPattern()
public string[] GetPatterns()
{
return _pattern;
return _patterns;
}
/// <summary>

View File

@@ -1,4 +1,8 @@
namespace WireMock.Matchers
using System;
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers
{
/// <summary>
/// MatchScores
@@ -29,5 +33,27 @@
{
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;
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -11,25 +12,34 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" />
public class RegexMatcher : IMatcher
{
private readonly string _pattern;
private readonly Regex _expression;
private readonly string[] _patterns;
private readonly Regex[] _expressions;
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</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;
if (ignoreCase)
options |= RegexOptions.IgnoreCase;
_expression = new Regex(_pattern, options);
_expressions = patterns.Select(p => new Regex(p, options)).ToArray();
}
/// <summary>
@@ -44,7 +54,7 @@ namespace WireMock.Matchers
try
{
return MatchScores.ToScore(_expression.IsMatch(input));
return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
}
catch (Exception)
{
@@ -53,12 +63,12 @@ namespace WireMock.Matchers
}
/// <summary>
/// Gets the pattern.
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
public virtual string GetPattern()
public virtual string[] GetPatterns()
{
return _pattern;
return _patterns;
}
/// <summary>

View File

@@ -1,5 +1,7 @@
using JetBrains.Annotations;
using System.Linq;
using JetBrains.Annotations;
using SimMetrics.Net;
using SimMetrics.Net.API;
using SimMetrics.Net.Metric;
using WireMock.Validation;
@@ -11,7 +13,7 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" />
public class SimMetricsMatcher : IMatcher
{
private readonly string _pattern;
private readonly string[] _patterns;
private readonly SimMetricType _simMetricType;
/// <summary>
@@ -19,11 +21,20 @@ namespace WireMock.Matchers
/// </summary>
/// <param name="pattern">The pattern.</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;
}
@@ -33,63 +44,52 @@ namespace WireMock.Matchers
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input)
{
IStringMetric m = GetStringMetricType();
return MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input)));
}
private IStringMetric GetStringMetricType()
{
switch (_simMetricType)
{
case SimMetricType.BlockDistance:
var sim2 = new BlockDistance();
return sim2.GetSimilarity(_pattern, input);
return new BlockDistance();
case SimMetricType.ChapmanLengthDeviation:
var sim3 = new ChapmanLengthDeviation();
return sim3.GetSimilarity(_pattern, input);
return new ChapmanLengthDeviation();
case SimMetricType.CosineSimilarity:
var sim4 = new CosineSimilarity();
return sim4.GetSimilarity(_pattern, input);
return new CosineSimilarity();
case SimMetricType.DiceSimilarity:
var sim5 = new DiceSimilarity();
return sim5.GetSimilarity(_pattern, input);
return new DiceSimilarity();
case SimMetricType.EuclideanDistance:
var sim6 = new EuclideanDistance();
return sim6.GetSimilarity(_pattern, input);
return new EuclideanDistance();
case SimMetricType.JaccardSimilarity:
var sim7 = new JaccardSimilarity();
return sim7.GetSimilarity(_pattern, input);
return new JaccardSimilarity();
case SimMetricType.Jaro:
var sim8 = new Jaro();
return sim8.GetSimilarity(_pattern, input);
return new Jaro();
case SimMetricType.JaroWinkler:
var sim9 = new JaroWinkler();
return sim9.GetSimilarity(_pattern, input);
return new JaroWinkler();
case SimMetricType.MatchingCoefficient:
var sim10 = new MatchingCoefficient();
return sim10.GetSimilarity(_pattern, input);
return new MatchingCoefficient();
case SimMetricType.MongeElkan:
var sim11 = new MongeElkan();
return sim11.GetSimilarity(_pattern, input);
return new MongeElkan();
case SimMetricType.NeedlemanWunch:
var sim12 = new NeedlemanWunch();
return sim12.GetSimilarity(_pattern, input);
return new NeedlemanWunch();
case SimMetricType.OverlapCoefficient:
var sim13 = new OverlapCoefficient();
return sim13.GetSimilarity(_pattern, input);
return new OverlapCoefficient();
case SimMetricType.QGramsDistance:
var sim14 = new QGramsDistance();
return sim14.GetSimilarity(_pattern, input);
return new QGramsDistance();
case SimMetricType.SmithWaterman:
var sim15 = new SmithWaterman();
return sim15.GetSimilarity(_pattern, input);
return new SmithWaterman();
case SimMetricType.SmithWatermanGotoh:
var sim16 = new SmithWatermanGotoh();
return sim16.GetSimilarity(_pattern, input);
return new SmithWatermanGotoh();
case SimMetricType.SmithWatermanGotohWindowedAffine:
var sim17 = new SmithWatermanGotohWindowedAffine();
return sim17.GetSimilarity(_pattern, input);
return new SmithWatermanGotohWindowedAffine();
case SimMetricType.ChapmanMeanLength:
var sim18 = new ChapmanMeanLength();
return sim18.GetSimilarity(_pattern, input);
return new ChapmanMeanLength();
default:
var sim1 = new Levenstein();
return sim1.GetSimilarity(_pattern, input);
return new Levenstein();
}
}
@@ -97,9 +97,9 @@ namespace WireMock.Matchers
/// Gets the pattern.
/// </summary>
/// <returns>Pattern</returns>
public string GetPattern()
public string[] GetPatterns()
{
return _pattern;
return _patterns;
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace WireMock.Matchers
@@ -9,25 +10,34 @@ namespace WireMock.Matchers
/// <seealso cref="IMatcher" />
public class WildcardMatcher : RegexMatcher
{
private readonly string _pattern;
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</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>
/// Gets the pattern.
/// </summary>
/// <returns>Pattern</returns>
public override string GetPattern()
public override string[] GetPatterns()
{
return _pattern;
return _patterns;
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Xml;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -12,17 +13,17 @@ namespace WireMock.Matchers
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class XPathMatcher : IMatcher
{
private readonly string _pattern;
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public XPathMatcher([NotNull] string pattern)
/// <param name="patterns">The patterns.</param>
public XPathMatcher([NotNull] params string[] patterns)
{
Check.NotNull(pattern, nameof(pattern));
Check.NotNull(patterns, nameof(patterns));
_pattern = pattern;
_patterns = patterns;
}
/// <summary>
@@ -38,9 +39,8 @@ namespace WireMock.Matchers
try
{
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)
{
@@ -49,12 +49,12 @@ namespace WireMock.Matchers
}
/// <summary>
/// Gets the pattern.
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
public string GetPattern()
/// <returns>Patterns</returns>
public string[] GetPatterns()
{
return _pattern;
return _patterns;
}
/// <summary>

View File

@@ -334,40 +334,40 @@ namespace WireMock.Server
Priority = mapping.Priority,
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)),
Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : 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)),
Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : 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,
Matchers = Map(hm.Matchers),
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,
Matchers = Map(cm.Matchers),
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,
Values = pm.Values?.ToList(),
Funcs = Map(pm.Funcs)
}).ToList(),
}).ToList() : null,
Body = new BodyModel
{
@@ -400,10 +400,13 @@ namespace WireMock.Server
if (matcher == null)
return null;
var patterns = matcher.GetPatterns();
return new MatcherModel
{
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 matcherType = parts.Length > 1 ? parts[1] : null;
string[] patterns = matcher.Patterns ?? new[] { matcher.Pattern };
switch (matcherName)
{
case "ExactMatcher":
return new ExactMatcher(matcher.Pattern);
return new ExactMatcher(patterns);
case "RegexMatcher":
return new RegexMatcher(matcher.Pattern);
return new RegexMatcher(patterns);
case "JsonPathMatcher":
return new JsonPathMatcher(matcher.Pattern);
return new JsonPathMatcher(patterns);
case "XPathMatcher":
return new XPathMatcher(matcher.Pattern);
case "WildcardMatcher":
return new WildcardMatcher(matcher.Pattern, matcher.IgnoreCase == true);
return new WildcardMatcher(patterns, matcher.IgnoreCase == true);
case "SimMetricsMatcher":
SimMetricType type = SimMetricType.Levenstein;