Summary

Class:WireMock.Matchers.Request.RequestMessageCookieMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageCookieMatcher.cs
Covered lines:43
Uncovered lines:2
Coverable lines:45
Total lines:111
Line coverage:95.5%
Branch coverage:90%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using JetBrains.Annotations;
 5using WireMock.Validation;
 6
 7namespace WireMock.Matchers.Request
 8{
 9    /// <summary>
 10    /// The request cookie matcher.
 11    /// </summary>
 12    public class RequestMessageCookieMatcher : IRequestMatcher
 13    {
 14        private readonly MatchBehaviour _matchBehaviour;
 15        private readonly bool _ignoreCase;
 16
 17        /// <value>
 18        /// The funcs.
 19        /// </value>
 1020        public Func<IDictionary<string, string>, bool>[] Funcs { get; }
 21
 22        /// <summary>
 23        /// The name
 24        /// </summary>
 1425        public string Name { get; }
 26
 27        /// <value>
 28        /// The matchers.
 29        /// </value>
 1430        public IStringMatcher[] Matchers { get; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
 34        /// </summary>
 35        /// <param name="matchBehaviour">The match behaviour.</param>
 36        /// <param name="name">The name.</param>
 37        /// <param name="pattern">The pattern.</param>
 38        /// <param name="ignoreCase">The ignoreCase.</param>
 1039        public RequestMessageCookieMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string patter
 1040        {
 1041            Check.NotNull(name, nameof(name));
 1042            Check.NotNull(pattern, nameof(pattern));
 43
 1044            _matchBehaviour = matchBehaviour;
 1045            _ignoreCase = ignoreCase;
 1046            Name = name;
 1047            Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
 1048        }
 49
 50        /// <summary>
 51        /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
 52        /// </summary>
 53        /// <param name="name">The name.</param>
 54        /// <param name="matchers">The matchers.</param>
 255        public RequestMessageCookieMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers)
 256        {
 257            Check.NotNull(name, nameof(name));
 258            Check.NotNull(matchers, nameof(matchers));
 59
 260            Name = name;
 261            Matchers = matchers;
 262        }
 63
 64        /// <summary>
 65        /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
 66        /// </summary>
 67        /// <param name="funcs">The funcs.</param>
 268        public RequestMessageCookieMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs)
 269        {
 270            Check.NotNull(funcs, nameof(funcs));
 71
 272            Funcs = funcs;
 273        }
 74
 75        /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
 76        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 1177        {
 1178            double score = IsMatch(requestMessage);
 1179            return requestMatchResult.AddScore(GetType(), score);
 1180        }
 81
 82        private double IsMatch(RequestMessage requestMessage)
 1183        {
 1184            if (requestMessage.Cookies == null)
 285            {
 286                return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
 87            }
 88
 89            // Check if we want to use IgnoreCase to compare the Cookie-Name and Cookie-Value
 990            var cookies = !_ignoreCase ? requestMessage.Cookies : new Dictionary<string, string>(requestMessage.Cookies,
 91
 992            if (Funcs != null)
 193            {
 294                return MatchScores.ToScore(Funcs.Any(f => f(cookies)));
 95            }
 96
 897            if (Matchers == null)
 098            {
 099                return MatchScores.Mismatch;
 100            }
 101
 8102            if (!cookies.ContainsKey(Name))
 2103            {
 2104                return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
 105            }
 106
 6107            string value = cookies[Name];
 12108            return Matchers.Max(m => m.IsMatch(value));
 11109        }
 110    }
 111}