Summary

Class:WireMock.Matchers.Request.RequestMessageBodyMatcher
Assembly:WireMock.Net
File(s):C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageBodyMatcher.cs
Covered lines:19
Uncovered lines:24
Coverable lines:43
Total lines:125
Line coverage:44.1%
Branch coverage:12.5%

Metrics

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

File(s)

C:\Users\Stef\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 as byte[].
 14        /// </summary>
 15        private readonly byte[] _bodyData;
 16
 17        /// <summary>
 18        /// The body function
 19        /// </summary>
 020        public Func<string, bool> Func { get; }
 21
 22        /// <summary>
 23        /// The body data function
 24        /// </summary>
 025        public Func<byte[], bool> DataFunc { get; }
 26
 27        /// <summary>
 28        /// The matcher.
 29        /// </summary>
 2630        public IMatcher Matcher { get; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 34        /// </summary>
 35        /// <param name="body">
 36        /// The body Regex pattern.
 37        /// </param>
 238        public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body))
 239        {
 240        }
 41
 42        /// <summary>
 43        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 44        /// </summary>
 45        /// <param name="body">
 46        /// The body Regex pattern.
 47        /// </param>
 048        public RequestMessageBodyMatcher([NotNull] byte[] body)
 049        {
 050            Check.NotNull(body, nameof(body));
 051            _bodyData = body;
 052        }
 53
 54        /// <summary>
 55        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 56        /// </summary>
 57        /// <param name="func">
 58        /// The body func.
 59        /// </param>
 060        public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
 061        {
 062            Check.NotNull(func, nameof(func));
 063            Func = func;
 064        }
 65
 66        /// <summary>
 67        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 68        /// </summary>
 69        /// <param name="func">
 70        /// The body func.
 71        /// </param>
 072        public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
 073        {
 074            Check.NotNull(func, nameof(func));
 075            DataFunc = func;
 076        }
 77
 78        /// <summary>
 79        /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
 80        /// </summary>
 81        /// <param name="matcher">
 82        /// The body matcher.
 83        /// </param>
 1584        public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
 1585        {
 1586            Check.NotNull(matcher, nameof(matcher));
 1587            Matcher = matcher;
 1588        }
 89
 90        /// <summary>
 91        /// Determines whether the specified RequestMessage is match.
 92        /// </summary>
 93        /// <param name="requestMessage">The RequestMessage.</param>
 94        /// <param name="requestMatchResult">The RequestMatchResult.</param>
 95        /// <returns>
 96        /// A value between 0.0 - 1.0 of the similarity.
 97        /// </returns>
 98        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 1399        {
 13100            double score = IsMatch(requestMessage);
 13101            requestMatchResult.TotalScore += score;
 102
 13103            requestMatchResult.TotalNumber++;
 104
 13105            return score;
 13106        }
 107
 108        private double IsMatch(RequestMessage requestMessage)
 13109        {
 13110             if (Matcher != null)
 13111                return Matcher.IsMatch(requestMessage.Body);
 112
 0113             if (_bodyData != null)
 0114                return MatchScores.ToScore(requestMessage.BodyAsBytes == _bodyData);
 115
 0116             if (Func != null)
 0117                return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
 118
 0119             if (DataFunc != null && requestMessage.BodyAsBytes != null)
 0120                return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes));
 121
 0122            return MatchScores.Mismatch;
 13123        }
 124    }
 125}