| | | 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 matchers |
| | | 16 | | /// </summary> |
| | 593 | 17 | | public IReadOnlyList<IStringMatcher> 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="matchBehaviour">The match behaviour.</param> |
| | | 28 | | /// <param name="paths">The paths.</param> |
| | 392 | 29 | | public RequestMessagePathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] paths) : this(paths.Se |
| | 196 | 30 | | { |
| | 196 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="matchers">The matchers.</param> |
| | 262 | 37 | | public RequestMessagePathMatcher([NotNull] params IStringMatcher[] matchers) |
| | 262 | 38 | | { |
| | 262 | 39 | | Check.NotNull(matchers, nameof(matchers)); |
| | | 40 | | |
| | 262 | 41 | | Matchers = matchers; |
| | 262 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="funcs">The path functions.</param> |
| | 1 | 48 | | public RequestMessagePathMatcher([NotNull] params Func<string, bool>[] funcs) |
| | 1 | 49 | | { |
| | 1 | 50 | | Check.NotNull(funcs, nameof(funcs)); |
| | | 51 | | |
| | 1 | 52 | | Funcs = funcs; |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 56 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 296 | 57 | | { |
| | 296 | 58 | | double score = IsMatch(requestMessage); |
| | 296 | 59 | | return requestMatchResult.AddScore(GetType(), score); |
| | 296 | 60 | | } |
| | | 61 | | |
| | | 62 | | private double IsMatch(RequestMessage requestMessage) |
| | 296 | 63 | | { |
| | 296 | 64 | | if (Matchers != null) |
| | 295 | 65 | | { |
| | 592 | 66 | | return Matchers.Max(m => m.IsMatch(requestMessage.Path)); |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | if (Funcs != null) |
| | 1 | 70 | | { |
| | 2 | 71 | | return MatchScores.ToScore(requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path))); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return MatchScores.Mismatch; |
| | 296 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |