mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-06-12 16:44:26 +02:00
.
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System.Linq;
|
|
||||||
using AnyOfTypes;
|
using AnyOfTypes;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using AnyOfTypes;
|
||||||
|
using Json.Path;
|
||||||
|
using Stef.Validation;
|
||||||
|
using WireMock.Extensions;
|
||||||
|
using WireMock.Models;
|
||||||
|
using WireMock.Util;
|
||||||
|
|
||||||
|
namespace WireMock.Matchers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SystemTextJsonPathMatcher - behaves the same as <see cref="JsonPathMatcher"/> but uses System.Text.Json instead of Newtonsoft.Json.
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="IStringMatcher" />
|
||||||
|
/// <seealso cref="IObjectMatcher" />
|
||||||
|
public class SystemTextJsonPathMatcher : IStringMatcher, IObjectMatcher
|
||||||
|
{
|
||||||
|
private readonly AnyOf<string, StringPattern>[] _patterns;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public MatchBehaviour MatchBehaviour { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public object Value { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SystemTextJsonPathMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="patterns">The patterns.</param>
|
||||||
|
public SystemTextJsonPathMatcher(params string[] patterns)
|
||||||
|
: this(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, patterns.ToAnyOfPatterns())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SystemTextJsonPathMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="patterns">The patterns.</param>
|
||||||
|
public SystemTextJsonPathMatcher(params AnyOf<string, StringPattern>[] patterns)
|
||||||
|
: this(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, patterns)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SystemTextJsonPathMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
|
||||||
|
/// <param name="patterns">The patterns.</param>
|
||||||
|
public SystemTextJsonPathMatcher(
|
||||||
|
MatchBehaviour matchBehaviour,
|
||||||
|
MatchOperator matchOperator = MatchOperator.Or,
|
||||||
|
params AnyOf<string, StringPattern>[] patterns)
|
||||||
|
{
|
||||||
|
_patterns = Guard.NotNull(patterns);
|
||||||
|
MatchBehaviour = matchBehaviour;
|
||||||
|
MatchOperator = matchOperator;
|
||||||
|
Value = patterns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public MatchResult IsMatch(string? input)
|
||||||
|
{
|
||||||
|
var score = MatchScores.Mismatch;
|
||||||
|
Exception? exception = null;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(input))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var node = JsonNode.Parse(input!);
|
||||||
|
score = IsMatchInternal(node);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
exception = ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public MatchResult IsMatch(object? input)
|
||||||
|
{
|
||||||
|
var score = MatchScores.Mismatch;
|
||||||
|
Exception? exception = null;
|
||||||
|
|
||||||
|
// When input is null or byte[], return Mismatch.
|
||||||
|
if (input != null && input is not byte[])
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonNode? node = input switch
|
||||||
|
{
|
||||||
|
JsonNode jsonNode => jsonNode,
|
||||||
|
string str => JsonNode.Parse(str),
|
||||||
|
_ => JsonNode.Parse(System.Text.Json.JsonSerializer.Serialize(input))
|
||||||
|
};
|
||||||
|
|
||||||
|
score = IsMatchInternal(node);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
exception = ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public AnyOf<string, StringPattern>[] GetPatterns()
|
||||||
|
{
|
||||||
|
return _patterns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public MatchOperator MatchOperator { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Name => nameof(SystemTextJsonPathMatcher);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string GetCSharpCodeArguments()
|
||||||
|
{
|
||||||
|
return $"new {Name}" +
|
||||||
|
$"(" +
|
||||||
|
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
|
||||||
|
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
|
||||||
|
$"{MappingConverterUtils.ToCSharpCodeArguments(_patterns)}" +
|
||||||
|
$")";
|
||||||
|
}
|
||||||
|
|
||||||
|
private double IsMatchInternal(JsonNode? node)
|
||||||
|
{
|
||||||
|
// JsonPath.Net requires the node to be inside an object or array for filter expressions.
|
||||||
|
// Similar to JsonPathMatcher's ConvertJTokenToJArrayIfNeeded, wrap a plain object in an array
|
||||||
|
// when it's an object with a single non-array child property.
|
||||||
|
var evaluationNode = WrapIfNeeded(node);
|
||||||
|
|
||||||
|
var values = _patterns
|
||||||
|
.Select(pattern =>
|
||||||
|
{
|
||||||
|
var path = JsonPath.Parse(pattern.GetPattern());
|
||||||
|
var result = path.Evaluate(evaluationNode);
|
||||||
|
return result.Matches is { Count: > 0 };
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
return MatchScores.ToScore(values, MatchOperator);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mirrors JsonPathMatcher.ConvertJTokenToJArrayIfNeeded:
|
||||||
|
// If the node is an object with exactly one property whose value is not already an array,
|
||||||
|
// wrap that value in an array so that filter expressions (e.g. [?(@.x == y)]) can match.
|
||||||
|
private static JsonNode? WrapIfNeeded(JsonNode? node)
|
||||||
|
{
|
||||||
|
if (node is not JsonObject obj)
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
var properties = obj.ToList();
|
||||||
|
if (properties.Count != 1)
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
var single = properties[0];
|
||||||
|
if (single.Value is JsonArray)
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
var clonedValue = JsonNode.Parse(single.Value?.ToJsonString() ?? "null");
|
||||||
|
return new JsonObject
|
||||||
|
{
|
||||||
|
[single.Key] = new JsonArray(clonedValue)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,9 +106,24 @@ internal class MatcherMapper
|
|||||||
var valueForJsonPartialWildcardMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
var valueForJsonPartialWildcardMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, useRegex);
|
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, useRegex);
|
||||||
|
|
||||||
|
case nameof(SystemTextJsonMatcher):
|
||||||
|
var valueForSystemTextJsonMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
|
return new SystemTextJsonMatcher(matchBehaviour, valueForSystemTextJsonMatcher!, ignoreCase, useRegex);
|
||||||
|
|
||||||
|
case nameof(SystemTextJsonPartialMatcher):
|
||||||
|
var valueForSystemTextJsonPartialMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
|
return new SystemTextJsonPartialMatcher(matchBehaviour, valueForSystemTextJsonPartialMatcher!, ignoreCase, useRegex);
|
||||||
|
|
||||||
|
case nameof(SystemTextJsonPartialWildcardMatcher):
|
||||||
|
var valueForSystemTextJsonPartialWildcardMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
|
return new SystemTextJsonPartialWildcardMatcher(matchBehaviour, valueForSystemTextJsonPartialWildcardMatcher!, ignoreCase, useRegex);
|
||||||
|
|
||||||
case nameof(JsonPathMatcher):
|
case nameof(JsonPathMatcher):
|
||||||
return new JsonPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
return new JsonPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||||
|
|
||||||
|
case nameof(SystemTextJsonPathMatcher):
|
||||||
|
return new SystemTextJsonPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||||
|
|
||||||
case nameof(JmesPathMatcher):
|
case nameof(JmesPathMatcher):
|
||||||
return new JmesPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
return new JmesPathMatcher(matchBehaviour, matchOperator, stringPatterns);
|
||||||
|
|
||||||
@@ -171,6 +186,10 @@ internal class MatcherMapper
|
|||||||
model.Regex = jsonMatcher.Regex;
|
model.Regex = jsonMatcher.Regex;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SystemTextJsonMatcher stjMatcher:
|
||||||
|
model.Regex = stjMatcher.Regex;
|
||||||
|
break;
|
||||||
|
|
||||||
case XPathMatcher xpathMatcher:
|
case XPathMatcher xpathMatcher:
|
||||||
model.XmlNamespaceMap = xpathMatcher.XmlNamespaceMap;
|
model.XmlNamespaceMap = xpathMatcher.XmlNamespaceMap;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -38,6 +38,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JmesPath.Net" Version="1.1.0" />
|
<PackageReference Include="JmesPath.Net" Version="1.1.0" />
|
||||||
|
<PackageReference Include="JsonPath.Net" Version="3.0.2" />
|
||||||
<PackageReference Include="NJsonSchema.Extensions" Version="0.2.0" />
|
<PackageReference Include="NJsonSchema.Extensions" Version="0.2.0" />
|
||||||
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
||||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||||
|
|||||||
@@ -0,0 +1,400 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.Matchers;
|
||||||
|
|
||||||
|
public class SystemTextJsonPathMatcherTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_GetName()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("X");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
string name = matcher.Name;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
name.Should().Be("SystemTextJsonPathMatcher");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_GetPatterns()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("X");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var patterns = matcher.GetPatterns();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
patterns.Should().ContainSingle("X");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_ByteArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var bytes = new byte[0];
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(bytes).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_NullString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(null).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_EmptyString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(string.Empty).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_NullObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
object? o = null;
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(o).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_String_Exception_Mismatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.Id");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch("not-json").Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_AnonymousObject()
|
||||||
|
{
|
||||||
|
// Arrange - RFC 9535: filter expression requires an array context
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$[?(@.Id == 1)]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(new[] { new { Id = 1, Name = "Test" } }).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_AnonymousObject_WithNestedObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(new { things = new { name = "x" } }).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_String_WithNestedObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = "{ \"things\": { \"name\": \"x\" } }";
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(json).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsNoMatch_String_WithNestedObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = "{ \"things\": { \"name\": \"y\" } }";
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.things[?(@.name == 'x')]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(json).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_JsonNode()
|
||||||
|
{
|
||||||
|
// Arrange - RFC 9535: filter expression requires an array context
|
||||||
|
string[] patterns = { "$[?(@.Id == 1)]" };
|
||||||
|
var matcher = new SystemTextJsonPathMatcher(patterns);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var node = JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]");
|
||||||
|
double match = matcher.IsMatch(node).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_JsonNode_Parsed()
|
||||||
|
{
|
||||||
|
// Arrange - RFC 9535: filter expression requires an array context
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$[?(@.Id == 1)]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_RejectOnMatch()
|
||||||
|
{
|
||||||
|
// Arrange - RFC 9535: filter expression requires an array context
|
||||||
|
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "$[?(@.Id == 1)]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse("[{\"Id\":1,\"Name\":\"Test\"}]")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_ArrayOneLevel()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.arr[0].line1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [{
|
||||||
|
""line1"": ""line1""
|
||||||
|
}]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_ObjectMatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.test");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [
|
||||||
|
{
|
||||||
|
""line1"": ""line1""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.test3");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [
|
||||||
|
{
|
||||||
|
""line1"": ""line1""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatchInArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$arr[0].line1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": []
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_DoesntMatchNoObjectsInArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$arr[2].line1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": []
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_NestedArrays()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.arr[0].sub[0].subline1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [{
|
||||||
|
""line1"": ""line1"",
|
||||||
|
""sub"":[
|
||||||
|
{
|
||||||
|
""subline1"":""subline1""
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorAnd()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.And, "$.arr[0].sub[0].subline1", "$.arr[0].line2");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [{
|
||||||
|
""line1"": ""line1"",
|
||||||
|
""sub"":[
|
||||||
|
{
|
||||||
|
""subline1"":""subline1""
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorOr()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "$.arr[0].sub[0].subline2", "$.arr[0].line1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(JsonNode.Parse(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""test"": ""test"",
|
||||||
|
""test2"": ""test2"",
|
||||||
|
""arr"": [{
|
||||||
|
""line1"": ""line1"",
|
||||||
|
""sub"":[
|
||||||
|
{
|
||||||
|
""subline1"":""subline1""
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}")).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_String_ArrayOneLevel()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.arr[0].line1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(@"{
|
||||||
|
""name"": ""PathSelectorTest"",
|
||||||
|
""arr"": [{
|
||||||
|
""line1"": ""line1""
|
||||||
|
}]
|
||||||
|
}").Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SystemTextJsonPathMatcher_IsMatch_String_DoesntMatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var matcher = new SystemTextJsonPathMatcher("$.test3");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double match = matcher.IsMatch(@"{ ""test"": ""test"" }").Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
match.Should().Be(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user