Summary

Class:WireMock.Matchers.Request.RequestMessageBodyMatcher
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageBodyMatcher.cs
Covered lines:62
Uncovered lines:2
Coverable lines:64
Total lines:144
Line coverage:96.8%
Branch coverage:87.5%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
.ctor(...)10100100
.ctor(...)10100100
.ctor(...)10100100
.ctor(...)10100100
.ctor(...)10100100
.ctor(...)10100100
GetMatchingScore(...)10100100
IsMatch(...)1225692.5988.24

File(s)

C:\Users\azureuser\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>
 415        public Func<string, bool> Func { get; }
 16
 17        /// <summary>
 18        /// The body data function for byte[]
 19        /// </summary>
 320        public Func<byte[], bool> DataFunc { get; }
 21
 22        /// <summary>
 23        /// The body data function for json
 24        /// </summary>
 225        public Func<object, bool> JsonFunc { get; }
 26
 27        /// <summary>
 28        /// The matcher.
 29        /// </summary>
 1730        public IMatcher Matcher { get; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 34        /// </summary>
 35        /// <param name="body">The body.</param>
 236        public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body))
 237        {
 238        }
 39
 40        /// <summary>
 41        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 42        /// </summary>
 43        /// <param name="body">The body.</param>
 144        public RequestMessageBodyMatcher([NotNull] byte[] body) : this(new ExactObjectMatcher(body))
 145        {
 146        }
 47
 48        /// <summary>
 49        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 50        /// </summary>
 51        /// <param name="body">The body.</param>
 152        public RequestMessageBodyMatcher([NotNull] object body) : this(new ExactObjectMatcher(body))
 153        {
 154        }
 55
 56        /// <summary>
 57        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 58        /// </summary>
 59        /// <param name="func">The function.</param>
 160        public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
 161        {
 162            Check.NotNull(func, nameof(func));
 163            Func = func;
 164        }
 65
 66        /// <summary>
 67        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 68        /// </summary>
 69        /// <param name="func">The function.</param>
 170        public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
 171        {
 172            Check.NotNull(func, nameof(func));
 173            DataFunc = func;
 174        }
 75
 76        /// <summary>
 77        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 78        /// </summary>
 79        /// <param name="func">The function.</param>
 180        public RequestMessageBodyMatcher([NotNull] Func<object, bool> func)
 181        {
 182            Check.NotNull(func, nameof(func));
 183            JsonFunc = func;
 184        }
 85
 86        /// <summary>
 87        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 88        /// </summary>
 89        /// <param name="matcher">The matcher.</param>
 1790        public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
 1791        {
 1792            Check.NotNull(matcher, nameof(matcher));
 1793            Matcher = matcher;
 1794        }
 95
 96        /// <see cref="IRequestMatcher.GetMatchingScore"/>
 97        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 1698        {
 1699            double score = IsMatch(requestMessage);
 16100            return requestMatchResult.AddScore(GetType(), score);
 16101        }
 102
 103        private double IsMatch(RequestMessage requestMessage)
 16104        {
 16105             if (requestMessage.Body != null)
 9106            {
 9107                 if (Matcher is IStringMatcher stringMatcher)
 8108                {
 8109                    return stringMatcher.IsMatch(requestMessage.Body);
 110                }
 1111            }
 112
 8113             if (Matcher is IObjectMatcher objectMatcher)
 5114            {
 5115                 if (requestMessage.BodyAsJson != null)
 4116                {
 4117                    return objectMatcher.IsMatch(requestMessage.BodyAsJson);
 118                }
 119
 1120                 if (requestMessage.BodyAsBytes != null)
 1121                {
 1122                    return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
 123                }
 0124            }
 125
 3126             if (Func != null)
 1127            {
 1128                return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
 129            }
 130
 2131             if (DataFunc != null)
 1132            {
 1133                return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes));
 134            }
 135
 1136             if (JsonFunc != null)
 1137            {
 1138                return MatchScores.ToScore(requestMessage.BodyAsJson != null && JsonFunc(requestMessage.BodyAsJson));
 139            }
 140
 0141            return MatchScores.Mismatch;
 16142        }
 143    }
 144}