Summary

Class:WireMock.Matchers.Request.RequestMessageParamMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageParamMatcher.cs
Covered lines:46
Uncovered lines:1
Coverable lines:47
Total lines:120
Line coverage:97.8%
Branch coverage:85%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using JetBrains.Annotations;
 5using WireMock.Util;
 6using WireMock.Validation;
 7
 8namespace WireMock.Matchers.Request
 9{
 10    /// <summary>
 11    /// The request parameters matcher.
 12    /// </summary>
 13    public class RequestMessageParamMatcher : IRequestMatcher
 14    {
 15        private readonly MatchBehaviour _matchBehaviour;
 16
 17        /// <summary>
 18        /// The funcs
 19        /// </summary>
 1120        public Func<IDictionary<string, WireMockList<string>>, bool>[] Funcs { get; }
 21
 22        /// <summary>
 23        /// The key
 24        /// </summary>
 925        public string Key { get; }
 26
 27        /// <summary>
 28        /// The matchers.
 29        /// </summary>
 2630        public IReadOnlyList<IStringMatcher> Matchers { get; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
 34        /// </summary>
 35        /// <param name="matchBehaviour">The match behaviour.</param>
 36        /// <param name="key">The key.</param>
 337        public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key) : this(matchBehaviour, ke
 338        {
 339        }
 40
 41        /// <summary>
 42        /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
 43        /// </summary>
 44        /// <param name="matchBehaviour">The match behaviour.</param>
 45        /// <param name="key">The key.</param>
 46        /// <param name="values">The values.</param>
 1847        public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] string[] valu
 748        {
 749        }
 50
 51        /// <summary>
 52        /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
 53        /// </summary>
 54        /// <param name="matchBehaviour">The match behaviour.</param>
 55        /// <param name="key">The key.</param>
 56        /// <param name="matchers">The matchers.</param>
 1357        public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] IStringMatche
 1358        {
 1359            Check.NotNull(key, nameof(key));
 60
 1361            _matchBehaviour = matchBehaviour;
 1362            Key = key;
 1363            Matchers = matchers;
 1364        }
 65
 66        /// <summary>
 67        /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
 68        /// </summary>
 69        /// <param name="funcs">The funcs.</param>
 170        public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs
 171        {
 172            Check.NotNull(funcs, nameof(funcs));
 73
 174            Funcs = funcs;
 175        }
 76
 77        /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
 78        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 1079        {
 1080            double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage));
 1081            return requestMatchResult.AddScore(GetType(), score);
 1082        }
 83
 84        private double IsMatch(RequestMessage requestMessage)
 1085        {
 1086            if (Funcs != null)
 187            {
 288                return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
 89            }
 90
 991            WireMockList<string> valuesPresentInRequestMessage = requestMessage.GetParameter(Key);
 992            if (valuesPresentInRequestMessage == null)
 193            {
 94                // Key is not present at all, just return Mismatch
 195                return MatchScores.Mismatch;
 96            }
 97
 898            if (Matchers != null && Matchers.Any())
 599            {
 100                // Matchers are defined, just use the matchers to calculate the match score.
 5101                var scores = new List<double>();
 31102                foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
 8103                {
 24104                    double score = Matchers.Max(m => m.IsMatch(valuePresentInRequestMessage));
 8105                    scores.Add(score);
 8106                }
 107
 5108                return scores.Any() ? scores.Average() : MatchScores.Mismatch;
 109            }
 110
 3111            if (Matchers == null || !Matchers.Any())
 3112            {
 113                // Matchers are null or not defined, and Key is present, just return Perfect.
 3114                return MatchScores.Perfect;
 115            }
 116
 0117            return MatchScores.Mismatch;
 10118        }
 119    }
 120}