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