| | | 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 path matcher. |
| | | 11 | | /// </summary> |
| | | 12 | | public class RequestMessagePathMatcher : IRequestMatcher |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The matcher. |
| | | 16 | | /// </summary> |
| | 53 | 17 | | public IReadOnlyList<IMatcher> Matchers { get; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The path functions |
| | | 21 | | /// </summary> |
| | 2 | 22 | | public Func<string, bool>[] Funcs { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="paths">The paths.</param> |
| | 51 | 28 | | public RequestMessagePathMatcher([NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatche |
| | 25 | 29 | | { |
| | 25 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="matchers">The matchers.</param> |
| | 31 | 36 | | public RequestMessagePathMatcher([NotNull] params IMatcher[] matchers) |
| | 31 | 37 | | { |
| | 31 | 38 | | Check.NotNull(matchers, nameof(matchers)); |
| | 31 | 39 | | Matchers = matchers; |
| | 31 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="funcs">The path functions.</param> |
| | 1 | 46 | | public RequestMessagePathMatcher([NotNull] params Func<string, bool>[] funcs) |
| | 1 | 47 | | { |
| | 1 | 48 | | Check.NotNull(funcs, nameof(funcs)); |
| | 1 | 49 | | Funcs = funcs; |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Determines whether the specified RequestMessage is match. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="requestMessage">The RequestMessage.</param> |
| | | 56 | | /// <param name="requestMatchResult">The RequestMatchResult.</param> |
| | | 57 | | /// <returns> |
| | | 58 | | /// A value between 0.0 - 1.0 of the similarity. |
| | | 59 | | /// </returns> |
| | | 60 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 27 | 61 | | { |
| | 27 | 62 | | double score = IsMatch(requestMessage); |
| | 27 | 63 | | requestMatchResult.TotalScore += score; |
| | | 64 | | |
| | 27 | 65 | | requestMatchResult.TotalNumber++; |
| | | 66 | | |
| | 27 | 67 | | return score; |
| | 27 | 68 | | } |
| | | 69 | | |
| | | 70 | | private double IsMatch(RequestMessage requestMessage) |
| | 27 | 71 | | { |
| | 27 | 72 | | if (Matchers != null) |
| | 54 | 73 | | return Matchers.Max(m => m.IsMatch(requestMessage.Path)); |
| | | 74 | | |
| | 1 | 75 | | if (Funcs != null) |
| | 2 | 76 | | return MatchScores.ToScore(requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path))); |
| | | 77 | | |
| | 0 | 78 | | return MatchScores.Mismatch; |
| | 27 | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |