Summary

Class:WireMock.Matchers.Request.RequestMessageBodyMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageBodyMatcher.cs
Covered lines:67
Uncovered lines:0
Coverable lines:67
Total lines:158
Line coverage:100%
Branch coverage:88.4%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using JetBrains.Annotations;
 3using WireMock.Validation;
 4
 5namespace WireMock.Matchers.Request
 6{
 7    /// <summary>
 8    /// The request body matcher.
 9    /// </summary>
 10    public class RequestMessageBodyMatcher : IRequestMatcher
 11    {
 12        /// <summary>
 13        /// The body function
 14        /// </summary>
 615        public Func<string, bool> Func { get; }
 16
 17        /// <summary>
 18        /// The body data function for byte[]
 19        /// </summary>
 520        public Func<byte[], bool> DataFunc { get; }
 21
 22        /// <summary>
 23        /// The body data function for json
 24        /// </summary>
 425        public Func<object, bool> JsonFunc { get; }
 26
 27        /// <summary>
 28        /// The matcher.
 29        /// </summary>
 3830        public IMatcher Matcher { get; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 34        /// </summary>
 35        /// <param name="matchBehaviour">The match behaviour.</param>
 36        /// <param name="body">The body.</param>
 237        public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new WildcardMatche
 238        {
 239        }
 40
 41        /// <summary>
 42        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 43        /// </summary>
 44        /// <param name="matchBehaviour">The match behaviour.</param>
 45        /// <param name="body">The body.</param>
 146        public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new ExactObjectMat
 147        {
 148        }
 49
 50        /// <summary>
 51        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 52        /// </summary>
 53        /// <param name="matchBehaviour">The match behaviour.</param>
 54        /// <param name="body">The body.</param>
 155        public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new ExactObjectMat
 156        {
 157        }
 58
 59        /// <summary>
 60        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 61        /// </summary>
 62        /// <param name="func">The function.</param>
 163        public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
 164        {
 165            Check.NotNull(func, nameof(func));
 166            Func = func;
 167        }
 68
 69        /// <summary>
 70        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 71        /// </summary>
 72        /// <param name="func">The function.</param>
 173        public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
 174        {
 175            Check.NotNull(func, nameof(func));
 176            DataFunc = func;
 177        }
 78
 79        /// <summary>
 80        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 81        /// </summary>
 82        /// <param name="func">The function.</param>
 183        public RequestMessageBodyMatcher([NotNull] Func<object, bool> func)
 184        {
 185            Check.NotNull(func, nameof(func));
 186            JsonFunc = func;
 187        }
 88
 89        /// <summary>
 90        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 91        /// </summary>
 92        /// <param name="matcher">The matcher.</param>
 2393        public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
 2394        {
 2395            Check.NotNull(matcher, nameof(matcher));
 2396            Matcher = matcher;
 2397        }
 98
 99        /// <see cref="IRequestMatcher.GetMatchingScore"/>
 100        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 22101        {
 22102            double score = IsMatch(requestMessage);
 22103            return requestMatchResult.AddScore(GetType(), score);
 22104        }
 105
 106        private double IsMatch(RequestMessage requestMessage)
 22107        {
 108            // Check if the matcher is a IObjectMatcher
 22109            if (Matcher is IObjectMatcher objectMatcher)
 9110            {
 111                // If the body is a JSON object, try to match.
 9112                if (requestMessage.BodyAsJson != null)
 5113                {
 5114                    return objectMatcher.IsMatch(requestMessage.BodyAsJson);
 115                }
 116
 117                // If the body is a byte array, try to match.
 4118                if (requestMessage.BodyAsBytes != null)
 2119                {
 2120                    return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
 121                }
 2122            }
 123
 124            // Check if the matcher is a IStringMatcher
 15125            if (Matcher is IStringMatcher stringMatcher)
 12126            {
 127                // If the  body is a JSON object, try to use Body (string) to match.
 12128                if (requestMessage.BodyAsJson != null && requestMessage.Body != null)
 1129                {
 1130                    return stringMatcher.IsMatch(requestMessage.Body);
 131                }
 132
 133                // If the string body is defined, try to match.
 11134                if (requestMessage.Body != null)
 9135                {
 9136                    return stringMatcher.IsMatch(requestMessage.Body);
 137                }
 2138            }
 139
 5140            if (Func != null)
 1141            {
 1142                return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
 143            }
 144
 4145            if (DataFunc != null)
 1146            {
 1147                return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes));
 148            }
 149
 3150            if (JsonFunc != null)
 1151            {
 1152                return MatchScores.ToScore(requestMessage.BodyAsJson != null && JsonFunc(requestMessage.BodyAsJson));
 153            }
 154
 2155            return MatchScores.Mismatch;
 22156        }
 157    }
 158}