| | | 1 | | using System.Linq; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// JsonPathMatcher |
| | | 11 | | /// </summary> |
| | | 12 | | /// <seealso cref="IMatcher" /> |
| | | 13 | | /// <seealso cref="IObjectMatcher" /> |
| | | 14 | | public class JsonPathMatcher : IStringMatcher, IObjectMatcher |
| | | 15 | | { |
| | | 16 | | private readonly string[] _patterns; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc cref="IMatcher.MatchBehaviour"/> |
| | 14 | 19 | | public MatchBehaviour MatchBehaviour { get; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="patterns">The patterns.</param> |
| | 15 | 25 | | public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) |
| | 15 | 26 | | { |
| | 15 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 33 | | /// <param name="patterns">The patterns.</param> |
| | 16 | 34 | | public JsonPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) |
| | 16 | 35 | | { |
| | 16 | 36 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 37 | | |
| | 16 | 38 | | MatchBehaviour = matchBehaviour; |
| | 16 | 39 | | _patterns = patterns; |
| | 16 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 43 | | public double IsMatch(string input) |
| | 5 | 44 | | { |
| | 5 | 45 | | double match = MatchScores.Mismatch; |
| | 5 | 46 | | if (input != null) |
| | 4 | 47 | | { |
| | | 48 | | try |
| | 4 | 49 | | { |
| | 4 | 50 | | var jtoken = JToken.Parse(input); |
| | 2 | 51 | | match = IsMatch(jtoken); |
| | 2 | 52 | | } |
| | 2 | 53 | | catch (JsonException) |
| | 2 | 54 | | { |
| | | 55 | | // just ignore JsonException |
| | 2 | 56 | | } |
| | 4 | 57 | | } |
| | | 58 | | |
| | 5 | 59 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 5 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc cref="IObjectMatcher.IsMatch"/> |
| | | 63 | | public double IsMatch(object input) |
| | 9 | 64 | | { |
| | 9 | 65 | | double match = MatchScores.Mismatch; |
| | | 66 | | |
| | | 67 | | // When input is null or byte[], return Mismatch. |
| | 9 | 68 | | if (input != null && !(input is byte[])) |
| | 7 | 69 | | { |
| | | 70 | | try |
| | 7 | 71 | | { |
| | | 72 | | // Check if JToken or object |
| | 7 | 73 | | JToken jtoken = input is JToken token ? token : JObject.FromObject(input); |
| | 7 | 74 | | match = IsMatch(jtoken); |
| | 7 | 75 | | } |
| | 0 | 76 | | catch (JsonException) |
| | 0 | 77 | | { |
| | | 78 | | // just ignore JsonException |
| | 0 | 79 | | } |
| | 7 | 80 | | } |
| | | 81 | | |
| | 9 | 82 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 9 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 86 | | public string[] GetPatterns() |
| | 1 | 87 | | { |
| | 1 | 88 | | return _patterns; |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc cref="IMatcher.Name"/> |
| | 1 | 92 | | public string Name => "JsonPathMatcher"; |
| | | 93 | | |
| | | 94 | | private double IsMatch(JToken jtoken) |
| | 9 | 95 | | { |
| | | 96 | | // Wrap in array if needed |
| | 9 | 97 | | JToken tokenOrArray = jtoken is JArray ? jtoken : new JArray(jtoken); |
| | | 98 | | |
| | 27 | 99 | | return MatchScores.ToScore(_patterns.Select(pattern => tokenOrArray.SelectToken(pattern) != null)); |
| | 9 | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |