| | | 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 | | /// <summary> |
| | | 16 | | /// The funcs |
| | | 17 | | /// </summary> |
| | 9 | 18 | | public Func<IDictionary<string, WireMockList<string>>, bool>[] Funcs { get; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The key |
| | | 22 | | /// </summary> |
| | 7 | 23 | | public string Key { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The values |
| | | 27 | | /// </summary> |
| | 9 | 28 | | public IEnumerable<string> Values { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="key">The key.</param> |
| | 1 | 34 | | public RequestMessageParamMatcher([NotNull] string key) : this(key, null) |
| | 1 | 35 | | { |
| | 1 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="key">The key.</param> |
| | | 42 | | /// <param name="values">The values.</param> |
| | 7 | 43 | | public RequestMessageParamMatcher([NotNull] string key, [CanBeNull] IEnumerable<string> values) |
| | 7 | 44 | | { |
| | 7 | 45 | | Check.NotNull(key, nameof(key)); |
| | | 46 | | |
| | 7 | 47 | | Key = key; |
| | 7 | 48 | | Values = values; |
| | 7 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="funcs">The funcs.</param> |
| | 1 | 55 | | public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs |
| | 1 | 56 | | { |
| | 1 | 57 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 58 | | |
| | 1 | 59 | | Funcs = funcs; |
| | 1 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 63 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 8 | 64 | | { |
| | 8 | 65 | | double score = IsMatch(requestMessage); |
| | 8 | 66 | | return requestMatchResult.AddScore(GetType(), score); |
| | 8 | 67 | | } |
| | | 68 | | |
| | | 69 | | private double IsMatch(RequestMessage requestMessage) |
| | 8 | 70 | | { |
| | 8 | 71 | | if (Funcs != null) |
| | 1 | 72 | | { |
| | 2 | 73 | | return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query))); |
| | | 74 | | } |
| | | 75 | | |
| | 7 | 76 | | var values = requestMessage.GetParameter(Key); |
| | 7 | 77 | | if (values == null) |
| | 1 | 78 | | { |
| | | 79 | | // Key is not present, just return Mismatch |
| | 1 | 80 | | return MatchScores.Mismatch; |
| | | 81 | | } |
| | | 82 | | |
| | 6 | 83 | | if (values.Count == 0 && (Values == null || !Values.Any())) |
| | 2 | 84 | | { |
| | | 85 | | // Key is present, but no values or null, just return Perfect |
| | 2 | 86 | | return MatchScores.Perfect; |
| | | 87 | | } |
| | | 88 | | |
| | 16 | 89 | | var matches = Values.Select(v => values.Contains(v)); |
| | 4 | 90 | | return MatchScores.ToScore(matches); |
| | 8 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |