| | | 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> |
| | 5 | 18 | | public Func<IDictionary<string, WireMockList<string>>, bool>[] Funcs { get; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The key |
| | | 22 | | /// </summary> |
| | 3 | 23 | | public string Key { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The values |
| | | 27 | | /// </summary> |
| | 5 | 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"> |
| | | 34 | | /// The key. |
| | | 35 | | /// </param> |
| | | 36 | | /// <param name="values"> |
| | | 37 | | /// The values. |
| | | 38 | | /// </param> |
| | 3 | 39 | | public RequestMessageParamMatcher([NotNull] string key, [CanBeNull] IEnumerable<string> values) |
| | 3 | 40 | | { |
| | 3 | 41 | | Check.NotNull(key, nameof(key)); |
| | | 42 | | |
| | 3 | 43 | | Key = key; |
| | 3 | 44 | | Values = values; |
| | 3 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="funcs">The funcs.</param> |
| | 1 | 51 | | public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs |
| | 1 | 52 | | { |
| | 1 | 53 | | Check.NotNull(funcs, nameof(funcs)); |
| | 1 | 54 | | Funcs = funcs; |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Determines whether the specified RequestMessage is match. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="requestMessage">The RequestMessage.</param> |
| | | 61 | | /// <param name="requestMatchResult">The RequestMatchResult.</param> |
| | | 62 | | /// <returns> |
| | | 63 | | /// A value between 0.0 - 1.0 of the similarity. |
| | | 64 | | /// </returns> |
| | | 65 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 4 | 66 | | { |
| | 4 | 67 | | double score = IsMatch(requestMessage); |
| | 4 | 68 | | requestMatchResult.TotalScore += score; |
| | | 69 | | |
| | 4 | 70 | | requestMatchResult.TotalNumber++; |
| | | 71 | | |
| | 4 | 72 | | return score; |
| | 4 | 73 | | } |
| | | 74 | | |
| | | 75 | | private double IsMatch(RequestMessage requestMessage) |
| | 4 | 76 | | { |
| | 4 | 77 | | if (Funcs != null) |
| | 2 | 78 | | return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query))); |
| | | 79 | | |
| | 3 | 80 | | List<string> values = requestMessage.GetParameter(Key); |
| | | 81 | | |
| | 3 | 82 | | return MatchScores.ToScore(values?.Intersect(Values).Count() == Values.Count()); |
| | 4 | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |