mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-06-09 07:12:43 +02:00
. (#392)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Collections;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
@@ -85,7 +86,7 @@ namespace WireMock.Matchers
|
|||||||
// Check if JToken or object
|
// Check if JToken or object
|
||||||
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
|
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
|
||||||
|
|
||||||
// Check if JToken or string or object
|
// Check if JToken, string, IEnumerable or object
|
||||||
JToken jtokenValue;
|
JToken jtokenValue;
|
||||||
switch (Value)
|
switch (Value)
|
||||||
{
|
{
|
||||||
@@ -97,6 +98,10 @@ namespace WireMock.Matchers
|
|||||||
jtokenValue = JsonUtils.Parse(stringValue);
|
jtokenValue = JsonUtils.Parse(stringValue);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case IEnumerable enumerableValue:
|
||||||
|
jtokenValue = JArray.FromObject(enumerableValue);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
jtokenValue = JObject.FromObject(Value);
|
jtokenValue = JObject.FromObject(Value);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
@@ -14,7 +15,7 @@ namespace WireMock.Transformers
|
|||||||
{
|
{
|
||||||
handlebarsContext.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
|
handlebarsContext.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
|
||||||
{
|
{
|
||||||
(JObject valueToProcess, string jsonPath) = ParseArguments(arguments);
|
(JToken valueToProcess, string jsonPath) = ParseArguments(arguments);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -29,7 +30,7 @@ namespace WireMock.Transformers
|
|||||||
|
|
||||||
handlebarsContext.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
|
handlebarsContext.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
|
||||||
{
|
{
|
||||||
(JObject valueToProcess, string jsonPath) = ParseArguments(arguments);
|
(JToken valueToProcess, string jsonPath) = ParseArguments(arguments);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -46,22 +47,26 @@ namespace WireMock.Transformers
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] arguments)
|
private static (JToken valueToProcess, string jsonpath) ParseArguments(object[] arguments)
|
||||||
{
|
{
|
||||||
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
|
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
|
||||||
Check.NotNull(arguments[0], "arguments[0]");
|
Check.NotNull(arguments[0], "arguments[0]");
|
||||||
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
|
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
|
||||||
|
|
||||||
JObject valueToProcess;
|
JToken valueToProcess;
|
||||||
|
|
||||||
switch (arguments[0])
|
switch (arguments[0])
|
||||||
{
|
{
|
||||||
case string jsonAsString:
|
case JToken tokenValue:
|
||||||
valueToProcess = JsonUtils.Parse(jsonAsString);
|
valueToProcess = tokenValue;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JObject jsonAsJObject:
|
case string stringValue:
|
||||||
valueToProcess = jsonAsJObject;
|
valueToProcess = JsonUtils.Parse(stringValue);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IEnumerable enumerableValue:
|
||||||
|
valueToProcess = JArray.FromObject(enumerableValue);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ namespace WireMock.Util
|
|||||||
/// Using : DateParseHandling = DateParseHandling.None
|
/// Using : DateParseHandling = DateParseHandling.None
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="json">A System.String that contains JSON.</param>
|
/// <param name="json">A System.String that contains JSON.</param>
|
||||||
/// <returns>A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.</returns>
|
/// <returns>A Newtonsoft.Json.Linq.JToken populated from the string that contains JSON.</returns>
|
||||||
public static JObject Parse(string json)
|
public static JToken Parse(string json)
|
||||||
{
|
{
|
||||||
return JsonConvert.DeserializeObject<JObject>(json, JsonSerializerSettings);
|
return JsonConvert.DeserializeObject<JToken>(json, JsonSerializerSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T ParseJTokenToObject<T>(object value)
|
public static T ParseJTokenToObject<T>(object value)
|
||||||
|
|||||||
@@ -75,6 +75,24 @@ namespace WireMock.Net.Tests.Matchers
|
|||||||
Check.That(match).IsEqualTo(0);
|
Check.That(match).IsEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_JArray()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new[] { "x", "y" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jArray = new JArray
|
||||||
|
{
|
||||||
|
"x",
|
||||||
|
"y"
|
||||||
|
};
|
||||||
|
double match = matcher.IsMatch(jArray);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, match);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonMatcher_IsMatch_JObject()
|
public void JsonMatcher_IsMatch_JObject()
|
||||||
{
|
{
|
||||||
@@ -139,6 +157,24 @@ namespace WireMock.Net.Tests.Matchers
|
|||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_JArrayAsString()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher("[ \"x\", \"y\" ]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jArray = new JArray
|
||||||
|
{
|
||||||
|
"x",
|
||||||
|
"y"
|
||||||
|
};
|
||||||
|
double match = matcher.IsMatch(jArray);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, match);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonMatcher_IsMatch_JObjectAsString()
|
public void JsonMatcher_IsMatch_JObjectAsString()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user