mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 09:30:59 +01:00
Update JSONPathMatcher.cs to cover the string path selection to a child (#993)
* Update JSONPathMatcher.cs to cover the string path selection to a child The .SelectToken method accept string path selection and JSONPath queries. The current code works only for the queries because the result is JObject. When the string path is selected the result is JValue and event with a valid result the code the code doesn't return valued result. https://www.newtonsoft.com/json/help/html/SelectToken.htm * Added unit tests * Addressed the comments * Addressed the comments * Update JSONPathMatcher.cs
This commit is contained in:
@@ -200,4 +200,128 @@ public class JsonPathMatcherTests
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_ArrayOneLevel()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$.arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1"",
|
||||
}]
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_ObjectMatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$.test");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [
|
||||
{
|
||||
""line1"": ""line1"",
|
||||
}
|
||||
]
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_DoesntMatch()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$.test3");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [
|
||||
{
|
||||
""line1"": ""line1"",
|
||||
}
|
||||
]
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_DoesntMatchInArray()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$arr[0].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": []
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_DoesntMatchNoObjectsInArray()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$arr[2].line1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": []
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_NestedArrays()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new JsonPathMatcher("$.arr[0].sub[0].subline1");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse(@"{
|
||||
""name"": ""PathSelectorTest"",
|
||||
""test"": ""test"",
|
||||
""test2"": ""test2"",
|
||||
""arr"": [{
|
||||
""line1"": ""line1"",
|
||||
""sub"":[
|
||||
{
|
||||
""subline1"":""subline1""
|
||||
}]
|
||||
}]
|
||||
}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user