Summary

Class:WireMock.Matchers.Request.RequestMessageHeaderMatcher
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageHeaderMatcher.cs
Covered lines:45
Uncovered lines:4
Coverable lines:49
Total lines:119
Line coverage:91.8%
Branch coverage:75%

Metrics

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

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageHeaderMatcher.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 header matcher.
 12    /// </summary>
 13    /// <inheritdoc cref="IRequestMatcher"/>
 14    public class RequestMessageHeaderMatcher : IRequestMatcher
 15    {
 16        /// <summary>
 17        /// The functions
 18        /// </summary>
 2219        public Func<IDictionary<string, string[]>, bool>[] Funcs { get; }
 20
 21        /// <summary>
 22        /// The name
 23        /// </summary>
 3124        public string Name { get; }
 25
 26        /// <value>
 27        /// The matchers.
 28        /// </value>
 3129        public IStringMatcher[] Matchers { get; }
 30
 31        /// <summary>
 32        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 33        /// </summary>
 34        /// <param name="name">The name.</param>
 35        /// <param name="pattern">The pattern.</param>
 36        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
 1337        public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true)
 1338        {
 1339            Check.NotNull(name, nameof(name));
 1340            Check.NotNull(pattern, nameof(pattern));
 41
 1342            Name = name;
 1343            Matchers = new IStringMatcher[] { new WildcardMatcher(pattern, ignoreCase) };
 1344        }
 45
 46        /// <summary>
 47        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 48        /// </summary>
 49        /// <param name="name">The name.</param>
 50        /// <param name="patterns">The patterns.</param>
 51        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
 1252        public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string[] patterns, bool ignoreCase = true)
 1253        {
 1254            Check.NotNull(name, nameof(name));
 1255            Check.NotNull(patterns, nameof(patterns));
 56
 1257            Name = name;
 2458            Matchers = patterns.Select(pattern => new WildcardMatcher(pattern, ignoreCase)).Cast<IStringMatcher>().ToArr
 1259        }
 60
 61        /// <summary>
 62        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 63        /// </summary>
 64        /// <param name="name">The name.</param>
 65        /// <param name="matchers">The matchers.</param>
 166        public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers)
 167        {
 168            Check.NotNull(name, nameof(name));
 169            Check.NotNull(matchers, nameof(matchers));
 70
 171            Name = name;
 172            Matchers = matchers;
 173        }
 74
 75        /// <summary>
 76        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 77        /// </summary>
 78        /// <param name="funcs">The funcs.</param>
 179        public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string[]>, bool>[] funcs)
 180        {
 181            Check.NotNull(funcs, nameof(funcs));
 82
 183            Funcs = funcs;
 184        }
 85
 86        /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
 87        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 2188        {
 2189            double score = IsMatch(requestMessage);
 2190            return requestMatchResult.AddScore(GetType(), score);
 2191        }
 92
 93        private double IsMatch(RequestMessage requestMessage)
 2194        {
 2195             if (requestMessage.Headers == null)
 096            {
 097                return MatchScores.Mismatch;
 98            }
 99
 21100             if (Funcs != null)
 1101            {
 4102                return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers.ToDictionary(entry => entry.Key, entr
 103            }
 104
 20105             if (Matchers == null)
 0106            {
 0107                return MatchScores.Mismatch;
 108            }
 109
 20110             if (!requestMessage.Headers.ContainsKey(Name))
 9111            {
 9112                return MatchScores.Mismatch;
 113            }
 114
 11115            WireMockList<string> list = requestMessage.Headers[Name];
 33116            return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?
 21117        }
 118    }
 119}