| | | 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 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> |
| | 10 | 20 | | public Func<IDictionary<string, string>, bool>[] Funcs { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The name |
| | | 24 | | /// </summary> |
| | 14 | 25 | | public string Name { get; } |
| | | 26 | | |
| | | 27 | | /// <value> |
| | | 28 | | /// The matchers. |
| | | 29 | | /// </value> |
| | 14 | 30 | | 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> |
| | 10 | 39 | | public RequestMessageCookieMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string patter |
| | 10 | 40 | | { |
| | 10 | 41 | | Check.NotNull(name, nameof(name)); |
| | 10 | 42 | | Check.NotNull(pattern, nameof(pattern)); |
| | | 43 | | |
| | 10 | 44 | | _matchBehaviour = matchBehaviour; |
| | 10 | 45 | | _ignoreCase = ignoreCase; |
| | 10 | 46 | | Name = name; |
| | 10 | 47 | | Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) }; |
| | 10 | 48 | | } |
| | | 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> |
| | 2 | 55 | | public RequestMessageCookieMatcher([NotNull] string name, [NotNull] params IStringMatcher[] matchers) |
| | 2 | 56 | | { |
| | 2 | 57 | | Check.NotNull(name, nameof(name)); |
| | 2 | 58 | | Check.NotNull(matchers, nameof(matchers)); |
| | | 59 | | |
| | 2 | 60 | | Name = name; |
| | 2 | 61 | | Matchers = matchers; |
| | 2 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="funcs">The funcs.</param> |
| | 2 | 68 | | public RequestMessageCookieMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs) |
| | 2 | 69 | | { |
| | 2 | 70 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 71 | | |
| | 2 | 72 | | Funcs = funcs; |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 76 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 11 | 77 | | { |
| | 11 | 78 | | double score = IsMatch(requestMessage); |
| | 11 | 79 | | return requestMatchResult.AddScore(GetType(), score); |
| | 11 | 80 | | } |
| | | 81 | | |
| | | 82 | | private double IsMatch(RequestMessage requestMessage) |
| | 11 | 83 | | { |
| | 11 | 84 | | if (requestMessage.Cookies == null) |
| | 2 | 85 | | { |
| | 2 | 86 | | return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // Check if we want to use IgnoreCase to compare the Cookie-Name and Cookie-Value |
| | 9 | 90 | | var cookies = !_ignoreCase ? requestMessage.Cookies : new Dictionary<string, string>(requestMessage.Cookies, |
| | | 91 | | |
| | 9 | 92 | | if (Funcs != null) |
| | 1 | 93 | | { |
| | 2 | 94 | | return MatchScores.ToScore(Funcs.Any(f => f(cookies))); |
| | | 95 | | } |
| | | 96 | | |
| | 8 | 97 | | if (Matchers == null) |
| | 0 | 98 | | { |
| | 0 | 99 | | return MatchScores.Mismatch; |
| | | 100 | | } |
| | | 101 | | |
| | 8 | 102 | | if (!cookies.ContainsKey(Name)) |
| | 2 | 103 | | { |
| | 2 | 104 | | return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); |
| | | 105 | | } |
| | | 106 | | |
| | 6 | 107 | | string value = cookies[Name]; |
| | 12 | 108 | | return Matchers.Max(m => m.IsMatch(value)); |
| | 11 | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |