using System; using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using WireMock.Validation; namespace WireMock.Matchers.Request { /// /// The request path matcher. /// public class RequestMessagePathMatcher : IRequestMatcher { /// /// The matchers /// public IReadOnlyList Matchers { get; } /// /// The path functions /// public Func[] Funcs { get; } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The paths. public RequestMessagePathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(matchBehaviour, path)).Cast().ToArray()) { } /// /// Initializes a new instance of the class. /// /// The matchers. public RequestMessagePathMatcher([NotNull] params IStringMatcher[] matchers) { Check.NotNull(matchers, nameof(matchers)); Matchers = matchers; } /// /// Initializes a new instance of the class. /// /// The path functions. public RequestMessagePathMatcher([NotNull] params Func[] funcs) { Check.NotNull(funcs, nameof(funcs)); Funcs = funcs; } /// public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); return requestMatchResult.AddScore(GetType(), score); } private double IsMatch(RequestMessage requestMessage) { if (Matchers != null) { return Matchers.Max(m => m.IsMatch(requestMessage.Path)); } if (Funcs != null) { return MatchScores.ToScore(requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path))); } return MatchScores.Mismatch; } } }