| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Util; |
| | | 6 | | using WireMock.Validation; |
| | | 7 | | |
| | | 8 | | namespace 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> |
| | 49 | 22 | | public Func<IDictionary<string, string[]>, bool>[] Funcs { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The name |
| | | 26 | | /// </summary> |
| | 74 | 27 | | public string Name { get; } |
| | | 28 | | |
| | | 29 | | /// <value> |
| | | 30 | | /// The matchers. |
| | | 31 | | /// </value> |
| | 74 | 32 | | 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> |
| | 47 | 41 | | public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string patter |
| | 47 | 42 | | { |
| | 47 | 43 | | Check.NotNull(name, nameof(name)); |
| | 47 | 44 | | Check.NotNull(pattern, nameof(pattern)); |
| | | 45 | | |
| | 47 | 46 | | _matchBehaviour = matchBehaviour; |
| | 47 | 47 | | _ignoreCase = ignoreCase; |
| | 47 | 48 | | Name = name; |
| | 47 | 49 | | Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) }; |
| | 47 | 50 | | } |
| | | 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> |
| | 2 | 59 | | public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string[] patt |
| | 2 | 60 | | { |
| | 2 | 61 | | Check.NotNull(name, nameof(name)); |
| | 2 | 62 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 63 | | |
| | 2 | 64 | | _matchBehaviour = matchBehaviour; |
| | 2 | 65 | | _ignoreCase = ignoreCase; |
| | 2 | 66 | | Name = name; |
| | 6 | 67 | | Matchers = patterns.Select(pattern => new WildcardMatcher(matchBehaviour, pattern, ignoreCase)).Cast<IString |
| | 2 | 68 | | } |
| | | 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> |
| | 2 | 75 | | public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers) |
| | 2 | 76 | | { |
| | 2 | 77 | | Check.NotNull(name, nameof(name)); |
| | 2 | 78 | | Check.NotNull(matchers, nameof(matchers)); |
| | | 79 | | |
| | 2 | 80 | | Name = name; |
| | 2 | 81 | | Matchers = matchers; |
| | 2 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="funcs">The funcs.</param> |
| | 2 | 88 | | public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string[]>, bool>[] funcs) |
| | 2 | 89 | | { |
| | 2 | 90 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 91 | | |
| | 2 | 92 | | Funcs = funcs; |
| | 2 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 96 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 50 | 97 | | { |
| | 50 | 98 | | double score = IsMatch(requestMessage); |
| | 50 | 99 | | return requestMatchResult.AddScore(GetType(), score); |
| | 50 | 100 | | } |
| | | 101 | | |
| | | 102 | | private double IsMatch(RequestMessage requestMessage) |
| | 50 | 103 | | { |
| | 50 | 104 | | if (requestMessage.Headers == null) |
| | 2 | 105 | | { |
| | 2 | 106 | | 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) |
| | 48 | 110 | | var headers = !_ignoreCase ? requestMessage.Headers : new Dictionary<string, WireMockList<string>>(requestMe |
| | | 111 | | |
| | 48 | 112 | | if (Funcs != null) |
| | 1 | 113 | | { |
| | 4 | 114 | | return MatchScores.ToScore(Funcs.Any(f => f(headers.ToDictionary(entry => entry.Key, entry => entry.Valu |
| | | 115 | | } |
| | | 116 | | |
| | 47 | 117 | | if (Matchers == null) |
| | 0 | 118 | | { |
| | 0 | 119 | | return MatchScores.Mismatch; |
| | | 120 | | } |
| | | 121 | | |
| | 47 | 122 | | if (!headers.ContainsKey(Name)) |
| | 20 | 123 | | { |
| | 20 | 124 | | return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); |
| | | 125 | | } |
| | | 126 | | |
| | 27 | 127 | | WireMockList<string> list = headers[Name]; |
| | 81 | 128 | | return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ? |
| | 50 | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |