| | | 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 parameters matcher. |
| | | 12 | | /// </summary> |
| | | 13 | | public class RequestMessageParamMatcher : IRequestMatcher |
| | | 14 | | { |
| | | 15 | | private readonly MatchBehaviour _matchBehaviour; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The funcs |
| | | 19 | | /// </summary> |
| | 11 | 20 | | public Func<IDictionary<string, WireMockList<string>>, bool>[] Funcs { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The key |
| | | 24 | | /// </summary> |
| | 9 | 25 | | public string Key { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The matchers. |
| | | 29 | | /// </summary> |
| | 26 | 30 | | public IReadOnlyList<IStringMatcher> Matchers { get; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 36 | | /// <param name="key">The key.</param> |
| | 3 | 37 | | public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key) : this(matchBehaviour, ke |
| | 3 | 38 | | { |
| | 3 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 45 | | /// <param name="key">The key.</param> |
| | | 46 | | /// <param name="values">The values.</param> |
| | 18 | 47 | | public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] string[] valu |
| | 7 | 48 | | { |
| | 7 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 55 | | /// <param name="key">The key.</param> |
| | | 56 | | /// <param name="matchers">The matchers.</param> |
| | 13 | 57 | | public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] IStringMatche |
| | 13 | 58 | | { |
| | 13 | 59 | | Check.NotNull(key, nameof(key)); |
| | | 60 | | |
| | 13 | 61 | | _matchBehaviour = matchBehaviour; |
| | 13 | 62 | | Key = key; |
| | 13 | 63 | | Matchers = matchers; |
| | 13 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="funcs">The funcs.</param> |
| | 1 | 70 | | public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs |
| | 1 | 71 | | { |
| | 1 | 72 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 73 | | |
| | 1 | 74 | | Funcs = funcs; |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 78 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 10 | 79 | | { |
| | 10 | 80 | | double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage)); |
| | 10 | 81 | | return requestMatchResult.AddScore(GetType(), score); |
| | 10 | 82 | | } |
| | | 83 | | |
| | | 84 | | private double IsMatch(RequestMessage requestMessage) |
| | 10 | 85 | | { |
| | 10 | 86 | | if (Funcs != null) |
| | 1 | 87 | | { |
| | 2 | 88 | | return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query))); |
| | | 89 | | } |
| | | 90 | | |
| | 9 | 91 | | WireMockList<string> valuesPresentInRequestMessage = requestMessage.GetParameter(Key); |
| | 9 | 92 | | if (valuesPresentInRequestMessage == null) |
| | 1 | 93 | | { |
| | | 94 | | // Key is not present at all, just return Mismatch |
| | 1 | 95 | | return MatchScores.Mismatch; |
| | | 96 | | } |
| | | 97 | | |
| | 8 | 98 | | if (Matchers != null && Matchers.Any()) |
| | 5 | 99 | | { |
| | | 100 | | // Matchers are defined, just use the matchers to calculate the match score. |
| | 5 | 101 | | var scores = new List<double>(); |
| | 31 | 102 | | foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage) |
| | 8 | 103 | | { |
| | 24 | 104 | | double score = Matchers.Max(m => m.IsMatch(valuePresentInRequestMessage)); |
| | 8 | 105 | | scores.Add(score); |
| | 8 | 106 | | } |
| | | 107 | | |
| | 5 | 108 | | return scores.Any() ? scores.Average() : MatchScores.Mismatch; |
| | | 109 | | } |
| | | 110 | | |
| | 3 | 111 | | if (Matchers == null || !Matchers.Any()) |
| | 3 | 112 | | { |
| | | 113 | | // Matchers are null or not defined, and Key is present, just return Perfect. |
| | 3 | 114 | | return MatchScores.Perfect; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return MatchScores.Mismatch; |
| | 10 | 118 | | } |
| | | 119 | | } |
| | | 120 | | } |