Summary

Class:WireMock.Matchers.Request.RequestMessageHeaderMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\Request\RequestMessageHeaderMatcher.cs
Covered lines:52
Uncovered lines:2
Coverable lines:54
Total lines:131
Line coverage:96.2%
Branch coverage:90%

Metrics

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

File(s)

C:\Users\StefHeyenrath\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        private readonly MatchBehaviour _matchBehaviour;
 17        private readonly bool _ignoreCase;
 18
 19        /// <summary>
 20        /// The functions
 21        /// </summary>
 4922        public Func<IDictionary<string, string[]>, bool>[] Funcs { get; }
 23
 24        /// <summary>
 25        /// The name
 26        /// </summary>
 7427        public string Name { get; }
 28
 29        /// <value>
 30        /// The matchers.
 31        /// </value>
 7432        public IStringMatcher[] Matchers { get; }
 33
 34        /// <summary>
 35        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 36        /// </summary>
 37        /// <param name="name">The name.</param>
 38        /// <param name="pattern">The pattern.</param>
 39        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 40        /// <param name="matchBehaviour">The match behaviour.</param>
 4741        public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string patter
 4742        {
 4743            Check.NotNull(name, nameof(name));
 4744            Check.NotNull(pattern, nameof(pattern));
 45
 4746            _matchBehaviour = matchBehaviour;
 4747            _ignoreCase = ignoreCase;
 4748            Name = name;
 4749            Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
 4750        }
 51
 52        /// <summary>
 53        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 54        /// </summary>
 55        /// <param name="name">The name.</param>
 56        /// <param name="patterns">The patterns.</param>
 57        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 58        /// <param name="matchBehaviour">The match behaviour.</param>
 259        public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string[] patt
 260        {
 261            Check.NotNull(name, nameof(name));
 262            Check.NotNull(patterns, nameof(patterns));
 63
 264            _matchBehaviour = matchBehaviour;
 265            _ignoreCase = ignoreCase;
 266            Name = name;
 667            Matchers = patterns.Select(pattern => new WildcardMatcher(matchBehaviour, pattern, ignoreCase)).Cast<IString
 268        }
 69
 70        /// <summary>
 71        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 72        /// </summary>
 73        /// <param name="name">The name.</param>
 74        /// <param name="matchers">The matchers.</param>
 275        public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers)
 276        {
 277            Check.NotNull(name, nameof(name));
 278            Check.NotNull(matchers, nameof(matchers));
 79
 280            Name = name;
 281            Matchers = matchers;
 282        }
 83
 84        /// <summary>
 85        /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
 86        /// </summary>
 87        /// <param name="funcs">The funcs.</param>
 288        public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string[]>, bool>[] funcs)
 289        {
 290            Check.NotNull(funcs, nameof(funcs));
 91
 292            Funcs = funcs;
 293        }
 94
 95        /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
 96        public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
 5097        {
 5098            double score = IsMatch(requestMessage);
 5099            return requestMatchResult.AddScore(GetType(), score);
 50100        }
 101
 102        private double IsMatch(RequestMessage requestMessage)
 50103        {
 50104            if (requestMessage.Headers == null)
 2105            {
 2106                return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
 107            }
 108
 109            // Check if we want to use IgnoreCase to compare the Header-Name and Header-Value(s)
 48110            var headers = !_ignoreCase ? requestMessage.Headers : new Dictionary<string, WireMockList<string>>(requestMe
 111
 48112            if (Funcs != null)
 1113            {
 4114                return MatchScores.ToScore(Funcs.Any(f => f(headers.ToDictionary(entry => entry.Key, entry => entry.Valu
 115            }
 116
 47117            if (Matchers == null)
 0118            {
 0119                return MatchScores.Mismatch;
 120            }
 121
 47122            if (!headers.ContainsKey(Name))
 20123            {
 20124                return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
 125            }
 126
 27127            WireMockList<string> list = headers[Name];
 81128            return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?
 50129        }
 130    }
 131}