Summary

Class:WireMock.Matchers.JsonMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\JsonMatcher.cs
Covered lines:37
Uncovered lines:5
Coverable lines:42
Total lines:105
Line coverage:88%
Branch coverage:83.3%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
IsMatch(...)000.7620.833
.ctor(...)0010
.ctor(...)0010
.ctor(...)0010
.ctor(...)0010

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\JsonMatcher.cs

#LineLine coverage
 1using JetBrains.Annotations;
 2using Newtonsoft.Json;
 3using Newtonsoft.Json.Linq;
 4using WireMock.Validation;
 5
 6namespace WireMock.Matchers
 7{
 8    /// <summary>
 9    /// JsonMatcher
 10    /// </summary>
 11    public class JsonMatcher : IValueMatcher
 12    {
 13        /// <inheritdoc cref="IValueMatcher.Value"/>
 714        public object Value { get; }
 15
 16        /// <inheritdoc cref="IMatcher.Name"/>
 117        public string Name => "JsonMatcher";
 18
 19        /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
 720        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>
 626        public JsonMatcher([NotNull] string value) : this(MatchBehaviour.AcceptOnMatch, value)
 627        {
 628        }
 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>
 234        public JsonMatcher([NotNull] object value) : this(MatchBehaviour.AcceptOnMatch, value)
 235        {
 236        }
 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>
 743        public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] string value)
 744        {
 745            Check.NotNull(value, nameof(value));
 46
 747            MatchBehaviour = matchBehaviour;
 748            Value = value;
 749        }
 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>
 256        public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value)
 257        {
 258            Check.NotNull(value, nameof(value));
 59
 260            MatchBehaviour = matchBehaviour;
 261            Value = value;
 262        }
 63
 64        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
 65        public double IsMatch(object input)
 766        {
 767            bool match = false;
 68
 69            // When input is null or byte[], return Mismatch.
 770            if (input != null && !(input is byte[]))
 471            {
 72                try
 473                {
 74                    // Check if JToken or object
 475                    JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
 76
 77                    // Check if JToken or string or object
 78                    JToken jtokenValue;
 479                    switch (Value)
 80                    {
 81                        case JToken tokenValue:
 082                            jtokenValue = tokenValue;
 083                            break;
 84
 85                        case string stringValue:
 286                            jtokenValue = JToken.Parse(stringValue);
 287                            break;
 88
 89                        default:
 290                            jtokenValue = JObject.FromObject(Value);
 291                            break;
 92                    }
 93
 494                    match = JToken.DeepEquals(jtokenValue, jtokenInput);
 495                }
 096                catch (JsonException)
 097                {
 98                    // just ignore JsonException
 099                }
 4100            }
 101
 7102            return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match));
 7103        }
 104    }
 105}