// Copyright © WireMock.Net using System.Linq; using Stef.Validation; namespace WireMock.Matchers.Request; /// /// The composite request matcher. /// public abstract class RequestMessageCompositeMatcher : IRequestMatcher { private readonly CompositeMatcherType _type; /// /// Gets the request matchers. /// /// /// The request matchers. /// private IEnumerable RequestMatchers { get; } /// /// Selected type to choose the matcher from which will immediately return a mismatch. /// internal RequestMatcherType? EarlyMatcherType { get; private protected set; } /// /// Initializes a new instance of the class. /// /// The request matchers. /// The CompositeMatcherType type (Defaults to 'And') protected RequestMessageCompositeMatcher(IEnumerable requestMatchers, CompositeMatcherType type = CompositeMatcherType.And) { RequestMatchers = Guard.NotNull(requestMatchers); _type = type; } /// public RequestMatcherType Type => RequestMatcherType.Composite; /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { if (!RequestMatchers.Any()) { return MatchScores.Mismatch; } var earlyMatcher = new RequestMessageEarlyMatcher(EarlyMatcherType, RequestMatchers); var earlyMatchResult = earlyMatcher.GetMatchingScore(requestMessage, requestMatchResult); if (!MatchScores.IsPerfect(earlyMatchResult)) { return MatchScores.Mismatch; } if (_type == CompositeMatcherType.And) { return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult)); } return RequestMatchers.Max(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult)); } }