mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 16:58:27 +02:00
Support for string and object in JsonMatcher. (#157)
* JsonMatcher (#154) * small update in example code
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
public interface IValueMatcher: IObjectMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value.
|
||||
/// Gets the value (can be a string or an obejct).
|
||||
/// </summary>
|
||||
/// <returns>Value</returns>
|
||||
string GetValue();
|
||||
object Value { get; }
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,8 @@ namespace WireMock.Matchers
|
||||
/// </summary>
|
||||
public class JsonMatcher : IValueMatcher
|
||||
{
|
||||
private readonly string _value;
|
||||
/// <inheritdoc cref="IValueMatcher.Value"/>
|
||||
public object Value { get; }
|
||||
|
||||
/// <inheritdoc cref="IMatcher.Name"/>
|
||||
public string Name => "JsonMatcher";
|
||||
@@ -21,22 +22,43 @@ namespace WireMock.Matchers
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check for equality.</param>
|
||||
/// <param name="value">The string value to check for equality.</param>
|
||||
public JsonMatcher([NotNull] string value) : this(MatchBehaviour.AcceptOnMatch, value)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The object value to check for equality.</param>
|
||||
public JsonMatcher([NotNull] object value) : this(MatchBehaviour.AcceptOnMatch, value)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||
/// <param name="value">The value to check for equality.</param>
|
||||
/// <param name="value">The string value to check for equality.</param>
|
||||
public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] string value)
|
||||
{
|
||||
Check.NotNull(value, nameof(value));
|
||||
|
||||
MatchBehaviour = matchBehaviour;
|
||||
_value = value;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||
/// <param name="value">The object value to check for equality.</param>
|
||||
public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value)
|
||||
{
|
||||
Check.NotNull(value, nameof(value));
|
||||
|
||||
MatchBehaviour = matchBehaviour;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
|
||||
@@ -48,9 +70,15 @@ namespace WireMock.Matchers
|
||||
try
|
||||
{
|
||||
// Check if JToken or object
|
||||
JToken jtoken = input is JToken token ? token : JObject.FromObject(input);
|
||||
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
|
||||
|
||||
match = JToken.DeepEquals(JToken.Parse(_value), jtoken);
|
||||
// Check if JToken or string or object
|
||||
JToken jtokenValue =
|
||||
Value is JToken tokenValue ? tokenValue :
|
||||
Value is string stringValue ? JToken.Parse(stringValue) :
|
||||
JObject.FromObject(input);
|
||||
|
||||
match = JToken.DeepEquals(jtokenValue, jtokenInput);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
@@ -60,8 +88,5 @@ namespace WireMock.Matchers
|
||||
|
||||
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IValueMatcher.GetValue"/>
|
||||
public string GetValue() => _value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user