| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers.Request |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The request header matcher. |
| | | 11 | | /// </summary> |
| | | 12 | | public class RequestMessageHeaderMatcher : IRequestMatcher |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The functions |
| | | 16 | | /// </summary> |
| | 4 | 17 | | public Func<IDictionary<string, string>, bool>[] Funcs { get; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The name |
| | | 21 | | /// </summary> |
| | 8 | 22 | | public string Name { get; } |
| | | 23 | | |
| | | 24 | | /// <value> |
| | | 25 | | /// The matchers. |
| | | 26 | | /// </value> |
| | 8 | 27 | | public IMatcher[] Matchers { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="name">The name.</param> |
| | | 33 | | /// <param name="pattern">The pattern.</param> |
| | | 34 | | /// <param name="ignoreCase">The ignoreCase.</param> |
| | 4 | 35 | | public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true) |
| | 4 | 36 | | { |
| | 4 | 37 | | Check.NotNull(name, nameof(name)); |
| | 4 | 38 | | Check.NotNull(pattern, nameof(pattern)); |
| | | 39 | | |
| | 4 | 40 | | Name = name; |
| | 4 | 41 | | Matchers = new IMatcher[] { new WildcardMatcher(pattern, ignoreCase) }; |
| | 4 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="name">The name.</param> |
| | | 48 | | /// <param name="matchers">The matchers.</param> |
| | 0 | 49 | | public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] params IMatcher[] matchers) |
| | 0 | 50 | | { |
| | 0 | 51 | | Check.NotNull(name, nameof(name)); |
| | 0 | 52 | | Check.NotNull(matchers, nameof(matchers)); |
| | | 53 | | |
| | 0 | 54 | | Name = name; |
| | 0 | 55 | | Matchers = matchers; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="funcs">The funcs.</param> |
| | 0 | 62 | | public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs) |
| | 0 | 63 | | { |
| | 0 | 64 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 65 | | |
| | 0 | 66 | | Funcs = funcs; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Determines whether the specified RequestMessage is match. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="requestMessage">The RequestMessage.</param> |
| | | 73 | | /// <param name="requestMatchResult">The RequestMatchResult.</param> |
| | | 74 | | /// <returns> |
| | | 75 | | /// A value between 0.0 - 1.0 of the similarity. |
| | | 76 | | /// </returns> |
| | | 77 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 4 | 78 | | { |
| | 4 | 79 | | double score = IsMatch(requestMessage); |
| | 4 | 80 | | requestMatchResult.TotalScore += score; |
| | | 81 | | |
| | 4 | 82 | | requestMatchResult.TotalNumber++; |
| | | 83 | | |
| | 4 | 84 | | return score; |
| | 4 | 85 | | } |
| | | 86 | | |
| | | 87 | | private double IsMatch(RequestMessage requestMessage) |
| | 4 | 88 | | { |
| | 4 | 89 | | if (requestMessage.Headers == null) |
| | 0 | 90 | | return MatchScores.Mismatch; |
| | | 91 | | |
| | 4 | 92 | | if (Funcs != null) |
| | 0 | 93 | | return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers))); |
| | | 94 | | |
| | 4 | 95 | | if (Matchers == null) |
| | 0 | 96 | | return MatchScores.Mismatch; |
| | | 97 | | |
| | 4 | 98 | | if (!requestMessage.Headers.ContainsKey(Name)) |
| | 0 | 99 | | return MatchScores.Mismatch; |
| | | 100 | | |
| | 4 | 101 | | string value = requestMessage.Headers[Name]; |
| | 8 | 102 | | return Matchers.Max(m => m.IsMatch(value)); |
| | 4 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |