Json fixes (#91) (#92)

* Fixes for JsonPath

* More tests

* Fixes + added tests
This commit is contained in:
Stef Heyenrath
2018-02-23 12:29:43 +00:00
committed by GitHub
parent 1ffd56701c
commit 0c25b2e9f2
27 changed files with 434 additions and 250 deletions

View File

@@ -41,6 +41,15 @@ namespace WireMock.Net.ConsoleApplication
// .RespondWith(Response.Create() // .RespondWith(Response.Create()
// .WithProxy("http://restcountries.eu")); // .WithProxy("http://restcountries.eu"));
server
.Given(Request
.Create()
.WithPath("/jsonthings")
.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
.UsingPut())
.RespondWith(Response.Create()
.WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}"));
server server
.Given(Request .Given(Request
.Create() .Create()

View File

@@ -7,8 +7,8 @@ namespace WireMock.Matchers
/// <summary> /// <summary>
/// ExactMatcher /// ExactMatcher
/// </summary> /// </summary>
/// <seealso cref="IMatcher" /> /// <seealso cref="IStringMatcher" />
public class ExactMatcher : IMatcher public class ExactMatcher : IStringMatcher
{ {
private readonly string[] _values; private readonly string[] _values;
@@ -23,29 +23,19 @@ namespace WireMock.Matchers
_values = values; _values = values;
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <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(_values.Select(value => value.Equals(input))); return MatchScores.ToScore(_values.Select(value => value.Equals(input)));
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
/// Gets the value.
/// </summary>
/// <returns>Patterns</returns>
public string[] GetPatterns() public string[] GetPatterns()
{ {
return _values; return _values;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
public string GetName() public string GetName()
{ {
return "ExactMatcher"; return "ExactMatcher";

View File

@@ -0,0 +1,46 @@
using System.Linq;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IObjectMatcher" />
public class ExactObjectMatcher : IObjectMatcher
{
private readonly object _object;
private readonly byte[] _bytes;
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] object value)
{
_object = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] byte[] value)
{
_bytes = value;
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool equals = _object != null ? Equals(_object, input) : _bytes.SequenceEqual((byte[])input);
return MatchScores.ToScore(equals);
}
/// <inheritdoc cref="IMatcher.GetName"/>
public string GetName()
{
return "ExactObjectMatcher";
}
}
}

View File

@@ -5,19 +5,6 @@
/// </summary> /// </summary>
public interface IMatcher public interface IMatcher
{ {
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(string input);
/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Patterns</returns>
string[] GetPatterns();
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
/// </summary> /// </summary>

View File

@@ -0,0 +1,15 @@
namespace WireMock.Matchers
{
/// <summary>
/// IObjectMatcher
/// </summary>
public interface IObjectMatcher : IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(object input);
}
}

View File

@@ -0,0 +1,21 @@
namespace WireMock.Matchers
{
/// <summary>
/// IStringMatcher
/// </summary>
public interface IStringMatcher : IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(string input);
/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Patterns</returns>
string[] GetPatterns();
}
}

View File

@@ -10,8 +10,9 @@ namespace WireMock.Matchers
/// JSONPathMatcher /// JSONPathMatcher
/// </summary> /// </summary>
/// <seealso cref="IMatcher" /> /// <seealso cref="IMatcher" />
public class JsonPathMatcher : IMatcher public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{ {
// private readonly object _jsonPattern;
private readonly string[] _patterns; private readonly string[] _patterns;
/// <summary> /// <summary>
@@ -25,15 +26,20 @@ namespace WireMock.Matchers
_patterns = patterns; _patterns = patterns;
} }
/// <summary> //public JsonPathMatcher([NotNull] object jsonPattern)
/// Determines whether the specified input is match. //{
/// </summary> // Check.NotNull(jsonPattern, nameof(jsonPattern));
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns> // _jsonPattern = jsonPattern;
//}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input) public double IsMatch(string input)
{ {
if (input == null) if (input == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
try try
{ {
@@ -47,19 +53,33 @@ namespace WireMock.Matchers
} }
} }
/// <summary> /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
/// Gets the patterns. public double IsMatch(object input)
/// </summary> {
/// <returns>Pattern</returns> if (input == null)
{
return MatchScores.Mismatch;
}
try
{
var o = input as JObject ?? JObject.FromObject(input);
return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null));
}
catch (Exception)
{
return MatchScores.Mismatch;
}
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns() public string[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
public string GetName() public string GetName()
{ {
return "JsonPathMatcher"; return "JsonPathMatcher";

View File

@@ -9,8 +9,8 @@ namespace WireMock.Matchers
/// <summary> /// <summary>
/// Regular Expression Matcher /// Regular Expression Matcher
/// </summary> /// </summary>
/// <seealso cref="IMatcher" /> /// <seealso cref="IStringMatcher" />
public class RegexMatcher : IMatcher public class RegexMatcher : IStringMatcher
{ {
private readonly string[] _patterns; private readonly string[] _patterns;
private readonly Regex[] _expressions; private readonly Regex[] _expressions;
@@ -37,20 +37,20 @@ namespace WireMock.Matchers
RegexOptions options = RegexOptions.Compiled; RegexOptions options = RegexOptions.Compiled;
if (ignoreCase) if (ignoreCase)
{
options |= RegexOptions.IgnoreCase; options |= RegexOptions.IgnoreCase;
}
_expressions = patterns.Select(p => new Regex(p, options)).ToArray(); _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input) public double IsMatch(string input)
{ {
if (input == null) if (input == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
try try
{ {
@@ -62,21 +62,13 @@ namespace WireMock.Matchers
} }
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
public virtual string[] GetPatterns() public virtual string[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>
/// Name
/// </returns>
public virtual string GetName() public virtual string GetName()
{ {
return "RegexMatcher"; return "RegexMatcher";

View File

@@ -9,21 +9,21 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
public class RequestMessageBodyMatcher : IRequestMatcher public class RequestMessageBodyMatcher : IRequestMatcher
{ {
/// <summary>
/// The body as byte[].
/// </summary>
private readonly byte[] _bodyData;
/// <summary> /// <summary>
/// The body function /// The body function
/// </summary> /// </summary>
public Func<string, bool> Func { get; } public Func<string, bool> Func { get; }
/// <summary> /// <summary>
/// The body data function /// The body data function for byte[]
/// </summary> /// </summary>
public Func<byte[], bool> DataFunc { get; } public Func<byte[], bool> DataFunc { get; }
/// <summary>
/// The body data function for json
/// </summary>
public Func<object, bool> JsonFunc { get; }
/// <summary> /// <summary>
/// The matcher. /// The matcher.
/// </summary> /// </summary>
@@ -32,9 +32,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="body"> /// <param name="body">The body.</param>
/// The body Regex pattern.
/// </param>
public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body)) public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body))
{ {
} }
@@ -42,21 +40,23 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="body"> /// <param name="body">The body.</param>
/// The body Regex pattern. public RequestMessageBodyMatcher([NotNull] byte[] body) : this(new ExactObjectMatcher(body))
/// </param>
public RequestMessageBodyMatcher([NotNull] byte[] body)
{ {
Check.NotNull(body, nameof(body));
_bodyData = body;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="body">The body.</param>
/// The body func. public RequestMessageBodyMatcher([NotNull] object body) : this(new ExactObjectMatcher(body))
/// </param> {
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="func">The function.</param>
public RequestMessageBodyMatcher([NotNull] Func<string, bool> func) public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
@@ -66,9 +66,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">The function.</param>
/// The body func.
/// </param>
public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func) public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
@@ -78,23 +76,24 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="matcher"> /// <param name="func">The function.</param>
/// The body matcher. public RequestMessageBodyMatcher([NotNull] Func<object, bool> func)
/// </param> {
Check.NotNull(func, nameof(func));
JsonFunc = func;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="matcher">The matcher.</param>
public RequestMessageBodyMatcher([NotNull] IMatcher matcher) public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
{ {
Check.NotNull(matcher, nameof(matcher)); Check.NotNull(matcher, nameof(matcher));
Matcher = matcher; Matcher = matcher;
} }
/// <summary> /// <see cref="IRequestMatcher.GetMatchingScore"/>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns>
/// A value between 0.0 - 1.0 of the similarity.
/// </returns>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{ {
double score = IsMatch(requestMessage); double score = IsMatch(requestMessage);
@@ -103,17 +102,43 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Matcher != null) if (requestMessage.Body != null)
return Matcher.IsMatch(requestMessage.Body); {
var stringMatcher = Matcher as IStringMatcher;
if (stringMatcher != null)
{
return stringMatcher.IsMatch(requestMessage.Body);
}
}
if (_bodyData != null) var objectMatcher = Matcher as IObjectMatcher;
return MatchScores.ToScore(requestMessage.BodyAsBytes == _bodyData); if (objectMatcher != null)
{
if (requestMessage.BodyAsJson != null)
{
return objectMatcher.IsMatch(requestMessage.BodyAsJson);
}
if (requestMessage.BodyAsBytes != null)
{
return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
}
}
if (Func != null) if (Func != null)
{
return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body)); return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
}
if (DataFunc != null && requestMessage.BodyAsBytes != null) if (DataFunc != null)
{
return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes)); return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes));
}
if (JsonFunc != null)
{
return MatchScores.ToScore(requestMessage.BodyAsJson != null && JsonFunc(requestMessage.BodyAsJson));
}
return MatchScores.Mismatch; return MatchScores.Mismatch;
} }

View File

@@ -14,7 +14,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The matchers. /// The matchers.
/// </summary> /// </summary>
public IReadOnlyList<IMatcher> Matchers { get; } public IReadOnlyList<IStringMatcher> Matchers { get; }
/// <summary> /// <summary>
/// The ClientIP functions. /// The ClientIP functions.
@@ -25,7 +25,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
/// </summary> /// </summary>
/// <param name="clientIPs">The clientIPs.</param> /// <param name="clientIPs">The clientIPs.</param>
public RequestMessageClientIPMatcher([NotNull] params string[] clientIPs) : this(clientIPs.Select(ip => new WildcardMatcher(ip)).Cast<IMatcher>().ToArray()) public RequestMessageClientIPMatcher([NotNull] params string[] clientIPs) : this(clientIPs.Select(ip => new WildcardMatcher(ip)).Cast<IStringMatcher>().ToArray())
{ {
} }
@@ -33,7 +33,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
public RequestMessageClientIPMatcher([NotNull] params IMatcher[] matchers) public RequestMessageClientIPMatcher([NotNull] params IStringMatcher[] matchers)
{ {
Check.NotNull(matchers, nameof(matchers)); Check.NotNull(matchers, nameof(matchers));
Matchers = matchers; Matchers = matchers;
@@ -59,10 +59,14 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Matchers != null) if (Matchers != null)
{
return Matchers.Max(matcher => matcher.IsMatch(requestMessage.ClientIP)); return Matchers.Max(matcher => matcher.IsMatch(requestMessage.ClientIP));
}
if (Funcs != null) if (Funcs != null)
{
return MatchScores.ToScore(requestMessage.ClientIP != null && Funcs.Any(func => func(requestMessage.ClientIP))); return MatchScores.ToScore(requestMessage.ClientIP != null && Funcs.Any(func => func(requestMessage.ClientIP)));
}
return MatchScores.Mismatch; return MatchScores.Mismatch;
} }

View File

@@ -24,7 +24,7 @@ namespace WireMock.Matchers.Request
/// <value> /// <value>
/// The matchers. /// The matchers.
/// </value> /// </value>
public IMatcher[] Matchers { get; } public IStringMatcher[] Matchers { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
@@ -38,7 +38,7 @@ namespace WireMock.Matchers.Request
Check.NotNull(pattern, nameof(pattern)); Check.NotNull(pattern, nameof(pattern));
Name = name; Name = name;
Matchers = new IMatcher[] { new WildcardMatcher(pattern, ignoreCase) }; Matchers = new IStringMatcher[] { new WildcardMatcher(pattern, ignoreCase) };
} }
/// <summary> /// <summary>
@@ -46,7 +46,7 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
public RequestMessageCookieMatcher([NotNull] string name, [NotNull] params IMatcher[] matchers) public RequestMessageCookieMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers)
{ {
Check.NotNull(name, nameof(name)); Check.NotNull(name, nameof(name));
Check.NotNull(matchers, nameof(matchers)); Check.NotNull(matchers, nameof(matchers));
@@ -83,16 +83,24 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (requestMessage.Cookies == null) if (requestMessage.Cookies == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
if (Funcs != null) if (Funcs != null)
{
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Cookies))); return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Cookies)));
}
if (Matchers == null) if (Matchers == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
if (!requestMessage.Cookies.ContainsKey(Name)) if (!requestMessage.Cookies.ContainsKey(Name))
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
string value = requestMessage.Cookies[Name]; string value = requestMessage.Cookies[Name];
return Matchers.Max(m => m.IsMatch(value)); return Matchers.Max(m => m.IsMatch(value));

View File

@@ -26,7 +26,7 @@ namespace WireMock.Matchers.Request
/// <value> /// <value>
/// The matchers. /// The matchers.
/// </value> /// </value>
public IMatcher[] Matchers { get; } public IStringMatcher[] Matchers { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
@@ -40,7 +40,7 @@ namespace WireMock.Matchers.Request
Check.NotNull(pattern, nameof(pattern)); Check.NotNull(pattern, nameof(pattern));
Name = name; Name = name;
Matchers = new IMatcher[] { new WildcardMatcher(pattern, ignoreCase) }; Matchers = new IStringMatcher[] { new WildcardMatcher(pattern, ignoreCase) };
} }
/// <summary> /// <summary>
@@ -55,7 +55,7 @@ namespace WireMock.Matchers.Request
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
Name = name; Name = name;
Matchers = patterns.Select(pattern => new WildcardMatcher(pattern, ignoreCase)).Cast<IMatcher>().ToArray(); Matchers = patterns.Select(pattern => new WildcardMatcher(pattern, ignoreCase)).Cast<IStringMatcher>().ToArray();
} }
/// <summary> /// <summary>
@@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IMatcher[] matchers) public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers)
{ {
Check.NotNull(name, nameof(name)); Check.NotNull(name, nameof(name));
Check.NotNull(matchers, nameof(matchers)); Check.NotNull(matchers, nameof(matchers));
@@ -93,16 +93,24 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (requestMessage.Headers == null) if (requestMessage.Headers == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
if (Funcs != null) if (Funcs != null)
{
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray())))); return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray()))));
}
if (Matchers == null) if (Matchers == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
if (!requestMessage.Headers.ContainsKey(Name)) if (!requestMessage.Headers.ContainsKey(Name))
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
WireMockList<string> list = requestMessage.Headers[Name]; WireMockList<string> list = requestMessage.Headers[Name];
return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ? return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?

View File

@@ -14,7 +14,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The matcher. /// The matcher.
/// </summary> /// </summary>
public IReadOnlyList<IMatcher> Matchers { get; } public IReadOnlyList<IStringMatcher> Matchers { get; }
/// <summary> /// <summary>
/// The path functions /// The path functions
@@ -25,7 +25,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="paths">The paths.</param> /// <param name="paths">The paths.</param>
public RequestMessagePathMatcher([NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(path)).Cast<IMatcher>().ToArray()) public RequestMessagePathMatcher([NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(path)).Cast<IStringMatcher>().ToArray())
{ {
} }
@@ -33,7 +33,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
public RequestMessagePathMatcher([NotNull] params IMatcher[] matchers) public RequestMessagePathMatcher([NotNull] params IStringMatcher[] matchers)
{ {
Check.NotNull(matchers, nameof(matchers)); Check.NotNull(matchers, nameof(matchers));
Matchers = matchers; Matchers = matchers;
@@ -59,10 +59,14 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Matchers != null) if (Matchers != null)
{
return Matchers.Max(m => m.IsMatch(requestMessage.Path)); return Matchers.Max(m => m.IsMatch(requestMessage.Path));
}
if (Funcs != null) if (Funcs != null)
{
return MatchScores.ToScore(requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path))); return MatchScores.ToScore(requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path)));
}
return MatchScores.Mismatch; return MatchScores.Mismatch;
} }

View File

@@ -14,7 +14,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The matchers. /// The matchers.
/// </summary> /// </summary>
public IReadOnlyList<IMatcher> Matchers { get; } public IReadOnlyList<IStringMatcher> Matchers { get; }
/// <summary> /// <summary>
/// The url functions. /// The url functions.
@@ -25,7 +25,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary> /// </summary>
/// <param name="urls">The urls.</param> /// <param name="urls">The urls.</param>
public RequestMessageUrlMatcher([NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(url)).Cast<IMatcher>().ToArray()) public RequestMessageUrlMatcher([NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(url)).Cast<IStringMatcher>().ToArray())
{ {
} }
@@ -33,7 +33,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
public RequestMessageUrlMatcher([NotNull] params IMatcher[] matchers) public RequestMessageUrlMatcher([NotNull] params IStringMatcher[] matchers)
{ {
Check.NotNull(matchers, nameof(matchers)); Check.NotNull(matchers, nameof(matchers));
Matchers = matchers; Matchers = matchers;
@@ -59,10 +59,14 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Matchers != null) if (Matchers != null)
{
return Matchers.Max(matcher => matcher.IsMatch(requestMessage.Url)); return Matchers.Max(matcher => matcher.IsMatch(requestMessage.Url));
}
if (Funcs != null) if (Funcs != null)
{
return MatchScores.ToScore(requestMessage.Url != null && Funcs.Any(func => func(requestMessage.Url))); return MatchScores.ToScore(requestMessage.Url != null && Funcs.Any(func => func(requestMessage.Url)));
}
return MatchScores.Mismatch; return MatchScores.Mismatch;
} }

View File

@@ -10,8 +10,8 @@ namespace WireMock.Matchers
/// <summary> /// <summary>
/// SimMetricsMatcher /// SimMetricsMatcher
/// </summary> /// </summary>
/// <seealso cref="IMatcher" /> /// <seealso cref="IStringMatcher" />
public class SimMetricsMatcher : IMatcher public class SimMetricsMatcher : IStringMatcher
{ {
private readonly string[] _patterns; private readonly string[] _patterns;
private readonly SimMetricType _simMetricType; private readonly SimMetricType _simMetricType;
@@ -38,11 +38,7 @@ namespace WireMock.Matchers
_simMetricType = simMetricType; _simMetricType = simMetricType;
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input string</param>
/// <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(); IStringMetric m = GetStringMetricType();
@@ -93,19 +89,13 @@ namespace WireMock.Matchers
} }
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
/// Gets the pattern.
/// </summary>
/// <returns>Pattern</returns>
public string[] GetPatterns() public string[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
public string GetName() public string GetName()
{ {
return $"SimMetricsMatcher.{_simMetricType}"; return $"SimMetricsMatcher.{_simMetricType}";

View File

@@ -7,7 +7,7 @@ namespace WireMock.Matchers
/// <summary> /// <summary>
/// WildcardMatcher /// WildcardMatcher
/// </summary> /// </summary>
/// <seealso cref="IMatcher" /> /// <seealso cref="RegexMatcher" />
public class WildcardMatcher : RegexMatcher public class WildcardMatcher : RegexMatcher
{ {
private readonly string[] _patterns; private readonly string[] _patterns;
@@ -31,21 +31,13 @@ namespace WireMock.Matchers
_patterns = patterns; _patterns = patterns;
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
/// Gets the pattern.
/// </summary>
/// <returns>Pattern</returns>
public override string[] GetPatterns() public override string[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>
/// Name
/// </returns>
public override string GetName() public override string GetName()
{ {
return "WildcardMatcher"; return "WildcardMatcher";

View File

@@ -12,8 +12,8 @@ namespace WireMock.Matchers
/// <summary> /// <summary>
/// XPath2Matcher /// XPath2Matcher
/// </summary> /// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" /> /// <seealso cref="IStringMatcher" />
public class XPathMatcher : IMatcher public class XPathMatcher : IStringMatcher
{ {
private readonly string[] _patterns; private readonly string[] _patterns;
@@ -28,15 +28,13 @@ namespace WireMock.Matchers
_patterns = patterns; _patterns = patterns;
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.IsMatch"/>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
public double IsMatch(string input) public double IsMatch(string input)
{ {
if (input == null) if (input == null)
{
return MatchScores.Mismatch; return MatchScores.Mismatch;
}
try try
{ {
@@ -53,19 +51,13 @@ namespace WireMock.Matchers
} }
} }
/// <summary> /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
/// Gets the patterns.
/// </summary>
/// <returns>Patterns</returns>
public string[] GetPatterns() public string[] GetPatterns()
{ {
return _patterns; return _patterns;
} }
/// <summary> /// <inheritdoc cref="IMatcher.GetName"/>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
public string GetName() public string GetName()
{ {
return "XPathMatcher"; return "XPathMatcher";

View File

@@ -17,7 +17,7 @@ namespace WireMock.Owin
{ {
public TimeSpan? RequestProcessingDelay { get; set; } public TimeSpan? RequestProcessingDelay { get; set; }
public IMatcher AuthorizationMatcher { get; set; } public IStringMatcher AuthorizationMatcher { get; set; }
public bool AllowPartialMapping { get; set; } public bool AllowPartialMapping { get; set; }

View File

@@ -10,38 +10,52 @@ namespace WireMock.RequestBuilders
public interface IBodyRequestBuilder public interface IBodyRequestBuilder
{ {
/// <summary> /// <summary>
/// The with body. /// WithBody: IMatcher
/// </summary> /// </summary>
/// <param name="matcher">The matcher.</param> /// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] IMatcher matcher); IRequestBuilder WithBody([NotNull] IMatcher matcher);
/// <summary> /// <summary>
/// The with body. /// WithBody: Body as string
/// </summary> /// </summary>
/// <param name="body">The body.</param> /// <param name="body">The body.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody(string body); IRequestBuilder WithBody(string body);
/// <summary> /// <summary>
/// The with body byte[]. /// WithBody: Body as byte[]
/// </summary> /// </summary>
/// <param name="body">The body as byte[].</param> /// <param name="body">The body.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody(byte[] body); IRequestBuilder WithBody(byte[] body);
/// <summary> /// <summary>
/// The with body string func. /// WithBody: Body as object
/// </summary> /// </summary>
/// <param name="body">The body string function.</param> /// <param name="body">The body.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<string, bool> body); IRequestBuilder WithBody(object body);
/// <summary> /// <summary>
/// The with body byte[] func. ///WithBody: func (string)
/// </summary> /// </summary>
/// <param name="body">The body byte[] function.</param> /// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<byte[], bool> body); IRequestBuilder WithBody([NotNull] Func<string, bool> func);
/// <summary>
///WithBody: func (byte[])
/// </summary>
/// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<byte[], bool> func);
/// <summary>
///WithBody: func (object)
/// </summary>
/// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<object, bool> func);
} }
} }

View File

@@ -14,7 +14,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithClientIP([NotNull] params IMatcher[] matchers); IRequestBuilder WithClientIP([NotNull] params IStringMatcher[] matchers);
/// <summary> /// <summary>
/// The with ClientIP. /// The with ClientIP.

View File

@@ -35,7 +35,7 @@ namespace WireMock.RequestBuilders
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithHeader([NotNull] string name, [NotNull] params IMatcher[] matchers); IRequestBuilder WithHeader([NotNull] string name, [NotNull] params IStringMatcher[] matchers);
/// <summary> /// <summary>
/// The with header. /// The with header.
@@ -59,7 +59,7 @@ namespace WireMock.RequestBuilders
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithCookie([NotNull] string name, [NotNull] params IMatcher[] matchers); IRequestBuilder WithCookie([NotNull] string name, [NotNull] params IStringMatcher[] matchers);
/// <summary> /// <summary>
/// The with cookie. /// The with cookie.

View File

@@ -14,7 +14,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithPath([NotNull] params IMatcher[] matchers); IRequestBuilder WithPath([NotNull] params IStringMatcher[] matchers);
/// <summary> /// <summary>
/// The with path. /// The with path.
@@ -35,7 +35,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithUrl([NotNull] params IMatcher[] matchers); IRequestBuilder WithUrl([NotNull] params IStringMatcher[] matchers);
/// <summary> /// <summary>
/// The with url. /// The with url.

View File

@@ -59,7 +59,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithClientIP(params IMatcher[] matchers) public IRequestBuilder WithClientIP(params IStringMatcher[] matchers)
{ {
Check.NotNullOrEmpty(matchers, nameof(matchers)); Check.NotNullOrEmpty(matchers, nameof(matchers));
@@ -98,7 +98,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithPath(params IMatcher[] matchers) public IRequestBuilder WithPath(params IStringMatcher[] matchers)
{ {
Check.NotNullOrEmpty(matchers, nameof(matchers)); Check.NotNullOrEmpty(matchers, nameof(matchers));
@@ -137,7 +137,7 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithUrl(params IMatcher[] matchers) public IRequestBuilder WithUrl(params IStringMatcher[] matchers)
{ {
Check.NotNullOrEmpty(matchers, nameof(matchers)); Check.NotNullOrEmpty(matchers, nameof(matchers));
@@ -234,67 +234,28 @@ namespace WireMock.RequestBuilders
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(string)"/>
/// The with body.
/// </summary>
/// <param name="body">
/// The body.
/// </param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(string body) public IRequestBuilder WithBody(string body)
{ {
_requestMatchers.Add(new RequestMessageBodyMatcher(body)); _requestMatchers.Add(new RequestMessageBodyMatcher(body));
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(byte[])"/>
/// The with body byte[].
/// </summary>
/// <param name="body">
/// The body as byte[].
/// </param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(byte[] body) public IRequestBuilder WithBody(byte[] body)
{ {
_requestMatchers.Add(new RequestMessageBodyMatcher(body)); _requestMatchers.Add(new RequestMessageBodyMatcher(body));
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(object)"/>
/// The with body. public IRequestBuilder WithBody(object body)
/// </summary>
/// <param name="func">
/// The body function.
/// </param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); _requestMatchers.Add(new RequestMessageBodyMatcher(body));
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(IMatcher)"/>
/// The with body.
/// </summary>
/// <param name="func">
/// The body function.
/// </param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(Func<byte[], bool> func)
{
Check.NotNull(func, nameof(func));
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this;
}
/// <summary>
/// The with body.
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IRequestBuilder" />.</returns>
public IRequestBuilder WithBody(IMatcher matcher) public IRequestBuilder WithBody(IMatcher matcher)
{ {
Check.NotNull(matcher, nameof(matcher)); Check.NotNull(matcher, nameof(matcher));
@@ -303,16 +264,34 @@ namespace WireMock.RequestBuilders
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{string, bool})"/>
/// The with parameters. public IRequestBuilder WithBody(Func<string, bool> func)
/// </summary> {
/// <param name="key"> Check.NotNull(func, nameof(func));
/// The key.
/// </param> _requestMatchers.Add(new RequestMessageBodyMatcher(func));
/// <param name="values"> return this;
/// The values. }
/// </param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{byte[], bool})"/>
public IRequestBuilder WithBody(Func<byte[], bool> func)
{
Check.NotNull(func, nameof(func));
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this;
}
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{object, bool})"/>
public IRequestBuilder WithBody(Func<object, bool> func)
{
Check.NotNull(func, nameof(func));
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this;
}
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, string[])"/>
public IRequestBuilder WithParam(string key, params string[] values) public IRequestBuilder WithParam(string key, params string[] values)
{ {
Check.NotNull(key, nameof(key)); Check.NotNull(key, nameof(key));
@@ -321,11 +300,7 @@ namespace WireMock.RequestBuilders
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IParamsRequestBuilder.WithParam(Func{IDictionary{string, WireMockList{string}}, bool}[])"/>
/// The with parameters.
/// </summary>
/// <param name="funcs">The funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs) public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
{ {
Check.NotNullOrEmpty(funcs, nameof(funcs)); Check.NotNullOrEmpty(funcs, nameof(funcs));
@@ -360,7 +335,7 @@ namespace WireMock.RequestBuilders
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithHeader(string name, params IMatcher[] matchers) public IRequestBuilder WithHeader(string name, params IStringMatcher[] matchers)
{ {
Check.NotNull(name, nameof(name)); Check.NotNull(name, nameof(name));
Check.NotNullOrEmpty(matchers, nameof(matchers)); Check.NotNullOrEmpty(matchers, nameof(matchers));
@@ -401,7 +376,7 @@ namespace WireMock.RequestBuilders
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="matchers">The matchers.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithCookie(string name, params IMatcher[] matchers) public IRequestBuilder WithCookie(string name, params IStringMatcher[] matchers)
{ {
Check.NotNullOrEmpty(matchers, nameof(matchers)); Check.NotNullOrEmpty(matchers, nameof(matchers));

View File

@@ -166,7 +166,8 @@ namespace WireMock.Serialization
return null; return null;
} }
var patterns = matcher.GetPatterns(); IStringMatcher stringMatcher = matcher as IStringMatcher;
string[] patterns = stringMatcher != null ? stringMatcher.GetPatterns() : new string[0];
return new MatcherModel return new MatcherModel
{ {
@@ -192,7 +193,9 @@ namespace WireMock.Serialization
public static IMatcher Map([CanBeNull] MatcherModel matcher) public static IMatcher Map([CanBeNull] MatcherModel matcher)
{ {
if (matcher == null) if (matcher == null)
{
return null; return null;
}
var parts = matcher.Name.Split('.'); var parts = matcher.Name.Split('.');
string matcherName = parts[0]; string matcherName = parts[0];

View File

@@ -606,7 +606,7 @@ namespace WireMock.Server
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP); var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel?.Matchers != null) if (clientIPModel?.Matchers != null)
{ {
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(MappingConverter.Map).ToArray()); requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(MappingConverter.Map).Cast<IStringMatcher>().ToArray());
} }
} }
} }
@@ -623,7 +623,7 @@ namespace WireMock.Server
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path); var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
if (pathModel?.Matchers != null) if (pathModel?.Matchers != null)
{ {
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).ToArray()); requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).Cast<IStringMatcher>().ToArray());
} }
} }
} }
@@ -640,7 +640,7 @@ namespace WireMock.Server
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url); var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
if (urlModel?.Matchers != null) if (urlModel?.Matchers != null)
{ {
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).ToArray()); requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).Cast<IStringMatcher>().ToArray());
} }
} }
} }
@@ -654,7 +654,7 @@ namespace WireMock.Server
{ {
foreach (var headerModel in requestModel.Headers.Where(h => h.Matchers != null)) foreach (var headerModel in requestModel.Headers.Where(h => h.Matchers != null))
{ {
requestBuilder = requestBuilder.WithHeader(headerModel.Name, headerModel.Matchers.Select(MappingConverter.Map).ToArray()); requestBuilder = requestBuilder.WithHeader(headerModel.Name, headerModel.Matchers.Select(MappingConverter.Map).Cast<IStringMatcher>().ToArray());
} }
} }
@@ -662,7 +662,7 @@ namespace WireMock.Server
{ {
foreach (var cookieModel in requestModel.Cookies.Where(c => c.Matchers != null)) foreach (var cookieModel in requestModel.Cookies.Where(c => c.Matchers != null))
{ {
requestBuilder = requestBuilder.WithCookie(cookieModel.Name, cookieModel.Matchers.Select(MappingConverter.Map).ToArray()); requestBuilder = requestBuilder.WithCookie(cookieModel.Name, cookieModel.Matchers.Select(MappingConverter.Map).Cast<IStringMatcher>().ToArray());
} }
} }

View File

@@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Newtonsoft.Json;
using NFluent; using NFluent;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.Util;
using Xunit; using Xunit;
namespace WireMock.Net.Tests namespace WireMock.Net.Tests
@@ -198,5 +200,86 @@ namespace WireMock.Net.Tests
var requestMatchResult = new RequestMatchResult(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
} }
[Fact]
public void Request_WithBodyAsJson_Object_JsonPathMatcher_true()
{
// given
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
// when
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
var bodyData = new BodyData
{
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
Encoding = Encoding.UTF8
};
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact]
public void Request_WithBodyAsJson_Array_JsonPathMatcher_true()
{
// given
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.books[?(@.price < 10)]"));
// when
string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";
var bodyData = new BodyData
{
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
Encoding = Encoding.UTF8
};
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact]
public void Request_WithBodyAsObject_ExactObjectMatcher_true()
{
// Assign
object body = DateTime.MinValue;
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body);
var bodyData = new BodyData
{
BodyAsJson = DateTime.MinValue
};
// Act
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
// Assert
var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact]
public void Request_WithBodyAsBytes_ExactObjectMatcher_true()
{
// Assign
byte[] body = { 123 };
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body);
var bodyData = new BodyData
{
BodyAsBytes = new byte[] { 123 }
};
// Act
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
// Assert
var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
} }
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NFluent; using NFluent;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
@@ -69,7 +70,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task Scenario_and_State_TodoList_Example() public void Scenario_and_State_TodoList_Example()
{ {
// Assign // Assign
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -104,18 +105,18 @@ namespace WireMock.Net.Tests
// Act and Assert // Act and Assert
string url = "http://localhost:" + _server.Ports[0]; string url = "http://localhost:" + _server.Ports[0];
string getResponse1 = await new HttpClient().GetStringAsync(url + "/todo/items"); string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
Check.That(getResponse1).Equals("Buy milk"); Check.That(getResponse1).Equals("Buy milk");
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription")); var postResponse = new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription")).Result;
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created); Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items"); string getResponse2 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription"); Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
} }
[Fact] [Fact]
public async Task Should_process_request_if_equals_state_and_multiple_state_defined() public void Should_process_request_if_equals_state_and_multiple_state_defined()
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -156,19 +157,20 @@ namespace WireMock.Net.Tests
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithBody("Test state msg 2")); .WithBody("Test state msg 2"));
Thread.Sleep(500);
// when / then // when / then
string url = "http://localhost:" + _server.Ports[0]; string url = "http://localhost:" + _server.Ports[0];
var http = new HttpClient(); var responseNoState1 = new HttpClient().GetStringAsync(url + "/state1").Result;
var responseNoState1 = await http.GetStringAsync(url + "/state1");
Check.That(responseNoState1).Equals("No state msg 1"); Check.That(responseNoState1).Equals("No state msg 1");
var responseNoState2 = await http.GetStringAsync(url + "/state2"); var responseNoState2 = new HttpClient().GetStringAsync(url + "/state2").Result;
Check.That(responseNoState2).Equals("No state msg 2"); Check.That(responseNoState2).Equals("No state msg 2");
var responseWithState1 = await http.GetStringAsync(url + "/foo"); var responseWithState1 = new HttpClient().GetStringAsync(url + "/foo").Result;
Check.That(responseWithState1).Equals("Test state msg 1"); Check.That(responseWithState1).Equals("Test state msg 1");
var responseWithState2 = await http.GetStringAsync(url + "/foo"); var responseWithState2 = new HttpClient().GetStringAsync(url + "/foo").Result;
Check.That(responseWithState2).Equals("Test state msg 2"); Check.That(responseWithState2).Equals("Test state msg 2");
} }