diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f1c448c..c37d43c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 1.0.3.12 (24 March 2018) + + - [#100](https://github.com/WireMock-Net/WireMock.Net/issues/100) - Issue: JsonPathMatcher - not working for rootless jsons? + +Commits: ... + + # 1.0.3.11 (20 March 2018) - [#110](https://github.com/WireMock-Net/WireMock.Net/issues/110) - Fix: remove `Func[]` from MappingModel diff --git a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj index 18266ed9..05054dc5 100644 --- a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj +++ b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj @@ -3,7 +3,7 @@ Lightweight StandAlone Http Mocking Server for .Net. WireMock.Net.StandAlone - 1.0.3.11 + 1.0.3.12 Stef Heyenrath net452;net46;netstandard1.3;netstandard2.0 true diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs index aab0b86c..64f9acf7 100644 --- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs @@ -7,12 +7,11 @@ using WireMock.Validation; namespace WireMock.Matchers { /// - /// JSONPathMatcher + /// JsonPathMatcher /// /// public class JsonPathMatcher : IStringMatcher, IObjectMatcher { - // private readonly object _jsonPattern; private readonly string[] _patterns; /// @@ -26,13 +25,6 @@ namespace WireMock.Matchers _patterns = patterns; } - //public JsonPathMatcher([NotNull] object jsonPattern) - //{ - // Check.NotNull(jsonPattern, nameof(jsonPattern)); - - // _jsonPattern = jsonPattern; - //} - /// public double IsMatch(string input) { @@ -43,9 +35,8 @@ namespace WireMock.Matchers try { - JObject o = JObject.Parse(input); - - return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null)); + var jtoken = JToken.Parse(input); + return IsMatch(jtoken); } catch (Exception) { @@ -63,9 +54,9 @@ namespace WireMock.Matchers try { - var o = input as JObject ?? JObject.FromObject(input); - - return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null)); + // Check if JToken or object + JToken jtoken = input is JToken token ? token : JObject.FromObject(input); + return IsMatch(jtoken); } catch (Exception) { @@ -84,5 +75,13 @@ namespace WireMock.Matchers { return "JsonPathMatcher"; } + + private double IsMatch(JToken jtoken) + { + // Wrap in array if needed + JToken jarray = jtoken is JArray ? jtoken : new JArray(jtoken); + + return MatchScores.ToScore(_patterns.Select(pattern => jarray.SelectToken(pattern) != null)); + } } } \ 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 f92a1bd3..6834001b 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -1,4 +1,5 @@ -using NFluent; +using Newtonsoft.Json.Linq; +using NFluent; using WireMock.Matchers; using Xunit; @@ -31,5 +32,104 @@ namespace WireMock.Net.Tests.Matchers // Assert Check.That(patterns).ContainsExactly("X"); } + + [Fact] + public void JsonPathMatcher_IsMatch_NullString() + { + // Assign + string s = null; + var matcher = new JsonPathMatcher(""); + + // Act + double match = matcher.IsMatch(s); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonPathMatcher_IsMatch_NullObject() + { + // Assign + object o = null; + var matcher = new JsonPathMatcher(""); + + // Act + double match = matcher.IsMatch(o); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonPathMatcher_IsMatch_String_Exception_Mismatch() + { + // Assign + var matcher = new JsonPathMatcher("xxx"); + + // Act + double match = matcher.IsMatch(""); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch() + { + // Assign + var matcher = new JsonPathMatcher("xxx"); + + // Act + double match = matcher.IsMatch(""); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonPathMatcher_IsMatch_AnonymousObject() + { + // Assign + var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]"); + + // Act + double match = matcher.IsMatch(new { Id = 1, Name = "Test" }); + + // Assert + Check.That(match).IsEqualTo(1); + } + + [Fact] + public void JsonPathMatcher_IsMatch_JObject() + { + // Assign + string[] patterns = { "$..[?(@.Id == 1)]" }; + var matcher = new JsonPathMatcher(patterns); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Check.That(match).IsEqualTo(1); + } + + [Fact] + public void JsonPathMatcher_IsMatch_JObject_Parsed() + { + // Assign + var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]"); + + // Act + double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}")); + + // Assert + Check.That(match).IsEqualTo(1); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs index 90453a08..6e3736c5 100644 --- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs +++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs @@ -147,7 +147,7 @@ namespace WireMock.Net.Tests public void Request_WithBodyJsonPathMatcher_true() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); // when string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; @@ -179,7 +179,7 @@ namespace WireMock.Net.Tests public void Request_WithBodyAsJson_Object_JsonPathMatcher_true() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); // when string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; @@ -199,7 +199,7 @@ namespace WireMock.Net.Tests public void Request_WithBodyAsJson_Array_JsonPathMatcher_1() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.books[?(@.price < 10)]")); + var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]")); // when string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";