-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* JmesPathMatcher * netstandard1.3 * update System.Linq.Dynamic.Core * simplyfy `double IsMatch(object input)`
- Loading branch information
Showing
10 changed files
with
261 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using DevLab.JmesPath; | ||
using JetBrains.Annotations; | ||
using Newtonsoft.Json; | ||
using System.Linq; | ||
using WireMock.Validation; | ||
|
||
namespace WireMock.Matchers | ||
{ | ||
/// <summary> | ||
/// http://jmespath.org/ | ||
/// </summary> | ||
public class JmesPathMatcher : IStringMatcher, IObjectMatcher | ||
{ | ||
private readonly string[] _patterns; | ||
|
||
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> | ||
public MatchBehaviour MatchBehaviour { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class. | ||
/// </summary> | ||
/// <param name="patterns">The patterns.</param> | ||
public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="patterns">The patterns.</param> | ||
public JmesPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) | ||
{ | ||
Check.NotNull(patterns, nameof(patterns)); | ||
|
||
MatchBehaviour = matchBehaviour; | ||
_patterns = patterns; | ||
} | ||
|
||
/// <inheritdoc cref="IStringMatcher.IsMatch"/> | ||
public double IsMatch(string input) | ||
{ | ||
double match = MatchScores.Mismatch; | ||
if (input != null) | ||
{ | ||
try | ||
{ | ||
match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern)))); | ||
} | ||
catch (JsonException) | ||
{ | ||
// just ignore JsonException | ||
} | ||
} | ||
|
||
return MatchBehaviourHelper.Convert(MatchBehaviour, match); | ||
} | ||
|
||
/// <inheritdoc cref="IObjectMatcher.IsMatch"/> | ||
public double IsMatch(object input) | ||
{ | ||
double match = MatchScores.Mismatch; | ||
|
||
// When input is null or byte[], return Mismatch. | ||
if (input != null && !(input is byte[])) | ||
{ | ||
string inputAsString = JsonConvert.SerializeObject(input); | ||
return IsMatch(inputAsString); | ||
} | ||
|
||
return MatchBehaviourHelper.Convert(MatchBehaviour, match); | ||
} | ||
|
||
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> | ||
public string[] GetPatterns() | ||
{ | ||
return _patterns; | ||
} | ||
|
||
/// <inheritdoc cref="IMatcher.Name"/> | ||
public string Name => "JmesPathMatcher"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using Newtonsoft.Json.Linq; | ||
using NFluent; | ||
using WireMock.Matchers; | ||
using Xunit; | ||
|
||
namespace WireMock.Net.Tests.Matchers | ||
{ | ||
public class JmesPathMatcherTests | ||
{ | ||
[Fact] | ||
public void JmesPathMatcher_GetName() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher("X"); | ||
|
||
// Act | ||
string name = matcher.Name; | ||
|
||
// Assert | ||
Check.That(name).Equals("JmesPathMatcher"); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_GetPatterns() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher("X"); | ||
|
||
// Act | ||
string[] patterns = matcher.GetPatterns(); | ||
|
||
// Assert | ||
Check.That(patterns).ContainsExactly("X"); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_ByteArray() | ||
{ | ||
// Assign | ||
var bytes = new byte[0]; | ||
var matcher = new JmesPathMatcher(""); | ||
|
||
// Act | ||
double match = matcher.IsMatch(bytes); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_NullString() | ||
{ | ||
// Assign | ||
string s = null; | ||
var matcher = new JmesPathMatcher(""); | ||
|
||
// Act | ||
double match = matcher.IsMatch(s); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_NullObject() | ||
{ | ||
// Assign | ||
object o = null; | ||
var matcher = new JmesPathMatcher(""); | ||
|
||
// Act | ||
double match = matcher.IsMatch(o); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_String_Exception_Mismatch() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher("xxx"); | ||
|
||
// Act | ||
double match = matcher.IsMatch(""); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_Object_Exception_Mismatch() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher(""); | ||
|
||
// Act | ||
double match = matcher.IsMatch("x"); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_AnonymousObject() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher("things.name == 'RequiredThing'"); | ||
|
||
// Act | ||
double match = matcher.IsMatch(new { things = new { name = "RequiredThing" } }); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(1); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_JObject() | ||
{ | ||
// Assign | ||
string[] patterns = { "things.x == 'RequiredThing'" }; | ||
var matcher = new JmesPathMatcher(patterns); | ||
|
||
// Act | ||
var sub = new JObject | ||
{ | ||
{ "x", new JValue("RequiredThing") } | ||
}; | ||
var jobject = new JObject | ||
{ | ||
{ "Id", new JValue(1) }, | ||
{ "things", sub } | ||
}; | ||
double match = matcher.IsMatch(jobject); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(1); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_JObject_Parsed() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher("things.x == 'RequiredThing'"); | ||
|
||
// Act | ||
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(1); | ||
} | ||
|
||
[Fact] | ||
public void JmesPathMatcher_IsMatch_RejectOnMatch() | ||
{ | ||
// Assign | ||
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, "things.x == 'RequiredThing'"); | ||
|
||
// Act | ||
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")); | ||
|
||
// Assert | ||
Check.That(match).IsEqualTo(0.0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters