diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs index 4cc3ec9e..1971616b 100644 --- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs @@ -118,8 +118,9 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher // The SelectToken method can accept a string path to a child token ( i.e. "Manufacturers[0].Products[0].Price"). // In that case it will return a JValue (some type) which does not implement the IEnumerable interface. - return MatchScores.ToScore( - _patterns.Select(pattern => array.SelectToken(pattern.GetPattern()) != null).ToArray(), MatchOperator); + var values = _patterns.Select(pattern => array.SelectToken(pattern.GetPattern()) != null).ToArray(); + + return MatchScores.ToScore(values, MatchOperator); } // https://github.com/WireMock-Net/WireMock.Net/issues/965 diff --git a/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs index fc8890cb..547ee89f 100644 --- a/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs @@ -1,4 +1,5 @@ using System; +using FluentAssertions; using Newtonsoft.Json.Linq; using NFluent; using WireMock.Matchers; @@ -163,4 +164,30 @@ public class JmesPathMatcherTests // Assert Check.That(match).IsEqualTo(0.0); } + + [Fact] + public void JmesPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorAnd() + { + // Assign + var matcher = new JmesPathMatcher(MatchOperator.And, "things.x == 'RequiredThing'", "things.x == 'abc'"); + + // Act + double score = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")).Score; + + // Assert + score.Should().Be(0); + } + + [Fact] + public void JmesPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorOr() + { + // Assign + var matcher = new JmesPathMatcher(MatchOperator.Or, "things.x == 'RequiredThing'", "things.x == 'abc'"); + + // Act + double score = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")).Score; + + // Assert + score.Should().Be(1); + } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index 6f71ed5b..30454252 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -1,4 +1,5 @@ using System; +using FluentAssertions; using Newtonsoft.Json.Linq; using NFluent; using WireMock.Matchers; @@ -324,4 +325,52 @@ public class JsonPathMatcherTests // Assert Check.That(match).IsEqualTo(1.0); } + + [Fact] + public void JsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorAnd() + { + // Assign + var matcher = new JsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.And, "$.arr[0].sub[0].subline1", "$.arr[0].line2"); + + // Act + double match = matcher.IsMatch(JObject.Parse(@"{ + ""name"": ""PathSelectorTest"", + ""test"": ""test"", + ""test2"": ""test2"", + ""arr"": [{ + ""line1"": ""line1"", + ""sub"":[ + { + ""subline1"":""subline1"" + }] + }] + }")).Score; + + // Assert + match.Should().Be(0); + } + + [Fact] + public void JsonPathMatcher_IsMatch_MultiplePatternsUsingMatchOperatorOr() + { + // Assign + var matcher = new JsonPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "$.arr[0].sub[0].subline2", "$.arr[0].line1"); + + // Act + double match = matcher.IsMatch(JObject.Parse(@"{ + ""name"": ""PathSelectorTest"", + ""test"": ""test"", + ""test2"": ""test2"", + ""arr"": [{ + ""line1"": ""line1"", + ""sub"":[ + { + ""subline1"":""subline1"" + }] + }] + }")).Score; + + // Assert + match.Should().Be(1); + } } \ No newline at end of file